Better option to "where to declare actions?(oop)" -
doubt where define actions going act on attributes of 1 class?
ex:-account class going define attributes of account account no,holder name.
usual employee(class) of bank can add,update(crud) operation on customer's account. doubt in class have define (crud)actions either in employee class or in account class?.
because these operations performed employee in real time.on other hand,actions need act on account attributes may define in account class itself,which better one?
'actions need performed on attributes ,defined in same class better' or 'actions defined in class respect actions they(actors) better'?
in understanding of problem describe, such "operations" go in service layer.
you can see in link shared above martin fowler describes service layer as:
defines application's boundary layer of services establishes set of available operations , coordinates application's response in each operation.
so, in view of solution, both employee , account pojos @rotka said. in view interfaces layers like
at data source layer:
interface accountrepository { account findbyid(long id); account save(account account); }
and doubt can delete bank account, put state of inactivity though.
than service layer like
interface accountservice { account openaccount(account account); void closeaccount(account account); transferfunds(account source, account target); ... }
what call employee wrong abstraction use here. think in terms of users, current user allowed open accounts?
so, consider following hypothetical implementation
class defaultaccountservice implements accountservice { private securitycontext securitycontext; private validator validator; private accountrespotiroy accountrepository; @override public account openaccount(account account) { if(!securitycontext.getuser().haspermission("openaccount")){ throw new unauthorizedexception("you cannot open accounts"); } set<constraintviolation> violations = validator.validate(account); if(violations.size() > 0){ throw new badrequestexception("invalid account", violations); } return accountrepository.save(account); } ... }
Comments
Post a Comment