1

我的问题是:您的服务层是否与您使用的技术绑定?

例如,如果您使用休眠,则将一些仅作为休眠功能的 hql 查询或条件查询放入服务层,或者您只需调用 DAO(并且 dao 具有休眠实现,可能还有 jdbc 实现等)?

我在为我的软件构建有效的分层架构时遇到了一些麻烦。

编辑 这是一项简单的服务……我认为这是一项服务……不受我使用的技术的约束(休眠)

@Repository
public class PersonHibernateDAO implements PersonDAO {

    @Autowired
    SessionFactory sessionFactory;

    ... dao crud operations(implementation of PersonDAO interface) using sessionfactory ...

    //and some hibernate features methods
    public Person findByCriteria(Criterion criterion){
        // code
    }
}

@Service
public class PersonService {

    @Autowired
    private PersonDAO personDao;

    @Autowired
    private AccessDAO accessDao;

    @Transactional
    public boolean hasPermission(String username, String accessCode){
        Person p=personDao.findByUsername(username);
        Access a=accessDao.findByCode(accessCode);
        ... etc ...
    }
}

这是一个使用 Dao 实现的服务

@Service
public class PersonService {

    @Autowired
    private PersonDAO personDao;

    @Autowired
    private AccessDAO accessDao;

    @Transactional
    public boolean hasPermission(String username, String password){
        Person p=((PersonHibernateDao)personDao).findByCriteria(Restrictions.eq("username", username);
        ... etc ...
    }
}

这两种方法哪一种是正确的?


编辑2

所以,总结一下我的理解:

// BASE DAO INTERFACE
public interface DAOInterface<EntityClass, IDType extends Serializable> {
    EntityClass get(IDType id);
    EntityClass findById(IDType id);
    EntityClass save(EntityClass entity);
    EntityClass update(EntityClass entity);
    void delete(EntityClass entity);
}

// AN HIBERNATE IMPLEMENTATION
public abstract class HibernateDAO<EntityClass, IDType extends Serializable> implements DAOInterface<EntityClass, IDType> {

    @Autowired
    private SessionFactory sessionFactory;

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

    public void getSessionFactory(){
        return this.sessionFactory;
    }

    // Implements all DAOInterface method using sessionFactory

}

// PERSON DAO INTERFACE
public interface PersonDAO extends DAOInterface<Person, Long>{

    Person findByName(String name, String surname);
    List<Person> getInAgeRange(int year1, int year2);
}

// PERSON HIBERNATE DAO IMPLEMENTATION
public PersonHDAO extends HibernateDAO<Person, Long> implements PersonDAO{

    // Implements the methods of PersonDAO interface using sessionFactory
}

@Service
public class PersonService {

    //spring inject the correct DAO by its xml config(in this case PersonHDAO
    @Autowired
    private PersonDAO personDAO; 

    // spring manage the transaction
    @Transactional
    public List<Person> getInAgeRange(int year1, int year2){
        return personDAO.getInAgeRange(year1, year2);
    }

}

// NOW... HOW USE IT
//let's assume i have a button, pressing it a table will be populated with all persons in age range
private void actionPerfom(ActionEvent e){
    List<Person> list=personService.getInAgeRange(age1Spinner.getValue(), age2Spinner.getValue());
    //Load a table with list
}

抱歉这堵文字墙,也许对我希望的其他人有用,我朝着正确的方向前进?我的服务层需要接口吗?都是正确分层的吗?我也需要控制层吗?

谢谢。

4

2 回答 2

2

我的建议:

对于较大的项目,请使用基于接口的专用 DAO 层。不要让您的服务层知道任何有关底层持久性技术的信息。仅在 DAO 层中使用 Hibernate / JPA / JDBC / JDO / 任何东西。

对于较小的项目,可能只有一个服务层是可以的(特别是考虑到 HibernateSession和 JPA都EntityManager 公开了大多数标准 DAO 行为。

基本经验法则:如果您要进行技术更改,请确保您只需要更改应用程序的一层

更新:这是一个示例 DAO 接口。您的服务层将仅针对此接口进行编码,并且实现将执行 session / entityManager / jdbc 调用,而无需服务层知道。

public interface CustomerDao extends CommonDao<Customer>{
    Customer getCustomerByEmail(String emailAddress);
    List<Customer> getCustomersWithinAgeRange(int lowerBound, int upperBound);
}

关键:在你的服务层,指定你的依赖基于接口,即

private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao){
    this.customerDao = customerDao;
}

代替

// this is horrible, it ties the service layer to implementation
// details of the dao layer
private HibernateCustomerDaoImpl customerDao;
public void setCustomerDao(HibernateCustomerDaoImpl customerDao){
    this.customerDao = customerDao;
}
于 2010-11-11T09:33:42.317 回答
1

DAO 是任何数据库特定查询的地方 - 在您的情况下是 JDBC 或 Hibernate。

服务层旨在向消费者提供 API,例如表示层或其他层。没有理由用数据库细节污染服务层。您的服务层可能具有很好的业务逻辑,但它不应该知道底层数据库实现 IMO

于 2010-11-11T09:33:55.493 回答