如何在静态上下文中使用泛型类和特定对象? [英] How to use generic class with specific objects in a static context?

查看:114
本文介绍了如何在静态上下文中使用泛型类和特定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽力解释。

我使用Play Framework 2,我会做很多CRUD操作。其中一些将是identitcal,所以我想KISS和DRY所以起初我在考虑一个包含列表的抽象类详细信息创建更新删除方法,使用通用对象,并通过指定要使用的对象(Model& Form)扩展此类:

I use Play Framework 2, and I will do a lot of CRUD actions. Some of them will be identitcal, so I'd like to KISS and DRY so at first I was thinking about an abstract class containing the list, details, create, update and delete methods, with generic object, and extend this class by specifying which object to use (Model & Form) :

public abstract class CrudController extends Controller {
    protected static Model.Finder<Long, Model> finder = null;
    protected static Form<Model> form = null;

    public static Result list() {
        // some code here
    }

    public static Result details(Long id) {
        // some code here
    }

    public static Result create() {
        // some code here
    }

    public static Result update(Long id) {
        // some code here
    }

    public static Result delete(Long id) {
        // some code here
    }
}

一个将使用CRUD的类:

And a class that will use CRUD :

public class Cities extends CrudController {
    protected static Model.Finder<Long, City> finder = City.find;
    protected static Form<City> form = form(City.class);

    // I can override a method in order to change it's behavior :
    public static Result list() {
        // some different code here, like adding some where condition
    }
}

如果我不在静态环境中,这将有效。

This would work if I wasn't in a static context.

但是既然如此,我该怎么办?

But since it's the case, how can I do ?

推荐答案

这可以使用委托来实现:定义一个包含CRUD动作逻辑的常规Java类:

This can be achieved using delegation: define a regular Java class containing the CRUD actions logic:

public class Crud<T extends Model> {

    private final Model.Finder<Long, T> find;
    private final Form<T> form;

    public Crud(Model.Finder<Long, T> find, Form<T> form) {
        this.find = find;
        this.form = form;
    }

    public Result list() {
        return ok(Json.toJson(find.all()));
    }

    public Result create() {
        Form<T> createForm = form.bindFromRequest();
        if (createForm.hasErrors()) {
            return badRequest();
        } else {
            createForm.get().save();
            return ok();
        }
    }

    public Result read(Long id) {
        T t = find.byId(id);
        if (t == null) {
            return notFound();
        }
        return ok(Json.toJson(t));
    }

    // … same for update and delete
}

然后你可以定义一个Play控制器,其静态字段包含 Crud< City> 的实例:

Then you can define a Play controller having a static field containing an instance of Crud<City>:

public class Cities extends Controller {
    public final static Crud<City> crud = new Crud<City>(City.find, form(City.class));
}

你差不多完成了:你只需要定义路线Crud行动:

And you’re almost done: you just need to define the routes for the Crud actions:

GET     /                     controllers.Cities.crud.list()
POST    /                     controllers.Cities.crud.create()
GET     /:id                  controllers.Cities.crud.read(id: Long)

注意:此示例为brevety生成JSON响应,但可以呈现HTML模板。但是,由于Play 2模板是静态类型的,因此您需要将所有这些模板作为 Crud 类的参数传递。

Note: this example produces JSON responses for brevety but it’s possible to render HTML templates. However, since Play 2 templates are statically typed you’ll need to pass all of them as parameters of the Crud class.

这篇关于如何在静态上下文中使用泛型类和特定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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