spring - Can Controller layer call directly to Persistence layer? -
i've webapp jsf, spring , mybatis. frameworks utlized in controller, business , dao layers respectively. in dao layer i've methods crud operations. now, in controller layer need use insert operation. use configuration:
controller layer
i'm using jsf annotations
@managedbean public class controller{ @managedproperty("#{business}") private business business; public void insert(){ business.insert(); } }
business layer
i'm using spring annotations
public interface business{ public void insert(); } @service("business") public class businessimpl implements business{ @autowired private daomapper mapper; @override @transactional public void insert(){ mapper.insert(); } }
dao layer
i'm using mybatis (mybatis-spring library)
public interface daomapper{ public void insert(); }
but in case business layer call dao layer , don't realize other operation, think use configuration:
controller layer
@managedbean public class controller{ @managedproperty("#{daomapper}") private daomapper mapper; public void insert(){ mapper.insert(); } }
dao layer
public interface daomapper{ @transactional public void insert(); }
i've tested , works fine know if incurring bad practice
[edit]
actually daomapper interface mybatis class mapper asociated xml file (look). don't know if makes dao class. think should call persisence layer or mapper layer
source: http://www.infoq.com/articles/ddd-in-practice
as can see. presentation layer, in case controller layer (i think made mistake naming), invoke directly dto (or that's understand )
if in controller or presentation layer (whatever call it) need use insert or update operation, invoke directly mapper class or have create business class for freshly class calls mapper class
now. if possible use configuration have doubt:
suppose in method of business class need invoke insert operation. like:
@service("business") public class businessimpl implements business{ @autowired private daomapper mapper; @override @transactional public void insert(){ mapper.insert(); } }
but method insert daomapper interface has @transactional annotation. method insert being affected twice @transactional annotation. not problem?
i suggest not using dao in controller directly. in opinion. dao layer mapping database, if want change database(eg. sql nosql) , thing create new dao , injected, leave controller&service functions not changed @ all. controller, main responsibility handle request , response, work should done business/service layer. if it's wrong appreciate point
Comments
Post a Comment