c# - 3-layer architecture - passing data between layers -


trying implement 3-layer (not: tier, want separate project logically, on 1 machine) architecture i've found many different approaches i'm confused, what's best way (if there's any) make in winforms app.

now have no doubts 3 layers should present in project:

  • ui (presentation layer)
  • bll (business logic layer)
  • dal (data acces layer)

in ui put winforms. there must logic fill object data controls , pass bll layer.

in dal want put classes , methods data manipulations using ado.net, like:

public class orderdal {     public orderdal()     {     }      public int add(order order)     {         //...add order database     }      public int update(order order)     {         //...update order in database     }      //...etc. } 

the problem bll , question - should use data transfer objects pass data between layers, or should pass whole class?

if choose use dto, i've create additional common class, order, reference ui, bll , dal:

public class order {     public int id { get; set; }     public datetime date { get; set; }     public string number { get; set; }     public string customername { get; set; }      public order ()     {     } } 

and put logic separated bll:

public class orderbll {     public orderbll()     {     }      public int add(order order)     {         orderdal orderdal = new orderdal();         return orderdal.add(order);     }      public int update(order order)     {         orderdal orderdal = new orderdal();         return orderdal.update(order);     }      //...etc. } 

this approach, under different names, used among others: here or here.
on other hand, "wise guys" , followers (like here) call anemic domain model , complain it's bad design , anti-pattern should not used.

the pros:

  • dto can design represent database table,
  • it's light , clear, contains fields needed database,
  • dal doesn't have reference bll,

the cons:

  • anti-pattern (sounds scary ;p),
  • violation of oop (separated properties methods),
  • because logic in different class, may more difficult maintain when changes.

so, opposite approach pass whole object between layers, here: no dto, bll looking that:

public class order {     public int id { get; set; }     public datetime date { get; set; }     public string number { get; set; }     public string customername { get; set; }      public order()     {     }      public int add()     {         orderdal orderdal = new orderdal();         return orderdal.add(this);     }      public int update(order order)     {         orderdal orderdal = new orderdal();         return orderdal.update(order);     } } 

the pros:

  • it's nicely encapsulated object, following oop rules (i suppose ;)).
  • both logic , properties in 1 place, easier maintain , debug.

the cons:

  • to use object, dal has reference bll (that's not how 3-tier layer should do, isn't it?).
  • class may contain fields not used in database, fields database (like id) not represent "real life" object.

so, it looks whatever choose, i'll violate rules. what's better way then, should choose? maybe there other approach haven't found?

i don't dtos, because mean creating dual hierarchy little or no value.

i don't idea of making model objects responsible own persistence. prefer separate persistence layer. why? model objects don't need persisted useful. business logic , functionality orthogonal persistence.

if have 2 layers it's possible keep 1 way dependency graph: persistence knows model, model not know persistence. end cyclic dependency if model objects responsible persistence. can never test or use model objects without persistence.

my advice? don't dtos. break out separate persistence layer.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -