java - DDD - how to write good, robust code using a basic example -
for new application, i'm using layers :
restressource -> applicationservice -> domain
i have basic utilize case user can create an"bduget". if no other budget exists current year, status 'initial'. after new budget can created , status 'corrective' coupled attribute 'index' : corrective#1, corrective#2, ...
now coding. applicationservice has methode called "createbudget"
@autowired private budgetrepository budgetrepo; @autowired private budgetservice budgetservice; public budget createbudget(int year) { budget newbudget; if (budgetservice.existsbudgetinitialfor(year)) { newbudget = new budget(year, "initial"); } else { newbudget = new budget(year, "corrective", budgetservice.nextindex()); } budgetrepo.insert(newbudget); homecoming newbudget; }
budgetservice using budgetrepository create database count. existsbudgetinitialfor returns boolean if count > 0 :)
and questions :
for example, find budgetservcie useless. improve move existsbudgetinitialfor within budgetrepository ? do think "if ... else ..." statement @ place : appservice layer responsibility or you, domain rule , enforce this, move code within budgetservice ? to encapsulate budgetservice.nextindex call, improve sue mill creating budget ? (in place of constructor). , think right time compute nextindex. the application layer subjet unit test or perhaps integration test ? why inquire ? when tried unit test methode needed mock @ to the lowest degree budgetservice, budgetrepository, thinking : sign of code smell ? solution ease unit test extractbudgetservice.existsbudgetinitialfor(year)
private method, mock returns true / false wish etc. i didn't think when writing code creation responsibility of entity. found awkard set "if... else ..." logic within budget entity. right ? (please tell me i'm right @ to the lowest degree 1 time :d) and lastly question asking myself : perhaps wrong start. improve if ui send createinitialbudget
command , createcorrectivebudget
command ? in case need check command validity (to avoid multiple initial creation : uniquess validation). it's simple code (i set aside rights management, validation, etc). seek create kind of code right before coding more complexe usecases. main purpose validating layers, explains colleagues, show code can tested, etc.
thanks lot !
françois
your application service seems have business logic. if that's case belongs in domain service. application service called domainbudgetservice.createbudget(int year) have now. can move existsbudgetinitialfor repository if want.
see #1
yes mill preferred.
if move budgetservice method repository don't think issue. unit test domain services , entities.
a mill should take away awkwardness
i have 1 createbudget command , maintain logic internally have it. otherwise ui needs have business logic determine kind of command send.
hope helps.
java design-patterns architecture domain-driven-design
No comments:
Post a Comment