java - Spring service class contains too many finder methods -
i using spring framework , spring data jpa develop application. below 1 of repository interface , service class.
public interface userrepository extends jparepository<user, long> user findbyname(string name); user findbyemail(string email); } public class defaultuserservice implements userservice { @inject protected userrepository userrepo; @override public user getuserbyid(long id) { return userrepo.findone(id); } @override public user getuserbyname(string name) { return userrepo.findbyname(name); } @override public user getuserbyemail(string email) { return userrepo.findbyemail(email); } }
as stated many experts, service layer design should coarse grained , focused on application operations. looking @ service class above, believe not design directly expose finder methods repository. since 3 service methods above returning same object type (user), want expose 1 finder method instead of 3 able encapsulate finder logic.
public class defaultuserservice implements userservice { @inject protected userrepository userrepo; // arguments , logic method. @override public user getuser() { } }
i appreciate if can point me solution on how solve design issue?
i think design not bad, mean seeing kind of approach several times, in fact have several finder methods each 1 use different property obtain user, if want make service method encapsulate logic retrieve user suggest this.
public class defaultuserservice implements userservice { @inject protected userrepository userrepo; enum userfindenum{ id, email, name; } public user getuser(userfindenum e, object obj){ switch(e.ordinal()){ case 0: return userrepo.findone(obj); case 1: return userrepo.findbyname(obj); case 2: return userrepo.findbyemail(obj); default: break; } } }
i mean need know property use find user @ least 1 parameter need sent service layer getuser()
not enough. using kind of logic above have 1 service method , needed logic within it.
Comments
Post a Comment