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:
unitdtohas method callunitservice::arecompatible, wrong! how dto (that should contain data) knows unitservice webservice! shouldn'toperationadd.execute()callsunitservice::arecompatible, wrong! how operationadd (an entity) knows unitservice webservice! shouldn'tor have
operationservicework (and can call services) butoperationentities 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
Post a Comment