域对象中的业务逻辑 [英] Business logic in domain objects

查看:169
本文介绍了域对象中的业务逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为网站编写功能区/成就系统,我必须为系统中的每个功能区编写一些逻辑。例如,如果您是第一批注册到该网站的2,000人或者在论坛中发布了1,000个帖子后,您就可以获得一个功能区。这个想法非常类似于stackoverflow的徽章,实际上。

I am coding a ribbon/achievements system for a website and I have to write some logic for each ribbon in my system. For example, you could earn a ribbon if you're in the first 2,000 people registering to the website or after 1,000 post in the forum. The idea is very similar to stackoverflow's badges, really.

因此,每个功能区显然都在数据库中,但它们还需要一些逻辑来确定用户何时获得功能区。

So, every ribbon is obviously in the database but they also need a bit of logic to determine when a user has earned the ribbon.

在编码方式中,功能区是一个简单的界面:

In the way I coded it, Ribbon is a simple interface:

public interface Ribbon {
    public void setId(int id);
    public int getId();
    public String getTitle();
    public void setTitle(String title);
    public boolean isEarned(User user);
}

RibbonJpa 是一个实现功能区接口的抽象类,避免定义 isEarned()方法:

RibbonJpa is an abstract class that implements the Ribbon interface, avoiding the definition of the isEarned() method:

@Entity
@Table(name = "ribbon")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ribbon_type")
public abstract class RibbonJpa implements Ribbon {
    @Id
    @Column(name = "id", nullable = false)
    int id;

    @Column(name = "title", nullable = false)
    private String title;

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id= id;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }
}

您可以看到我将继承策略定义为 SINGLE_TABLE (因为我必须像50个色带一样编码,我不需要为其中任何一个添加其他列)。

You can see I define the inheritance strategy as SINGLE_TABLE (since I have to code like 50 ribbons and I don't need additional columns for any of them).

现在,一个特定的功能区将实现如下:

Now, a specific ribbon will be implemented like this:

@Entity
public class FirstUsersRibbon extends RibbonJpa implements Ribbon {
    public FirstUsersRibbon() {
        super.setId(1);
        super.setTitle("First 2,000 users registered to the website");
    }

    @Override
    public boolean isEarned(User user) {
        // My logic to check whether the specified user has won the award
    }
}

这段代码工作正常,表格是按照我的方式在数据库中创建的期望(我在我的本地环境中使用DDL生成)。

This code works fine, the tables are created in the database in the way I expect (I use DDL generation in my local environment).

问题是,在域对象中编写业务逻辑感觉不对。这是好的做法吗?你能建议更好的解决方案吗?此外,我无法自动装入实体中的任何DAO( FirstUsersRibbon ),我需要它们在业务逻辑中(在这种情况下,我需要一个DAO来检查是否用户是在网站上注册的前2,000位用户。)

The thing is, it feels wrong to code business logic in a domain object. Is it good practice? Can you suggest a better solution? Also, I'm not able to Autowire any DAOs in the entity (FirstUsersRibbon) and I need them in the business logic (in this case, I need a DAO to check whether the user is in the first 2,000 users registered to the website).

任何帮助非常感谢。

谢谢你好!

推荐答案


问题是,在域对象中编写业务逻辑感觉不对。

The thing is, it feels wrong to code business logic in a domain object.

很多人会说反过来是正确的:它是一种反模式(贫血领域模型)在其他地方拥有业务逻辑。有关详细信息,请参见域驱动设计

Many would say the reverse was true: that it is an anti-pattern (an anaemic domain model) to have business logic anywhere else. See Domain-Driven Design for more information.

您可能会想知道传统3层架构的中间层是什么。它为应用程序提供服务层。请参阅我的相关问题 EJB的用途是什么?

You might then wonder what the middle tier of the conventional 3-tier architecture was for. It provides a service layer for the application. See my related question "What use are EJBs?".

这篇关于域对象中的业务逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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