简体   繁体   English

在MVP / MVC架构中使用有状态Bean作为演示者/控制器是一个好主意吗?

[英]Is it a good idea to use a stateful bean as a presenter/controller in a MVP/MVC architecture?

I'd like a controller that is able to seamlessy access the persistence layer and EJB is a nice technology for this purpose. 我想要一个能够无缝访问持久性层的控制器,而EJB是一个很好的技术。 I can do something like this: 我可以做这样的事情:

@PersistenceContext
EntityManager em;

...    

Cat timmy = em.findById(Cat.class, "Timmy");
timmy.color = "red";

And it feels like timmy is in-memory. 感觉就像是在记忆中。 The sad part is that this has to be done at business level because beans are only at business level. 可悲的是,这必须在业务级别上完成,因为bean仅在业务级别上。 However transactions should be defined at client level in the MVP. 但是,应在MVP的客户端级别定义事务。 That's why I would like to have a Presenter that is a @Stateful bean. 这就是为什么我想要一个Presenter是@Stateful bean。 This way I can start and end transactions at client level. 这样,我可以在客户端级别开始和结束事务。 Anyways I have this strange feeling that @Stateful bean are evil and I'd rather not use them. 无论如何,我有一种奇怪的感觉,即@Stateful bean是邪恶的,我宁愿不使用它们。

is that: 就是它:

@PersistenceContext
EntityManager em;

...

Cat timmy = em.findById(Cat.class, "Timmy");
timmy.setColor("red");
em.merge(timmy);

working? 工作?

in term of architecture I use @Stateless Dao 在架构方面,我使用@Stateless Dao

@Stateless
public class CatDao extends Dao<Cat> {
    public CatDao () {
        super(Cat.class);
    }
}

public class Dao<E> {

    @PersistenceContext(unitName = "myappPU")
    private EntityManager em;

    private final Class<E> entityClass;

    protected Dao(Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    public E find(Object id) {
        return em.find(entityClass, id);
    }

    public List<E> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        Query q = entityManager.createQuery(cq) ;
        return q.getResultList();
    }

    public void create(E entity) {
        em.persist(entity);
    }

    public void edit(E entity) {
        em.merge(entity);
    }

    public void remove(E entity) {
        em.remove(entityManager.merge(entity));
    }
}

so in the controller I do 所以在控制器中

    @Inject
    private CatDao dao;

    //...

    Cat cat = dao.find("Timmy");
    cat.setColor("red");
    dao.edit(cat);

see... statless 看...无状态

Netbeans generate this Dao automatically but called them Facade (new>Other>Entreprise JavaBeans>session beans for entity classes) Netbeans自动生成此Dao,但将其称为Facade(新的> Other> Entreprise JavaBeans>实体类的会话bean)

Finally I did it! 终于我做到了! The solution was hidden in CDI specs: 该解决方案隐藏在CDI规范中:

@Inject CatRepository catRepository;

 @Transactional
 void changeColorToCat(String color, String catName) {
    Cat timmy = catRepository.findByName("Timmy");
    timmy.color = "red";
 }

And it's done! 完成了! It's like databases don't exist at all! 就像数据库根本不存在!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM