-1

I have a base class for all the ENTITIES of my project which is inheriting from below model :

public class BaseModel
    {
        public int Id { get; set; }
        public int CreatedDate { get; set; }
        public override string ToString();
    }

Now I have 1 another functionality which is common for so many modules and I want to keep BaseModel for that functionality and want it to be inherited from it.

Public class BaseNotice
{
    // Common info related to notice which is use to send notice to employees in different scenarios
}

Now our every model is suppose to inherit from BaseModel so inheriting from BaseNotice will be multiple inheritance.

Now I cannot like below :

Public class BaseNotice : BaseModel
{
    // Common info related to notice which is use to send notice to employees in different scenarios
}

Because I would like to control functionality related to Notice from BaseNotice model and for notice I would like to keep BaseNotice as base model.

But I am not getting how to avoid multiple inheritance here and so what would be the proper way to design this?

xyzWty
  • 133
  • 1
  • 14
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • What's wrong with multiple inheritence? Notice : BaseNotice, and BaseNotice : BaseModel... seems reasonable to me. – Skintkingle Oct 26 '18 at 11:16
  • 3
    For things like this I would prefer an interface over inheritance because you are offering no shared behavior, only simple properties. Use `IBaseModel` and `IBaseNotice` and apply these interfaces where you need them. – Igor Oct 26 '18 at 11:19
  • 1
    Why do you think this involves multiple inheritance (which, strictly speaking, C# doesn't actually allow)? You say "every model is supposed to inherit from `BaseModel`", but if it inherits from `BaseNotice` then it automatically also inherits from `BaseModel` - how is this a problem? – Dylan Nicholson Oct 26 '18 at 11:37
  • @Igor But using interface just for properties is good and recommended?because what i have seen uptill now is interface is mostly use when you want to force derive class to implement contracts – I Love Stackoverflow Oct 26 '18 at 11:42
  • @DylanNicholson Actually i am talking about some situations where i need only BaseNotice in which i dont want BaseModel. – I Love Stackoverflow Oct 26 '18 at 11:44
  • Ah sorry, misread your post. If a "BaseNotice" isn't a logically type of "BaseModel", then what sort of objects would be both BaseNotices AND BaseModels? If they just need to have the ability to expose the same properties as a BaseNotice, containment (inheriting from BaseModel and containing a reference to a BaseNotice) might work better. – Dylan Nicholson Oct 26 '18 at 19:48

1 Answers1

0

There is No need to Multiple Inheritance. you can do that in this way:

public class BaseModel
{
    public int Id { get; set; }
    public int CreatedDate { get; set; }
    public override string ToString();
}

public interface IBaseNotice
{
   // Base Notices Contracts should be placed here
}

Public class BaseNotice: IBaseNotice
{
    // Common info related to notice which is use to send notice to employees in different scenarios
}


public class ModelX:BaseModel
{
    public IBaseNotice Notice { get ; set; } 
    public ModelX(IBaseNotice baseNotice) 
    {
        Notice = baseNotice;
    }
}

Or you can use Second Generation of your BaseModel:

public class BaseModeNoticable:BaseModel
{
    public IBaseNotice Notice { get ; set; } 
    public BaseModeNoticable(IBaseNotice baseNotice) 
    {
        Notice = baseNotice;
    }
}
RezaNoei
  • 1,266
  • 1
  • 8
  • 24