Controller mapping for the Domain model

Asked

Viewed 127 times

1

We have the following situations in general, consider objects larger than the example.

Situation 1
We may have a request that has all Account parameters;
{"id":"1";"name":"test","some":"xxx".............} and the other fields.

Situation 2
We may have a request that has some Account parameters, for example in the case of an update;
{"id":"1";"name":"testUpdated"}

Situation 3
We may have a request that has some parameters from Account, as id plus has others as user together;
{"id":"1";"user":"xxx","service":"yyy"} in which case each piece of the request will become an object.

Example of Dominio

public class Account {    
    private Long id;
    private String name ;
    private String some ;    
}

I see some options;

1 - I can receive this Accountform in the controller and set its properties in the Account object and others if NO CONTROLLER;

+ atende as situações 1, 2 e 3 
+ separa a requisçao do objeto de dominio 
- polui o controller com codigo de conversao 
- controller fica cheio de setters.. se for uma classe maior como um objeto grande como um pedido fica bem confuso. 

controller(AccountForm from){
    Account account = new Account()
    account.setNome=form.getNome();
    account.setSome=form.getSome();
    Other outher = new Other();
    other.setSome(form.getSome());
}

2 - I can receive in the controller this Accountrequest have a method in itself as AccountRequest.getAccount() to return a mapped model, in which case the mapping is in the Request object itself.

+ separa a requisçao do objeto de dominio 
+ encapsula a conversao em um lugar de facil acesso. 
+ atende situacao1 situacao2 e situacao3; 
+ um objeto burro que so tem os parametros da requisçao tem alguma funcionalidade; 
- objeto de request tem duas responsabilidades representar a requisiçao e mapear para um model valido.

controller(AccountForm accountRequest){
   Account account = accountRequest.getAccount();
   Outher outher = accountRequest.getOther();
}

3 - I can receive direct account in the controller where it will be full of nulls.

+ elimino objeto de request 
+ alta simplicidade 
- atende apenas a situacao1 situacao2 , por que request pode ter informações sobre vários objetos... o que não funcionaria. 
- forte acoplamento.

controller(Account account){
    account.someMethod();
}

4 - Externalize this mapping of the request parameters to another request mapper object.

+ isola logica do mapeamento 
- complexidade ate para casos mais simples se usado como padrão para tudo por exemplo um find por id.
- mais uma classe para cada request;

In the case of responsive Api, 2 more classes are worse. speaking in terms of Sponse request....

request | mapper | model | mapper | response 
AccountRequest, AccountRequestMapper,Account,AccountResponseMapper,AccountResponse........ 

I’m doing Option 3 Hybrid plus testing for simple cases(find by id or updates).... with option 2 for example for more complex cases... seems ideal from what I’m seeing, I hope with your opinions it becomes clearer.. what could be better, however in the hybrid approach, the problem is that Oce does not have the famous "PADRAO" and has to think about how to do each case, and I see resistance in adopting something like this. The bad thing about PADRAO is that it generates architecture to make a find for example.

What you use /What would be ideal/ What becomes expressive and easy to maintain?

  • Imagine the work you can do to answer and help you with something you need. If you can’t make an effort, because random people on the internet who get nothing for it should make an effort?

  • I should summarize the question to the maximum. This is not a question is a thesis... The simpler the question, the faster you will get the answer, then you can improve, sharpening the answers

  • Well the question is simple and it’s about code design and architecture, I’d like to hear experience-based opinions, anyone who’s not junior has had to opt for any of these in any web application. the case I’m seeing is that only Jr around here and are wanting to earn points... because of this gamification so I hate this model of questions and answer, so was testing this tool for those who do not believe look at the level of the answers to the same question in guj http://www.guj.com.br/java/308252-mapping-do-controller-para-o-domain-model#1639927

  • I think there is a confusion of several things here. The problem with your question is not even the benefit it can bring, but that it can potentially provoke some discussion, which is one of the premises the site tries to avoid. In fact, the correct thing is to take it to a forum or a place where the discussion is free (as the case of the GUJ). I apologize if the site does not meet its purpose.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.