When using the Modelsbuilder across projects, you'll avoid circular references if you work in 'Dll' mode instead of 'AppData' - then you can reference the generated dll in your seperate projects.
Check out this visual studio extension, for generating them within VS:
I'd use a Partial class within the same namespace as the generated models to add a property or a method. I think one of the reasons that Partial classes were introduced in c# was to handle this extending a 'code generated' class problem..
... but you can still use inheritance to add extra properties and methods if that makes more sense within your project, but if you do use an extended model for a view, you'll need to hijack the route to tell Umbraco about your enriched class.
so if I understand it, I need to setup modelsbuilder as dll and not as appdata. Then install this extension from your link and run it so it creates a dll from the models.
But how do I run this extension tool and where will this dll be located?
Can't you just run modelsbuilder from the umbraco backoffice under developer section to generate the dll?
If the dll is generated, I think in the main project of my solution, I can reference it from my other projects but then I have a reference in both directions between 2 projects in the same solution.
No need to install the Visual Studio extension!! - (sometimes if you are working in visual studio, it can save time to not have to jump into the backoffice to generate the models, that is all)
So yes, change the configuration to run in 'dll' mode, and then when you visit the backoffice and generate your models, you will have a dll generated in the format:
/bin/Umbraco.Web.PublishedContentModels.dll
you can either copy that dll to your other project, or reference it directly (you will get circular references if you reference the project but not the specific dll!)
Extend ModelsBuilder generated classes
Hi all,
I am using the modelsbuilder as AppData.
I want to extend some of these classes with extra methods. I have a seperated project where I put all the logic.
Question 1:
If I move all models to a seperated project I get exceptions about the modelsbuilder references that are not present.
How can I add modelsbuilder in my other project?
Question 2:
How can I add extra methods or properties to my classes?
Do I create a new partial class of the same type or do I create an extension class for this?
Any help or info is welcome!
/Michaël
Hi Michael
When using the Modelsbuilder across projects, you'll avoid circular references if you work in 'Dll' mode instead of 'AppData' - then you can reference the generated dll in your seperate projects.
Check out this visual studio extension, for generating them within VS:
https://visualstudiogallery.msdn.microsoft.com/3ac3afd4-09db-4c4f-89aa-6ce9e8e7c046
I'd use a Partial class within the same namespace as the generated models to add a property or a method. I think one of the reasons that Partial classes were introduced in c# was to handle this extending a 'code generated' class problem..
... but you can still use inheritance to add extra properties and methods if that makes more sense within your project, but if you do use an extended model for a view, you'll need to hijack the route to tell Umbraco about your enriched class.
regards
Marc
Hi marc,
so if I understand it, I need to setup modelsbuilder as dll and not as appdata. Then install this extension from your link and run it so it creates a dll from the models.
But how do I run this extension tool and where will this dll be located?
Can't you just run modelsbuilder from the umbraco backoffice under developer section to generate the dll?
If the dll is generated, I think in the main project of my solution, I can reference it from my other projects but then I have a reference in both directions between 2 projects in the same solution.
Or am I missing something?
/Michaël
Hi Michael
No need to install the Visual Studio extension!! - (sometimes if you are working in visual studio, it can save time to not have to jump into the backoffice to generate the models, that is all)
So yes, change the configuration to run in 'dll' mode, and then when you visit the backoffice and generate your models, you will have a dll generated in the format:
/bin/Umbraco.Web.PublishedContentModels.dll
you can either copy that dll to your other project, or reference it directly (you will get circular references if you reference the project but not the specific dll!)
regards
Marc
Hi Marc,
I have done it the way you said, but now I got the exception that the class is ambitious.
My class definition:
/Michaël
Ok when you render the dll the classes aren't partial...
Someone that has done this with success?
/Michaël
Michael,
I don't know if this issue is still pertinent for you, but I am successfully generating models into a separate class library:
This forum post gives a little background and hints at a bit of my setup: ModelsBuilder generate models to Umbraco Cloud .Core project
There are some good pages in the ModelsBuilder wiki, too, that might be useful to you: Install and Configure Understand and Extend Control Models Generation
If you are still interested in the topic and want more detail on how I've got my projects set up, I am glad to provide it.
is working on a reply...