Results 1 to 5 of 5
  1. #1
    bdmstyle is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Design - how map Request parameters data to domain model

    This is a design question is the same problem in any language.

    Guys as you do to map the controller to the domain model?

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

    situation.1 - We have a request that has all the parameters of the account ;
    { " id" : " 1 " , "name " : "test " , "some " : " xxx " } ............. and other fields .

    situation.2 - can request that has to have a certain account parameters , for example in the case of an update;
    {" id" , " 1" , "name " , " testUpdated "}

    situation.3 - We have a request that has some parameters of the account , others have more like id as user together;
    { " id" : " 1 " , "user " : " xxx " , "service " : " yyy " } in which case each piece of the request will turn an object .

    Java Code:
        public class Account {
    
        private Long id;
        private String name ;
        private String some ;
    
        }
    I see a few options ;

    1 - Can I get AccountForm in the controller and set the properties for the Account object and others in CONTROLLER ;

    + ok for situation.1 situations 2, and situation.3
    + Separates the requisition of the object domain
    - Pollutes the controller with code conversion
    - Controller is full of setters .. if a higher class as a large object as a request is very confusing .

    Java Code:
        controller ( AccountForm from ) {
        Account account = new Account ( )
        account.setNome form.getNome = ();
        account.setSome form.getSome = ();
        Other outher = new Other ( ) ;
        other.setSome ( form.getSome ( ) ) ;
        }
    2 - Can I get AccountRequest in the controller and have a method in itself as AccountRequest.getAccount ( ) to return a mapped model , in this case the mapping is at own Request object .

    + Separates the requisition of the object domain
    + Encapsulates the conversion in a place with easy access .
    + Meets situation.1 situation.2 and situation3 ;
    - Request object has two responsibilities represent the request and map to a valid model .

    Java Code:
        controller ( AccountForm accountRequest ) {
        Account account = accountRequest.getAccount ( ) ;
        Outher outher accountRequest.getOther = ( ) 
        }
    3 - Can I get the controller Direct Account which had been filled with nulls .

    + Eliminate object request
    - Serves only situation.1 situation.2 .

    Java Code:
        controller (Account account ) {
        account.someMethod ();
        }
    4 - Outsource this mapping request parameters to another object mapper for request ..

    + Isolates logic mapping
    - Until complexity for simpler cases are used as standard for all such a find by id .
    - One more class for each request ;

    In the case of API gets worse response has two further classes. speaking in terms of request for response .... AccountRequest, AccountRequestMapper, Account, AccountResponseMapper, AccountResponse ........

    I'm doing more testing the Hybrid option 3 for simple cases (find ID or updates) .... with option 2 for example for more complex cases ...

    What would be ideal / What is expressive and easy to maintain? Thank you.

  2. #2
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Design - how map Request parameters data to domain model

    Hi,

    you could use java.lang.reflect...

    Java Code:
    private static void callSetters(Object obj, Map<String, Object> parameters)
    		throws IllegalAccessException, IllegalArgumentException,
    		InvocationTargetException {
    	Map<String, Method> mapSetters = getMapSetters(obj.getClass());
    
    	for (String paramName : parameters.keySet()) {
    		Method setter = mapSetters.get(paramName);
    		Object value = parameters.get(paramName);
    		if (null != setter && null != value) {
    			setValue(obj, setter, value);
    		}
    	}
    }
    
    private static void setValue(Object obj, Method setter, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    	setter.invoke(obj, value);
    }
    
    private static Map<String, Method> getMapSetters(Class<?> clazz) {
    	Map<String, Method> mapSetters = new HashMap<>();
    	for (Method method : clazz.getMethods()) {
    		if (null != method && null != method.getName()
    				&& method.getName().startsWith("set")) {
    			String methodName = method.getName();
    			String attribute = methodName.substring(3);
    			attribute = attribute.toLowerCase();
    			mapSetters.put(attribute, method);
    		}
    	}
    	return mapSetters;
    }
    Then you can set all parameters with:

    Java Code:
    Account account = new Account ()
    callSetters(account, parameters);
    With this you can map parameter to objects without knowing the class of the object.
    The class and the list of paramter have to following some simple rules for this example to work:

    1. The setter and the name parameter have to be fitting (setter = "setName" : parameterName = "name").
    2. The type of the value expected by the setter has to be the same as the type of the value for the parameter.
    3. All parameter have to be in a map.

  3. #3
    bdmstyle is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Design - how map Request parameters data to domain model

    Is ok, but where do you put this code in controller?

  4. #4
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Design - how map Request parameters data to domain model

    If you use your own AbstractController that is implemented in all your controllers you can put it there.
    Else you could create a new utility class and put it there, than you can access it from all controllers.

    And if the type of the values in parameters does not fit the type of value in setters, you could add a method to parse the parameter if possible.
    I would call such a method within the method callSetters after the line:
    Object value = parameters.get(paramName);

    The call of the new parse Method could be like this:
    Class<?> type = setter.getParameterTypes()[0];
    value = parseValue(value, type);

    If parseValue() fails it should not throw an Exception but instead return "null" to stop the setter from being invoked.

  5. #5
    bdmstyle is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Design - how map Request parameters data to domain model

    Thanks for help .

Similar Threads

  1. Replies: 0
    Last Post: 09-20-2010, 10:25 AM
  2. JavATE: The domain driven design framework
    By andreamattioli in forum Java Software
    Replies: 0
    Last Post: 04-13-2010, 06:08 PM
  3. How to set request parameters to an URLConnection
    By somesh A in forum Networking
    Replies: 0
    Last Post: 04-10-2009, 09:08 AM
  4. web design software(Domain Name and web hosting for sales)
    By Bettypeng in forum Reviews / Advertising
    Replies: 0
    Last Post: 01-22-2008, 09:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •