Add a field to a Grails Domain Class? - grails

I would like to add a field to an existing domain class. I do not want to lose my view files however as I know will happen if i run generate-all. Is there another way I can update the mapping etc?

I think this is a common concern.
This is not a direct solution to your problem, but this is what I do and works very well for me.
I never make direct modifications to the grails scaffolded artifacts (views and controllers)
I keep my production views/controllers separate from the scaffolded artifacts; through I use the scaffolded as the starting point of my application controllers and views.
If there are changes to the domain model, I re-generate the views and copy-paste (wherever possible) from scaffolded artifacts to hand-coded artifacts.
At some point, I either delete all the scaffolded artifacts from the app or just protect access to them.
Hope this helps.

There are a couple of ways to do this.
If you have not modified your generated views too much and are using version control, you could allow grails to overwrite your views and then merge the changes to the new templates with what is in version control.
If you have modified your generated views, you could just answer no to the prompt for overwriting the views. If you are only adding a new field, changing a field name or something simple like that, modifying the view templates should be pretty straight forward to do manually.
You would deal with any possible changes to the Controllers in the same way.

just declare it:
class MyDomain {
String newField;
}
Should be all you need to do.

Don't regenerate the views, just copy and paste another similar field to the one you are putting in the view(s) you want.

Related

How to generate entities to custom folder with jhipster:entity?

I would like to generate my models/entities into a custom folder/package and not to domain. Is this possible with the entity generator command?
it wasn't as easy for me. After refactoring the entity class, I had to change all the classes and .js files generated by jhipster. For example, #RequestMapping in the rest controller has to be changed by adding the new path to the entity (#RequestMapping("/myEntity") becomes #RequestMapping("newpackage/myEntity")).
No.
Of course, refactoring your domain classes is just a matter of dragging/dropping those files to another directory, if you have a decent IDE, so that shouldn't be an issue.

Updating scaffolded controllers and views with model changes

For the basic functionality of my project I've been defining Entity Framework models and then using the scaffolding feature of Visual Studio to implement CRUD functionality (Right Click -> Add -> New Scaffolded Item...).
If I need to make a change to the model after I've done this, how do I make the scaffolded controllers and views update to the latest model changes? For example, I'm seeing the need now to add a new attribute to my model and change the data type of an existing one, but the controllers and views likely won't work after the change.
I could always just re-scaffold, but I've made some changes to the controller and views already which I don't want to lose. Is my only solution to make the changes myself?
Rename the files having your changes, scaffold again, and replace the newly-scaffolded files with your originals.
Naturally, you'll have to reconcile any domain changes that were made to the files containing your changes.

Need an Automatic Editor

what I'm looking for is some something similar to the automatically generated "delete" and "Create" functions that are created in MVC. I'd like one that is used to update a record. Preferably it would work much like a gridview (I'd prefer not to mix in ASPX pages), but if it creates a separate form, I'm okay with that too.
The best way to get started is to scaffold a controller. In Visual Studio right-click the Controller folder, Add > Controller, you'll have the option to scaffold a controller with Read/Write actions or using Entity Framework. In the latter case you'll need a create model to base it on before hand.
Once you've done this have a look for the Edit ActionResult, this allows you to edit single records. It's possible to do multiple records but it take a fair bit more work.

ASP.NET MVC EditorTemplate sub folders

I am working on, what I would consider to be, a large ASP.NET MVC website. Currently there are nearly 100 editor templates (all for 1 controller), and this number will grow.
What I want to accomplish is organize my views to make them easier to find and version. This 'version' step is what will make the views multiply as time goes on. you can think of this project as an Question/Answer application, where Exams are created, and can be pulled up later. Basically, for this particular project, the views/EditorTemplates can't really change once in production, so a new copy must be created for future use. References to the old view would still exist, making that exam look and behave the way it did a year ago. Likewise, new exams will automatically pick up the new version of the view, and use that version.
I would like to have this type of structure, but I am up for other ideas.
Views/Shared/EditorTemplates/Common
Views/Shared/EditorTemplates/Common/v2
Views/Shared/EditorTemplates/Common/v3
Views/Shared/EditorTemplates/Department
Views/Shared/EditorTemplates/Department/v2
Note: Even though I will have versioned subdirectories, which implies that I will have multiple versions of the same model and template, the new files will have a unique file name. Also, I am also attempting to use the Razor Generator to compile my views. Not sure if that can be extended to add the additional EditorTemplate search paths or not.
The framework won't look there, use local EditorTemplate folders instead, e.g. Views/Department/EditorTemplates.
Editor templates are located by the view engine, which first looks in ~/Views/{1}/{0}.cshtml and then in ~/Views/Shared/{0}.cshtml.
For example, if the controller is Department and the model is a String, the framework asks for EditorTemplates/String, and the view engine looks in ~/Views/Department/EditorTemplates/String.cshtml and ~/Views/Shared/EditorTemplates/String.cshtml.
Max's answer is a much more elegant and simple answer. If you don't want to do that AND you want a ton of work, you can write your own ViewEngine.

mvc rebuild strongly typed view

is there any way to re-build a strongly typed view when the model class has new fields added?
At present I have not modified the initial generated view so deleting and re-creating is not a problem.
When I start to customize it to my liking I will lose all changes and I was wondering if there was a good way to manage this?
Thanks
ASP.NET MVC offers two types of scaffolding, each with its own advantages:
The first kind of scaffolding is design-time scaffolding, which is done through the Add View dialog and T4 templates. The advantage of this is that the code is entirely generated and you can completely customize it. The disadvantage is that if you change your model you have to regenerate your view (by deleting it and adding back a new one).
The second kind of scaffolding is runtime scaffolding, which is done through the Html.EditorFor() and Html.DisplayFor() methods in your view. The advantage of this is that if your model changes then the scaffolding will be automatically generated at runtime. The disadvantage is that you cannot directly customize the rendering. You can, however, give this scaffolding many hints using DataAnnotations attributes such as [DisplayText], [UIHint], and so forth, so it is quite flexible - but it is not nearly as flexible as being able to 100% customize the rendering.
To customize the rendering of runtime scaffolding (editor templates and display templates) you can find more info on Brad Wilson's blog series.
You can manually add code to your view to reflect any changes in the model.
Default scafolding is there just to give you something.
One thing you might want to look at is t4 templates using wich mvc tooling generates your default views. google for it there are examples of how you can copy those to your project and modify to have mvc generate views you want instead of default ones.

Resources