Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 13, 2016 @ 07:50
    Michaël Vanbrabandt
    0

    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

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 13, 2016 @ 11:03
    Marc Goodson
    0

    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

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 14, 2016 @ 09:28
    Michaël Vanbrabandt
    0

    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

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 14, 2016 @ 16:19
    Marc Goodson
    0

    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

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 14, 2016 @ 18:04
    Michaël Vanbrabandt
    0

    Hi Marc,

    I have done it the way you said, but now I got the exception that the class is ambitious.

    My class definition:

    public partial class SubCategory : PublishedContentModel
    {
        public SubCategory(IPublishedContent content) : base(content)
        {
        }
    
        public bool IsNew { get; set; }
    }
    

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 14, 2016 @ 18:06
    Michaël Vanbrabandt
    0

    Ok when you render the dll the classes aren't partial...

    Someone that has done this with success?

    /Michaël

  • Jeff Subat 28 posts 159 karma points
    Feb 04, 2017 @ 18:27
    Jeff Subat
    1

    Michael,

    I don't know if this issue is still pertinent for you, but I am successfully generating models into a separate class library:

    • I extend the model classes there using partials.
    • I use AppData as the ModelsMode.
    • I installed the ModelsBuilder Nuget package into my class library to resolve missing references (per your Question 1, I think?)

    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.

Please Sign in or register to post replies

Write your reply to:

Draft