Alternatives to repository access from domain (DDD) -
i've seen number of these questions, seem mismatch thoughts. think due sloppy understanding of relationship between aggregates-aggregate roots-entities-value objects.
the finished solutions looks dtos me, since (all logic) seems end inside repositories. perhaps i've been looking @ ef tutorials much.
assuming have extremely simple class, first version of repo (let's ignore handles single person):
class person { int age; void makeolder() { age++; } } interface ipersonrepository { person getaperson(); }
now, ui (say, @ press of button), not:
person.makeolder(); _repo.save(person);
but perhaps just:
person.makeolder();
for me, action of "makeolder" should trigger save. of course require reference repo in person. options this, can think of, is:
_repo.makepersonolder(person);
(seems awful.)
person.makeolder(_repo);
(no gain ui pov, i'm looking @ now)
class person : imyentitybasetype { ... void makeolder() { age++; entitydatawaschangednowiwanttobesaved(); } }
or variant of this; events, aop, etc. somehow signalling or capturing save should done.
i think might mixing view of ddd event sourcing , similar concepts.
am wrong in thinking saveperson
call in ui code feels dirty?
what "right" thing do?
you shouldn't pass repository person. model, you'd have pass repository every method. typically, if using ef or other orm, there built-in concept of unit of work. unit of work keeps track of objects have changed within defined interaction. when commit unit of work, commits changes within it. code more like:
person.makeolder(); _unitofwork.commit();
this approach can used when there changes multiple entities. since orm keeps track of changes, don't have explicitly save each object part of unit of work.
a better option, addresses concern, encapsulate use case application service. application service have method such as:
public void makeolder(int personid) { var person = this.personrepository.get(personid); person.makeolder(); this.unitofwork.commit(); }
the ui call application service instead of domain object directly.
Comments
Post a Comment