c# - Rich Domain Model Implementation -


i started reading rich domain model instead of anemic models. projects worked on before, followed service pattern. in new new project i'm trying implement rich domain model. 1 of issues i'm running trying decide behavior goes in (in class). consider example -

public class order {     int orderid;    string ordername;     list<items> orderitems; }  public class item {    int orderid;    int itemid;    string itemname;  } 

so in example, have additem method in item class. before add item order, need make sure valid order id passed in. validation in additem method. on right track this? or need create validation in order class tells if orderid valid?

wouldn't order have additem method? item added order, not other way around.

public class order {     int orderid;    string ordername;    list<items> orderitems;    bool additem(item item)    {      //add item list    } } 

in case, order valid, because has been created. of course, order doesn't know item valid, there persists potential validation issue. validation added in additem method.

public class order {     int orderid;    string ordername;    list<items> orderitems;    public bool additem(item item)    {      //if valid      if(isvalid(item))      {          //add item list      }     }    public bool isvalid(item item)   {      //validate   }  } 

all of in line original oop concept of keeping data , behaviors in class. however, how validation performed? have make database call? check inventory levels or other things outside boundary of class? if so, pretty order class bloated code not related order, check validity of item, call external resources, etc. not oopy, , not solid.

in end, depends. behaviors' needs contained within class? how complex behaviors? can used elsewhere? needed in limited part of object's life-cycle? can tested? in cases makes more sense extract behaviors classes more focused.

so, build out richer classes, make them work , write appropriate tests see how , smell , decide if meet objectives, can extended , maintained, or if need refactored.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -