Pro/Con of reusing single CommandObject instance between multiple forms/actions? - grails

Are there any disadvantages to using a single instance of a grails command object across multiple actions?
I.E. you have a several pages with forms in a process and thus create a single grails command object class and only use a single instance of the class throughout the process.
Presume the instance is stored in the flow of a webflow, thus accessible throughout the process, and when validate is called it's provided with a list of properties to validate instead of validating all the properties. The command object properties are populated like obj.property1 = params.property1 not with bindData() or as a parameter.
Is this a good fit for a grails command object or should a command object for each form be created?
Edit:
I guess what I'm looking for is a Pro/Con list of using one command object for one entire process with multiple forms vs. multiple command objects.

I would prefer to use single CommandObject for each form and only reuse the CommandObject in multiple forms if the forms are for same purpose but in different pages.

Related

Is there way to ignore Ids generated to Binding class

When using viewBinding with ConstraintLayout, maybe a lot of Ids will be create to help describe the view relationship, but will never used in Kotlin/Java.
I just found two useful tools attribute
tools:viewBindingIgnore="true" used to prevent whole layout generated in Binding, but not for single Id.
tools:viewBindingType="TextView" used for changing the Type generated in Binding.
So is there any way to ignore Id(s)? I don't want to expose them to pollute the Kotlin/Java.
Currently, there is no way to ignore view binding generation on view by view basis. If you use Proguard the unused ViewBinding IDs will be removed from the final APK.

Populate model properties inside of the model

I'm developing a webpage in MVC 5 and have been developing in MVC quite sometime now.
I always try/want to learn new ways/best practice of my programming skills. Right now I just stumbled upon something with ViewModels.
For example, I'm using the same ViewModel for multiple pages where I have Dictionary properties to fill my drop down lists.
So normally what I have done is creating a private method in the controller like "SetupViewModel" and then populate the dictionary to the model properties there and inside the model constructor just a "failsafe" creating an empty dictionary.
But for this project I'm working on now I thought that I would try to directly in the model constructor call my service method that returns the list for the ddl and then populate it right there.
Is there any advantages or disadvantages doing this way. What would you say is best practice? Because I can see some problems like, if the database goes down I could still load the page with empty values if I have the "setupViewModel"- method in the controller and with some try/catch or something preventing crash and if it is in the VM it would crash directly if I don't add some fail-safe in the services like returning empty lists if I can't get anything from the DB.
So it's equally much development but at different locations (well I can always have some fail-safe in the service ofc).
But the main question is, what is the best practice, populate model properties from model or controller?
Personally in MVC I see accessing the data layer as a model concern. Controllers are supposed to do as little as possible. So ideally I would in the constructor first initialise the dictionary to an empty one, then in a try..catch (which does nothing on error) get the database value for the dictionary and set it again.

Grails: how to programatically bind command object data to domain object in service?

I have a command object that I want to convert into a domain object.
However, the object I want to convert the command object into may be one of two domain classes (they're both derived classes), and I need to do it in a service (which is where, based on other data, I decide which type of object it should be bound to). Is this possible and what's the best way to do this? bindData() only exists in a controller.
Do I just have to manually map command object parameters to the appropriate domain object properties? Or is there a faster/better way?
If the parameters have the same name, then you can use this question to copy the values over. A quick summary can be as follows.
Using the Grails API
You can cycle through the properties in a class by accessing the properties field in the class.
object.properties.each { property ->
// Do something
}
You can then check to see if the property is present in the other object.
if(otherObject.hasProperty(property) && !(key in ['class', 'metaClass']))
Then you can copy it from one object to the other.
Using Commons
Spring has a really good utility class called BeanUtils that provides a generic copy method that means you can do a simlple oneliner.
BeanUtils.copyProperties(object, otherObject);
That will copy values over where the name is the same. You can check out the docs here.
Otherwise..
If there is no mapping between them, then you're kind of stuck because the engine has no idea how to compare them, so you'll need to do it manually.

Using a command object chain command with a view

I am using grails v2.3.3 and I am trying to work with a set of instances of a command object across two actions.
In the first action I create the set of command object instances which I then display in a view to be edited.
I then use a link in the view to submit this data to another action in the same controller that needs to access the updated set of command object instances.
I have looked at the 'chain' command which enables command objects to be accessible across different actions but it seems to offer a direct link from one action to another without the option to display a view and enable some user interface.
I cannot see how to implement this with a view in order to update the contents of these command object instances which then get sent to the 2nd action to be processed.
I have the command object set to a 'session' scope and am surprised that by default it is not accessible across all actions of the controller anyway - what does the scope mean?
-mike
The normal way of doing this is to recreate the command objects from posted form data in the second action. This means that the view's form must include all of the fields of the command object either as visible (editable) or hidden (pass through) input fields. Grails command object binding support will automatically populate new command objects if you place them as parameters to the action. Also, you can always manually construct new command object instances using the "params" map values which contains all of the posted form fields.
See the sections on Command Objects and Data Binding in the Grails manual for details and examples.

Grails: Performance benefits to using Command objects to validate constraints over using Domain objects

Is there any performance benefits to using Command objects to validate constraints over using Domain objects to validate constraints
Meaning if i intercept incoming params and place then in a command object and call cmd.hasErrors() , before binding it to the object and trying a obj.save()
and allowing the object constraints to do the validation
I've never noticed any performance benefit - in fact there may be a slight performance hit doing this. Your command object will do it's validation and binding and your domain object will still do it's validation and binding as well. That's just a guess and if true it's probably a relatively small hit.
My basic strategy has been if I'm doing form input for strictly a single class I just use the domain. If there is either no domain required (like for a login) or multiple domains (like an author and a book) on the same form use a command object.

Resources