i'm trying optimize algorithm makes use of list of coordinates. following domain driven design principles - coordinate defined class in separate valueobjects project used main project. coordinate class consists of 2 doubles (latitude , longitude) , validation in constructor ensure valid coordinates when writing.
using visual studio's profiling tools have found large amount of processing ends in get_longitude , get_latitude functions. might due huge number of calls made.
would worth storing coordinates 2 doubles instead of object contained 2 doubles properties? or decrease readability no performance improvement?
since coordinate value object, immutable. thus, using 2 readonly public fields acceptable (but read rest).
moreover using struct further improve computation performances (as jon skeet pointed out in comments).
from ddd perspective these implementation details fine.
moreover:
- for validation, use static factory methods in
coordinatedefinition - for computation, code closure of operations right in
coordinatedefinition (but avoiding operator overloading, if performance matters)
these 2 ddd patterns prove in context.
having factory methods (and proper semantics default value) in struct, enable remove validation logic in constructors, since invariants ensured factory methods (actually not have public constructor except default one).
moreover, supposing coordinates form additive group, struct expose instance method coordinate add(coordinate other). in implementation not need validation, computation (based on access of other's private fields) , initialization since know current instance valid , argument.
note if go far enough closure of operations won't need public field or property.
indeed, if care oop design, instead of exposing internal info (property or field, doesn't change much) expose couple of methods following without violating encapsulation
public void eval(action<double, double> action) { if(null == action) throw new argumentnullexception("action"); action(_lat, _long); } public t eval<t>(func<double, double, t> function) { if (null == function) throw new argumentnullexception("function"); return function(_lat, _long); } furthermore, if need such methods, don't belong ubiquitous language, hide them behind explicit implementation of infrastructural interface.
Comments
Post a Comment