开发者

How do i extend a property through inheritance?

开发者 https://www.devze.com 2023-02-15 16:58 出处:网络
i have a class Product that contains a Dictionary with a price curve. The key is a string that can be parsed to a TimeStamp.

i have a class Product that contains a Dictionary with a price curve. The key is a string that can be parsed to a TimeStamp.

public class Product
{
    public virtual int Id { get; set; }
    public virtual IDictionary<string, decimal> PriceList { get; set; }

    public Product()
    {
        this.PriceList = new Dictionary<string, decimal>();
    }
}

Now i need a second class with more prices for each key

public class SpecialProduct : Product
{
    public enum PriceType
    {
        BusineesDays,
        Weekends,
        Holidays
    }

    public virtual IDictionary<string, IDictionary<PriceType, decimal>> PriceList { get; set; }

    public SpecialProduct()
    {
        this.PriceList = new Dictionary<string, IDictionary<PriceTy开发者_开发技巧pe, decimal>>();
    }
}

I am not sure if this is a good approach for this case. I would also like to constrain the enum type to decimal. Any ideas?

UPDATE: I forgot to mention that i need to save all products i a generic List (List)


This is hiding a member.

Polymorphism means changing method, property implementation in some subclass, say it "derived" or "inherited" class. But, anyway, it's signature is immutable.

C# provides a good way of hiding members: the "new" keyword.

You just need to put "new" before access modifier in the derived class and you got it. If you don't do that, C# compiler will suggest you to do that.

By the way, if your goal is using polymorphism, you should take generics in account.

Your base class should have a "TPriceValue" generic parameter and it'll look like this:

public class Product<TPriceValue>
{
    public virtual int Id { get; set; }
    public virtual TPriceValue PriceList { get; set; }

    public Product()
    {
        // NOTE/EDIT: You wouldn't initialize "PriceList" here...
    }
}

So, if you want your price value to be a decimal, you should instantiate your class this way:

new Product<decimal>();

Or, if you want that your value as another dictionary:

new Product<IDictionary<PriceType, decimal>>();

If I'm not wrong, this is the way to go in your case ;)

EDIT: Sorry I forgot to change something in your base class, check again this answer :)


I would go for simplification, since this code has a certain bad smell to it.

Why don't you use the PriceList in SpecialProduct inside Product as well, and make the sub-dictionary have just one value always of PriceType.Default or something like that? You would then be able to use all Products without requiring any casts or checks.


What about this approach?


public abstract class AProductBase<T>
{
  public IDictionary<string, T> PriceList { get; set; }
  ...
}

public class Product : AProductBase<decimal>
{
  ...
}

public enum PriceTypeEnum { ... }

public class SpecialProduct : AProductBase<IDictionary<PriceTypeEnum, decimal>>
{
  ...
}

Perhaps this fits better your requirements.

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消