责任和使用的服务和DAO层 [英] Responsibilities and use of Service and DAO Layers

查看:121
本文介绍了责任和使用的服务和DAO层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发使用的Struts2与Spring插件的Web应用程序和休眠,虽然我一直在寻找在线的例子,我看到使用的服务和DAO层,现在它来到了我什么是真正使用服务和数据访问对象层?如果服务层只调用DAO层的方法来执行CRUD操作。不会是明智的,只是直接调用DAO层的方法呢?

I am currently developing a web application using Struts2 with Spring plugin and hibernate and while I was looking at online examples I saw the use of Service and DAO layers now it came to me what are the real use of Service and data access object layers? If The Service layer is just calling the methods of DAO layers to perform CRUD operations. wouldn't be sensible to just call the DAO layers methods directly?

假设道和业务层的这个例子

Let's say this example of Dao and Service Layer

PeopleService

PeopleService

  @Transactional
    public class PeopleService {

        private PeopleDao pDao;

        public PeopleDao getPDao() { return pDao; }

        public void setPDao(PeopleDao peopleDao) { this.pDao = peopleDao;   }

        public void createPerson(String name){
            pDao.createPerson(name);
        }

        public List<Person> getPeople(){
            return pDao.getPeople();
        }

    }

PeopleDao

PeopleDao

public class PeopleDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session sess() {
        return sessionFactory.getCurrentSession();
    }

    public Person getPersonById(long id) {
        return (Person) sess().load(Person.class, id);
    }

    public void deletePersonById(long id) {
        sess().delete(getPersonById(id));
    }

    public void createPerson(String name) {
        Person p = new Person();
        p.setName(name);
        sess().save(p);
    }

    @SuppressWarnings("unchecked")
    public List<Person> getPeople() {
        return sess().createQuery("from Person").list();
    }

}

我的问题是什么是真正的使用服务层,如果他们只是被他们重新presentative DAO注入,然后调用它的方法是什么?

My question is what is the real use of Service layers if they are only being injected by their representative DAO and then calling its method?

推荐答案

这是有这两层时,你的业务逻辑是比你的数据逻辑更为复杂的是一个好主意。服务层实现业务逻辑。在大多数情况下,该层具有比只调用从DAO对象的方法,来执行多个操作。如果你想使你的应用做大,这可能是最好的解决方案。

It is a good idea to have those two layers when your business logic is more complex than your data logic. The service layer implements the business logic. In most cases, this layer has to perform more operations than just calling a method from a DAO object. And if you're thinking of making your application bigger, this is probably the best solution.

想象一下,你想包括一个城市的实体,创造人与城市之间的关系。下面是一个例子:

Imagine you want to include a City entity and create a relationship between People and City. Here is an example:

@Transactional
public class PeopleService {

    ....
    private PeopleDAO pDAO;
    private CityDAO cDAO;

    ...

    public void createPerson(String name, String city)
     throws PeopleServiceException {
        Person p = new Person();
        p.setName(name);

        City c = cDAO.getCityByName(city);
        if (c == null) throw new ServiceException(city + " doesn't exist!");
        if (c.isFull()) throw new ServiceException(city + " is full!");
        c.addPeople(p);

        sess().save(p);
        sess().save(c);
    }

    ...
}

在这个例子中,可以实现更复杂的验证,如检查数据的一致性。和的PersonDAO未被修改

In this example, you can implement more complex validations, like checking the consistency of the data. And PersonDAO has not been modified.

另外一个例子:

DAO层和业务层使用Spring

服务层图案的定义

这篇关于责任和使用的服务和DAO层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆