java - Service layer and controller: who takes care of what? -


in class we're learning how build spring application, though spring isn't directly involved, learned how make interfaces dao , service layer objects.

please correct me if i'm wrong: dao layer pretty abstract: contains crud operations , further used read data.(ie: objects, specific objects, etc)

service layer: contains services create things, , delete things, business logic should be.

now of makes sense in service layer; except "updating" objects. put "update" function saves object in database? or need define logic there well? confusion as, understanding objects in spring pojo's. validates data?

let's have object "child" has:name, surname, gender, photo, birthdate fields. how name services? or let controller take care of validation, doesn't seem right me. on other hand wouldn't seem right either delegate every setter needs called service layer.

so basically: me how define saving objects via service layer.

if wish have controllers able persist changes child object, traditionally have method in service named childservice.update(child newchild), handle calling correct daos persist new version of child.

controllers free ask service child, change fields around (conceivably based on user input) - sane design have controller doing work child pojo , asking service persist change. model pojo should know nothing controller, service, or dao hold data suggest - not want every call setname() or setgender() automatically result in database update.

instead, controller and/or service should acquire child object, whatever work needs object in it's unit of work, , ask service (and dao) persist changes.

validation can take place in several layers - controller might want validate input web user, , service may want validate has valid child object before persists it. makes sense have level of validation in both layers in case want re-use service in other capacities - such exposing rest interface, different front-end, etc.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -