mardi 23 juin 2020

Apply solid principle without too much repetition of codes?

I have tried to practice solid principle using some simple classes. Basically I want to grab a few fields from product A and product B to fit into a general class Product. And I want to be able to calculate the price based on currency. I devide the GetInUSDollars() in separate as I want to leave the possibility for more currencies without changing the exising class. But try to use a single class for one function seems to create so much duplicates. Any direction I should go for to reduce the amount of duplicates?

public class ProductAData:IProduct
    {
        public List<Product> GetAll()
        {
            return (from n in new ProductARepository().GetAll()
                    select new Product { Id = n.Id, model = n.Model, Price = n.Price, Type = "ProductA" }).ToList();
        }
    }
public class ProductADataInUSDollars: IProductInUSDollars
    {
        public List<Product> GetInUSDollars()
        {
            return (from n in new PhoneCaseRepository().GetAll()
                select new Product { Id = n.Id, model= n.Model, Price = n.Price * 0.85, Type = "ProductA" }).ToList();
        }
    }

There is another class that should fit into Product class as well.

public class ProductBCaseData : IProduct
{

    public List<Product> GetAll()
    {
        return (from n in new ProductBRepository().GetAll()
                select new Product { Id = n.Id, model= n.Model, Price = n.Price, Type = "ProductB" }).ToList();
    }

}
public class ProductBDataInUSDollars: IProductInUSDollars
        {
            public List<Product> GetInUSDollars()
            {
                return (from n in new ProductBRepository().GetAll()
                    select new Product { Id = n.Id, model= n.Model, Price = n.Price * 0.85, Type = "ProductB" }).ToList();
            }
        }

Aucun commentaire:

Enregistrer un commentaire