播放2.4:如何将问题与Ebean分开?

时间:2015-07-01 11:44:53

标签: java playframework ebean separation-of-concerns

所以最近开始使用Play 2.4的新网络应用程序。我曾经在很多项目中使用JPA,但这次我决定给Ebean一个机会。

然而,我想知道关注点的分离。使用JPA,我通常会为持久性创建模型,服务和DAO。使用Ebean,我了解模型上对静态方法的支持(参见http://ebean-orm.github.io/quickstart"创建模型")。

我已经看过很多例子,其中所有内容似乎都存在于模型中的这些静态方法中;业务逻辑,持久性逻辑和什么不是。这是我使用Ebean时最好的做法吗?或者使用服务和DAO分离逻辑是否仍然有意义,我将以最佳方式实现这一目标?

1 个答案:

答案 0 :(得分:3)

您可以像使用JPA一样完成任务,并创建DAO等。

如果您检查Ebean示例中的静态方法,您会发现它们通常使用Finder。对于主键为Country String的表iso2,您将拥有如下模型类:

@Entity
public class Country extends Model {
    @Id
    public String iso2;

    // other stuff
}

在一个Ebean示例ActiveRecord风格的代码中,会有一些额外的东西。

@Entity
public class Country extends Model {

    private static final Finder<String, Country> FIND = new Finder<>(String.class, Country.class);

    @Id
    public String iso2;

    public static List<Country> findAll() {
        return FIND.all();
    }

    // other stuff
}

如果您想将其更改为DAO风格的方法,则只需将Finder相关代码移出Country

public class CountryDao {
    private static final Model.Finder<String, Country> FIND = new Model.Finder<>(String.class, Country.class);

    // not static if you don't want it to be
    public List<Country> findAll() {
        return FIND.all();
    }
}

你使用哪种方法进入意见领域,但DAO方法有很多值得推荐的,特别是现在Play支持DI开箱即用。您可以注入DAO,而不是让控制器对模型类进行硬编码引用。

// direct reference to the Country class
public class ControllerWithActiveRecord extends Controller {
    public F.Promise<Result> getAllCountries() {
        return F.Promise.promise(() -> Country.findAll())
                        .map(Json::toJson)
                        .map(Results::ok);
    }
}

使用DI替代。

public class ControllerWithInjectedDAO extends Controller {

    private final CountryDAO countryDAO;

    @Inject
    public ControllerWithInjectedDAO(final CountryDAO countryDAO) {
        this.countryDAO = countryDAO;
    }

    public F.Promise<Result> getAllCountries() {
        return F.Promise.promise(() -> countryDAO.findAll())
                        .map(Json::toJson)
                        .map(Results::ok);
    }
}
相关问题