web services - DDD with webservices DTO as value object: how to handle them? -


i have "myunits" application let's manage units (like meters, kilograms, pounds, miles, km/h...). model complex, supports unit compatibilities, operations, conversions, etc.

i have application (myapp) need use "units", want make use "units" application.

what thought have "units" service (webservice) unitservice consumes , returns unit dto unitdto.

in myapp, have model:

operand     value: float     unit: unitdto operationadd     operand1: operand     operand2: operand     execute() 

the problem: in operationadd.execute(), need check units compatibles (for example).

so either:

  • unitdto has method call unitservice::arecompatible, wrong! how dto (that should contain data) knows unitservice webservice! shouldn't

  • operationadd.execute() calls unitservice::arecompatible, wrong! how operationadd (an entity) knows unitservice webservice! shouldn't

  • or have operationservice work (and can call services) but operation entities data containers, entities no methods, , that's not ddd about

i don't want anemic entities, in case have entity uses service, how can do?

and: wrong thinking unitdto can used vo?

an unit should "advertise" compatibilities. don't know if language you're using supports generics way.

first of all, unitdto contains state unit. use unidto create concrete unit (which btw vo). each unit should know compatibilities.unitdto should dto , create other vo work.

c#

public class unitbase {     public virtual bool iscompatible(unitbase unit)     {         return false;     } }  public interface icompatible<t> {      bool iscompatible(t unit); } public class unitfeet {} public class unitmeter:unitbase,icompatible<unitfeet> {     bool iscompatible(unitfeet unit) { return true;}  }  public override bool iscompatible(unitbase unit)  {     return iscompatible((unitfeet)unit);   }  

the compiler should chose right overload depending on compared unit. icompatbile interface can have conversion methods 1 unit another. let's suppose want things more abstract

public class operandvalue:icompatbile<operandvalue> {       public decimal value {get;set;}      public unitbase unit {get;set;}      public bool iscompatbile(operandvalue other)      {         return unit.iscompatbile(other.unit);      }       public static operandvalue fromdto(operand data)       {          return new operandvalue(data.value,unitbase.fromdto(data.unit));        } }   operandvalue first=operandvalue.fromdto(operand1);  operandvalue second=operandvalue.fromdto(operand2);  if (first.iscompatbile(second)){     operationservice.add(first,second)   } 

so don't need unitservice::arecompatbile method, lot of polymorphism , careful object design.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -