c# - Accepting multiple similar entities in a Method - Elegant solution -


i have 2 data entities, similar, design like:

public class entity1 : base {   public int layerid;    public list<int> groups; } 

difference entity1 has collection of integer groups

public class entity2 : base {   public int layerid; } 

these entities filled input ui using json, need pass them processing method, gives same output entity. method has logic handle if list<int> groups null, need create method capable of handling each of input in elegant manner. cannot use entity1, since 2 different functional inputs different business process, using entity1 direct replacement mis-representation

instead of creating overload of function, can think of following options:

  1. use object type input , typecast in function internally

  2. i think can use dynamic types, solution similar above, not clean solution in either case, along switch-case mess.

what doing processing method this:

public ouputentity processmethod(entity 1) {   // data processing } 

i have created constructor of entity1, takes entity2 input.

any suggestion create elegant solution, can have multiple such entities. may using generic, use func delegate create common type out of 2 or more entities, similar have done. like:

func<t,entity1> 

thus use entity1 output further processing in logic.

i need create method capable of handling each of input in elegant manner

create interface, or contract speak, each entity adheres particular design. way common functionality can processed in similar manner. subsequently each difference expressed in other interfaces , testing interface sis done , differences handled such.

may using generic,

generic types can tested against interfaces , clean method of operations hence follows suit.

for example have 2 entities both have name properties string, 1 has order property. define common interface

public interface iname {    string name { get; set; }    string fullname { get;  } }  public interface iorder {    decimal amount { get; set; } } 

so once have our 2 entities of entityname , entityorder can add interfaces them, using partial class definition such when ef creates them on fly:

public partial class entityname : iname {   // nothing entityname defines public string name { get; set; }    public string fullname { { return "person: " +  name; }} }   public partial class entityorder : iname, iorder {   // nothing entity order defines public string name { get; set; }   // , amount.    public string fullname { { return "order: " + name; } } } 

then can process each of them in same method

public void process(iname entity) {      logoperation( entity.fullname );     // if have order process uniquely    var order = entity iorder;     if (order != null)    {       logoperation( "order: " + order.amount.tostring() );    }  } 

generic methods can enforce interface(s) such as:

public void process<t>(t entity) t : iname {    // same before ensured elements of iname     // used enforced compiler. } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -