I've webapp with JSF, Spring and Mybatis. Those frameworks are utlized in controller, business and dao layers respectively. In my DAO layer I've methods for CRUD operations. Now, in my controller layer I need use insert operation. For this I could use this configuration:
Controller Layer
I'm using JSF with annotations
@ManagedBean
public class Controller{
@ManagedProperty("#{business}")
private Business business;
public void insert(){
business.insert();
}
}
Business Layer
I'm using Spring with 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 as in this case my business layer only call to DAO layer and don't realize any other operation, I would think to use this 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 already tested and works fine but I would like to know if I am incurring a bad practice
[EDIT]
Actually DaoMapper interface is a MyBatis class mapper that is asociated to XML file (Look). I don't know if this makes a DAO class. I think I should actually call it Persisence Layer or Mapper Layer
Source: http://www.infoq.com/articles/ddd-in-practice
As you can see. Presentation layer, In my case Controller Layer (I think I made another mistake naming), invoke directly to DTO (Or that's what I understand )
If in my controller or presentation layer (Whatever you call it) I need use insert or update operation, I could invoke directly from mapper class or I necessarily have to create a business class for for freshly from this class calls to mapper class
Now. If is possible use this configuration I have a doubt:
Suppose in a method of my business class I need to invoke to insert operation. This would be like:
@Service("business")
public class BusinessImpl implements Business{
@Autowired
private DaoMapper mapper;
@Override
@Transactional
public void insert(){
mapper.insert();
}
}
But method insert from DaoMapper interface already has @Transactional annotation. Method insert would be being affected twice by @Transactional annotation. Would not this be a problem?
I suggest not using DAO in controller directly. In my opinion. Dao layer is the mapping from database, like if you want to change another database(eg. from sql to nosql) , the only thing is only create a new DAO and injected, but leave controller&Service functions not changed at all. So is the controller, its main responsibility is to handle the request and response, the work should be done by business/service layer. If it's wrong appreciate to point