c# - Another application architecture question -
i trying ddd , try describe in simple manner have done.
core project
core project contains entities, vo , domain services. example have user entity , userrelation entity. think know user entity. userrelation contains information how users connected each other follow , connect(bidirectional follow). because of have userdomainservice stateless methods follow(user,user) , connect(user, user).
public class user { public string name { get; set; } public string username { get; set; } public void changeusername(stirng newusername) { applyevent(new userusernamechanged(newusername)); } } public class userrelation { ctor(user1, user2, isbidirectional) public user user1 { get; set; } public user user2 { get; set; } public bool isbidirectional { get; set; } } public class userdomainservice { public userrelation follow(user user1, user user2) { return new userrelation(user1,user2,false); } }
repository project
i using nhibernate decided not create such project. instead using nhibernate directly. example, in ui user object db, change , call session.save(user).
the problem
if want operation follow this:
- get info db
- call follow(user1, user2) service
- save userrelation object db in end application code becomes bit complex. imagine if want use code in 2-3 places , @ point have refactor it.
my solution
solution create applicationservice project dirty work , force consumer use applicationservice instead of using entitties , domain services directly
public class userapplicationservice { ctor(userdomainservice userdomainservice){} user getuser(guid id) { return nhibernatesession.get(id) } public void follow(guid user1id, guid user2id) { var u1 = getuser(user1id); var u2 = getuser(user2id); var userrelation = _userdomainservice.follow(u1,u2); nhibernatesession.save(userrelation); } public void changeusername(guid user, string newusername) { user.changeusername(newusername); nhibernatesession.save(user); } }
is application service or bad? can see new service act repository can create userrepository class lets skip now. bothers me parameters in 2 methods. accept guids , service retrieves users db. other option pass directly user object. changeusername method 1 user entity + persistence.
well, think this?
i don't see wrong, in fact how company work has done it, long abstract out data access service part of business layer mediator between data layer , ui layer, saves repeating , making sure rules outlined operation fires.
Comments
Post a Comment