design patterns - Domain objects encapsulation: static methods vs Service classes -
i've read in ddd book (eric evans) procedures require used in presentation should moved services classes. instance bankaccountmanagementservice has changebankaccount, getbyaccountid ... methods.
however need encapsulate setters of properties forbid assigning them other business objects. c# doesn't have friendly classes impossible use type of encapsulation in case of services. possible using static methods of bankaccount business object.
(1) how solve limitation in case of using services mentioned above reason?
edit: additional question
(2) why bad use static method instead of services? can place them in separate partial class file not mix proc code entity code.
thanks in advance :)
if properties of domain object should not set (immutable), make them private (or protected).
the service method responsible altering private properties of domain object perform necessary validation , or permissions checks , create new object via 1 of constructors (including id) properties wants change , save object.
another option put set method on domain object takes new value, , kind of permission object, or attribute method require privileges. way can constrain set called from.
edit: making things static architectural black hole: cant inherit them or alter them in way. makes impossible use dependency injection. versioning harder; once have made static , used, hard reverse decision. also, static method may not use instance data today, may need in future.
when methods instance methods, can make use of polymorphism , generics, creating generic servicebase class , putting commonly used methods there.
Comments
Post a Comment