DAO模式 - 使用静态或非静态方法? [英] DAO pattern - use Static or non Static methods?

查看:262
本文介绍了DAO模式 - 使用静态或非静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用DAO模式。
我应该使实现方法静态吗?

My application is using a DAO pattern. Should I make the implementing methods "static" or not?

我在说这个具体的实现:

I'm talking about this concrete implementation:


  • findByIdentifier(String identifier)

还有关于Generic的实现:

But also about the Generic implementations:


  • E getByID(K ID);

  • 列出findAll();


  • 更新(E实体);

  • 删除(E实体);

  • E getByID(K ID);
  • List findAll();
  • save(E entity);
  • update(E entity);
  • delete(E entity);

感谢您的建议!

public interface DaoBase<K, E> {
    E getByID(K ID);
    List<E> findAll();
    void save(E entity);
    void update(E entity);
    void delete(E entity);
}



public interface DaoLanguage extends DaoBase<Long, LanguageEntity> {    
    LanguageEntity findByIdentifier(String identifier);
}

我正在使用JPA来保留所有实体类。

I'm using JPA to persist all entity classes.

public class JpaDaoLanguage extends JpaDaoBase<Long, LanguageEntity> implements DaoLanguage {
    public LanguageEntity findByIdentifier(String identifier) {
        LanguageEntity language = (LanguageEntity) entityManager.createQuery(
                "select lan from LanguageEntity as lan where lan.identifier = ?1")
                .setParameter(1, identifier)
                .getSingleResult();
        return language;
    }
}

通用基本实现

public abstract class JpaDaoBase<K, E> implements DaoBase<K, E> {
    protected EntityManager entityManager;
    protected Class<E> entityClass;

    @SuppressWarnings("unchecked")
    public JpaDaoBase() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass()
                .getGenericSuperclass();
        this.entityClass = (Class<E>) genericSuperclass
                .getActualTypeArguments()[1];

        EntityManagerFactory factory = Persistence
                .createEntityManagerFactory("CyberLabPersistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public E getByID(K ID) {
        return entityManager.find(entityClass, ID);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<E> findAll() {
        Query q = entityManager.createQuery("SELECT e FROM "
                + entityClass.getName() + " e ");
        return (List<E>) q.getResultList();
    }

    @Override
    public void save(E entity) {
        entityManager.persist(entity);
    }

    @Override
    public void update(E entity) {
        entityManager.merge(entity);
    }

    @Override
    public void delete(E entity) {
        entityManager.remove(entity);
    }
}


推荐答案

尝试这是为您使用该DAO的代码编写单元测试。

Try this: write a unit test for the code you have that uses this DAO.

您的目标之一应该是快速通过的测试,并且没有对外部资源的依赖(良好的单元测试不应该要求实际的数据库存在))所以,不用让你的测试调用真正的DAO,创建一个返回一个固定的语言实体的模拟DAO,并且你的测试代码使用模拟而不是真正的DAO。那还行吗如果真正的DAO具有这些方法的静态实现,你可以成功地将你的模拟DAO替换为真正的DAO?

One of your goals should be tests that pass very quickly, and don't have dependencies on external resources (a good unit test should not require an actual database to be present.) So instead of having your test call the real DAO, create a mock DAO that returns a fixed language entity, and have your test code use the mock instead of the real DAO. Does that still work? Can you successfully substitute your mock DAO for the real DAO if the real DAO has static implementations of these methods?

静态使你的代码变脆。避免它们,你可以。

这篇关于DAO模式 - 使用静态或非静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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