Populate base class property by calling method to populate base class only once?

I have 3 classes below:

public class Department
{
    public string Prop1 { get; set; }
    public string Prop2 { set; get; }
}

public class DeptCode100 : Department
{
    public string Prop3 { get; set; }
    public string Prop4 { set; get; }
}

public class DeptCode200 : Department
{
    public string Prop5 { get; set; }
    public string Prop6 { set; get; }
}

public class Employee
{
  public void Process()
  {
   foreach (var employee in _employees)
   {
      if (employee.deptCode == 100) // 100
      {
         var deptCode100 = new DeptCode100();
         InjectDepartment(deptCode100,employee);
      }
      else if (employee.deptCode == 200)//200
      {
         var deptCode200 = new DeptCode200();
         InjectDepartment(deptCode200,employee);
      }
   }
}
 protected void InjectDepartment(Department dept,Employee emp)
 {
     dept.Prop1 = emp.Code;
     dept.Prop2 = emp.Basic;
 }
}

      

Now what I want to do is I want to call this InjectDepartment method only once instead of calling twice (because later when I have 3-4 subclasses I would have to call this method 4 times depending on the number of subclasses) and pass another subclass based on condition.

+3


source to share


2 answers


If at all possible, just initialize objects using constructors, which means that they are fully formed the moment they are available for code invocation; and makes it impossible for initialization to happen twice on any given object:

public class Department
{
    public string Prop1 { get; set; }
    public string Prop2 { set; get; }

    public Department(string prop1, string prop2)
    {
        Prop1 = prop1;
        Prop2 = prop2;
    }
}

public class DeptCode100 : Department
{
    public string Prop3 { get; set; }
    public string Prop4 { set; get; }

    public DeptCode100(string prop1, string prop2, string prop3, string prop4) : base(prop1, prop2)
    {
        Prop3 = prop3;
        Prop4 = prop4;
    }
}

public class DeptCode200 : Department
{
    public string Prop5 { get; set; }
    public string Prop6 { set; get; }

    public DeptCode200(string prop1, string prop2, string prop5, string prop6) : base(prop1, prop2)
    {
        Prop5 = prop5;
        Prop6 = prop6;
    }
}

      

You don't need InjectDepartment

, because you have to create Department

when you have everything you need to create it:



// ...
if (employee.deptCode == 100)
{
    var dept1 = new Department100(employee.Code, employee.Basic, ...);
}

      

Unless you really know and cannot know the meanings for Prop3

etc. when building Department

, leave them outside the subclass constructors and fill them in later.

+2


source


I'm not sure if this is what you want, but I will try:



protected void InjectDetpartment(Department dept,Employee emp)
 {
     if(dept is DeptCode100 dc1)
     {
         dc1.Prop3 = emp.Code;
         dc1.Prop4 = emp.Basic;
     }
     else if(dept is DeptCode200 dc2)
     {
         dc2.Prop5 = emp.Code;
         dc2.Prop6 = emp.Basic;
     }
     else
     {
         dept.Prop1 = emp.Code;
         dept.Prop2 = emp.Basic;
     }
 }

      

+2


source







All Articles