1

There are three templates:

BaseProduct - Title, Price  

SpecificProduct - Color, Weight
[SpecificProduct inherits BaseProduct]

SpecificProductPage - PageTitle
[SpecificProductPage inherits SpecificProduct]

And their respective classes:

public class BaseProduct : CustomItem
{
   public BaseProduct(Item innerItem) : base(innerItem) { }
   public string Title
        {
            get
            {
                return InnerItem.Fields[Templates.BaseProduct.Fields.Title].Value;
            }
        }
    ....

}

public class SpecificProduct : CustomItem
{
   public SpecificProduct(Item innerItem) : base(innerItem) { }
   public string Color
        {
            get
            {
                return InnerItem.Fields[Templates.SpecificProduct.Fields.Color].Value;
            }
        }
    ....
}

At the end I would like to get all the properties for the SpecificProductPage. Example:

   SpecificProduct product = new SpecificProduct(Sitecore.Context.Item); //page item

or maybe

AnyOtherClass product = new AnyOtherClass(Sitecore.Context.Item);

I could inherit the BaseProduct class to the SpecificProduct class, but what if SpecificProduct has to inherit some other class also later.
I never had such a case before. As both these classes need to inherit CustomItem, I'm confused how to use interface or even where to start. The reason to post this question here than SO is, I felt, SSE people will quickly understand seeing CustomItem and hopefully get a better resolution.

0

2 Answers 2

2

So if you want to match exactly your template inheritance, then your SpecificProduct should derive from BaseProduct as you have it already.

There are a couple of principles you should follow here tho:

YAGNI - do you need to do that now? No, then don't worry about it, move on to the next problem. Worry about that when you need to and you can refactor then.

Interfaces, using interfaces your class can implement multiple interfaces:

public class SpecificProduct : BaseProduct, IAnotherBaseProductThing
{
}

Now you will have to provide the implementation for that, but it will still work.

Finally: Be careful about multiple inheritance in Sitecore. Only do it where it really makes sense. E.g - you could go crazy and have a template called _body that has a single rich text field on it and then any template where you want a body field could inherit from that - reuse right? well to a point, but it quickly gets complicated, what happens when you need some different validation rules on that field etc... Sometimes its better just to add the fields to the template you want to add them too and not go so crazy with inheritance. Its a balance.

0

Best is use multiple inheritance support in C# using Interfaces. i.e. in your case say your "CustomItem" class can implement multiple "Interfaces" depending on multiple "Templates".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.