Home > Software design >  How to make new model with some fields of other models by inheritance
How to make new model with some fields of other models by inheritance

Time:09-29

<I have two classes with too many fields . which are basically the Model of MVC . I want to create a third class which take few fields from both 1st,2nd by inheritance but not all the fields of 1st and 2nd class is it possible by inheritance .I want to make a new class without declare field in it

 public class OpeningBalanceLiteModel
    {
        public Int64 LedgerId { get; set; }
        public String LedgerCode { get; set; }
        public Int64? ParentLedgerId { get; set; }
   
        public Decimal OpeningBalance { get; set; }
        public Boolean? Status { get; set; }
        public Int32? LedgerCategoryId { get; set; }
        
        public Byte Is_Active { get; set; }
        public Byte Is_Deleted { get; set; }
    }
    public class DrCrDetailLiteModel
    {
        public Int32 Id { get; set; }
        public Int32 TransactionTypeId { get; set; }

        public Int32? AmountType { get; set; }
        public Decimal? Amount { get; set; }
        public String VoucherNarration { get; set; }
     
        public Int32 FinancialYearId { get; set; }
        public Int64 CompanyId { get; set; }
        public Decimal? SubsidiaryId { get; set; }
        public Decimal? LocationBranchId { get; set; }
        public Decimal? DivisionId { get; set; }
        public Int32? DepartmentId { get; set; }
        public Int32? ProjectId { get; set; }
        public Int32? ProjectEstimationId { get; set; }
       
        public Boolean? IsAdvancePayment { get; set; }
        public Boolean? IsHiddenFromSourceLedger { get; set; }
        public Boolean? IsReversed { get; set; }
    }
    public class OpeningBalanceDrCrMurg
    {

    }```


* i wan to add 3 fields of both class by inheritance 

CodePudding user response:

We commonly calls inheritance a "is a" relation ship, an apple is a fruit. If you do not include all properties it is not a "is a" relationship. Is all fruits have a color, then an apple must have a color.

Moreover, c# does not support multiple inheritance. You could inherit from multiple interfaces, and that would also allow you to use explicit interface implementation to hide properties unless a reference of the interface type is used. But I get the impression that your goal is implementation inheritance.

My recommendation would be to use Composition instead of inheritance. Group your properties in to logical groups, and compose these models, for example:

public class LedgerModel{
        public Int64 LedgerId { get; set; }
        public String LedgerCode { get; set; }
        public Int64? ParentLedgerId { get; set; }
}
public class OpeningBalanceLiteModel{
     LedgerModel {get;set;}
    ...
}

That should allow finer grained control to allow you to include any combination of property-groups in your classes.

CodePudding user response:

you should create a Base Class ex:

public class BaseClass
{
    public int Id1 { get; set; }
    public int Id2 { get; set; }
    public int Id3 { get; set; }

}





 public class childrenClass1 :BaseClass
    {
        ...

    }



  public class childrenClass2:BaseClass
    {
        ...

    }

Now children class1 and 2 have Id1/Id2/Id3

  • Related