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

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

问题描述

我目前正在使用带有 Spring 插件和 hibernate 的 Struts2 开发 Web 应用程序,当我查看在线示例时,我看到了 Service 和 DAO 层的使用,现在我想到了 Service 和数据访问对象的真正用途层?如果Service层只是调用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?

比如说这个 Dao 和 Service Layer 的例子

Let's say this example of Dao and Service Layer

人员服务

  @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();
        }

    }

人道

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();
    }

}

我的问题是,如果服务层仅由其代表 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.

假设您想要包含一个 City 实体并在 People 和 City 之间创建关系.下面是一个例子:

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.

另一个例子:

使用 Spring 的 DAO 和服务层

服务层模式定义

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

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