model view controller - MVC (Spring): Logic Organisation Entity and Service -


i have simple question code organisation.

  • teamrepository (repository class)
  • teamservice (service class)
  • team (entity class)

i want set new team leader team object. should function "setleader(user user)" in entity class or better in teamservice class?

team.setleader(user); teamservice.save(team); 

or

teamservice.setleader(team, user); 

inside "setleader" function there if condition (if user team member) declare user new team leader.

i guess it's ok in entity class or? confused because have read "business logic" should not in entity classes.

best regards

kindly keep in mind that:

entity class:

in designed architecture, should pojo class, containing simple attributes, default constructor, getter() & setters() , other optional methods tostring() & equal() etc. example:

@entity public class team{     private string id;     private user leader;      public team(){}     //getters & setters } 

repository class:

this main purpose of class should either save, update, delete or retrieve simple record database, no business logic.

service class:

this class can specify business/ domain related logic here. should populate required entity object, , call specific repository class save given entity.

@service public class teamservice{     @autowired     teamrepository repo;      public void createteam(){        team team = new team();        if(//user member of team){           team.setleader(user);//assume user object initialized        }        repo.save(user); } } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -