declex icon indicating copy to clipboard operation
declex copied to clipboard

Add parameters for Model Types

Open smaugho opened this issue 7 years ago • 0 comments

Right now @Model annotation takes as parameter a "query", "orderBy" and "fields" parameters. But since @UseModel descendant can have different requirements, this doesn't fully meet all the requirements.

For instance, for the @ServerModel, the "orderBy" parameter it is used to specify what request to do in the backend to Inject or Store the model, but this is a little odd.

There's two solutions to this issue, one is to create for every annotation the parameters that it requires (so, @ServerModel can be specified better).

Now, one of the ideas behind using @Model it was to permit to the controller to get the model, independently of the mean from which this model had to be fetched. But each controller has its own requirements for each fetch, so this "controller-specific" parameters are the one that are typically sent when injecting the model (it is also easier to have this in the controller, so that you don't have to add a method in an outside class to program these parameters.

A solution would be to pass the parameters by name in a params bidimentional array:

@Model({
    "query = age > 50",
    "orderBy = created_at DESC"
})
List<User> users;

or simply

@Model("age > 50")
List<User> users;

In this case, the first parameter, is the default parameter (it is not mandatory to provide the name). It can be also:

@Model({"age > 50", "orderBy = created_at DESC"})
List<User> users;

for a network connection then it makes more sense:

@Model("list")
List<User> users;

Other variables could be passed and used then, a good example is fields, which needed to be added and it complecated the logic a lot, in this way, this will be no dependent of this. It would fulfill:

@Model({"list", "fields = name, email, image"})
List<User> users;

A second approach is to create @Model at compile time, adding all the annotations used in the project. Every time an annotation is used, it introduce the name of the parameters it supports, with this a @Model class is created using all those parameters, then basically it can be used:

@Model(
    query = "age > 50", 
    orderBy = "created_at DESC", 
    name = "list"
)
List<User> users;

Where the parameters "query" and "orderBy" are introduced by the @LocalDBModel annotation, and "name" is introduced by the @ServerModel annotation.

In this approach the param "value" of @Model annotation can be assigned to the "default" parameter of the model itself.

smaugho avatar May 08 '17 15:05 smaugho