Copied to clipboard

Flag this post as spam?

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


  • Nathan Woulfe 368 posts 1383 karma points MVP 2x c-trib
    1 week ago
    Nathan Woulfe
    2

    ModelsBuilder best practices

    Hi all, wanting to start some conversation around MB and how to best use it in large, complex solutions.

    In a simple site, MB works great without any extra effort - turn it on, enjoy fresh models built to order. I'm wrestling with a v7 site with 150+ content types (including NC elements and DTGE components), and trying to determine the optimum way to use MB, and get the most benefit from it.

    So far, I'm approaching it like so:

    • DLL mode
    • Building models into a separate project
    • referencing that project as required

    Which works, to a point. Given I have 150 or so models, many of which I want to extend with custom data, it's not really practical to dump a heap of partials in one folder alongside the generated models.

    That's query 1 - how are people configuring MB in their solutions?

    All of my grid components share a common base class, to add some generic helpers and useful bits. Similar to above, if I tell MB to use that base class, which then inherits from PublishedContentModel, I'm back to lots of partials in the same location as the generated files.

    I'd love to be able to generate multiple ModelsBuilder DLLs in the one project, or use a different base class in different namespaces.

    I could create a view model inheriting the MB models, and maybe change my base class to an interface, but then I'm having to implement the interface across 20+ classes, which isn't cool. In that case, I'd be better off just building my own models, which I want to avoid.

    Query 2 - base classes vs interfaces vs view models. How are people extending MB models?

    I'm really just looking for any and all input and ideas around ModelsBuilder and how to make it awesome. Fire away...

  • Carl 63 posts 85 karma points
    1 week ago
    Carl
    0

    I work in the same way, I have a library project where I generate the modes, I then keep my partials in the same folder for those models that I extend. It's the cleanest way I've found to work so far. I'm aware this isn't answering necessarily all of your questions, but its confirmation for me too that perhaps the approach I'm taking isn't the worst, so I offer rhe reciprocation of that to you :)

  • Søren Kottal 432 posts 2625 karma points MVP c-trib
    1 week ago
    Søren Kottal
    0

    I can follow you about having partial classes messed up with all the generated ones, that is quite messy.

    I am using the AppData mode, generating models into another project, which is then referred by my web-project.

    I don't like having seperate view models for views (because if you have one, you should have everyone for consistency, but not every model needs extension in that way). So I add properties to partial classes, which gets set by a hijacking controller.

    I really miss two things in this workflow. 1. ModelsBuilder being able to read partial classes from another folder, so I don't have to mix my own partials with the generated ones.

    1. Visual Studio being able to just include every cs-file in a folder automatically.
  • Mads Krohn 211 posts 501 karma points c-trib
    1 week ago
    Mads Krohn
    3

    I have a structure like this:

    Models/
      - Generated/  
        - Page.generated.cs  
        - ContentPage.generated.cs  
      - Extended/  
        - ContentPage.extended.cs
    

    Then this configuration in web.config for MB:

    <add key="Umbraco.ModelsBuilder.ModelsMode" value="AppData" />
    <add key="Umbraco.ModelsBuilder.ModelsNamespace" value="SomeProject.Models" />
    <add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/Models/Generated" />
    

    The generated models will end up like

    namespace SomeProject.Models
    {
        [PublishedContentModel("ContentPage")]
        public partial class ContentPage : PublishedContentModel, ICompositionPage
    

    Then I change the namespace on the extended model, so it matches the generated
    And boom, its all separated out :P

  • Søren Kottal 432 posts 2625 karma points MVP c-trib
    1 week ago
    Søren Kottal
    0

    Seems like I need to try again - I never got it working having them in different places :)

  • Mads Krohn 211 posts 501 karma points c-trib
    1 week ago
    Mads Krohn
    0

    Definitely try again, it's working just fine my end at least :)

  • Nathan Woulfe 368 posts 1383 karma points MVP 2x c-trib
    1 week ago
    Nathan Woulfe
    0

    I thought I'd tried that, or something similar. That's what I'm looking for - nice separation between generated and extended, everything in a separate project.

    Will have another crack at it tomorrow

  • Mads Krohn 211 posts 501 karma points c-trib
    1 week ago
    Mads Krohn
    0

    Great! Do hit me up, if you run into issues.

  • Nathan Woulfe 368 posts 1383 karma points MVP 2x c-trib
    1 week ago
    Nathan Woulfe
    0

    I still get Intellisense errors using this setup - my extended partial doesn't have access to properties on the generated class, unless both are included in the solution...

  • Mads Krohn 211 posts 501 karma points c-trib
    1 week ago
    Mads Krohn
    0

    yeah, VS needs to no about them, so they of course have to be included in the solution :)

  • Carl 63 posts 85 karma points
    1 week ago
    Carl
    0

    @Mads I like this approah too - and it should just be a namespace to get it working. I'll shift to this approach going forward - it's a small change but improves clarity

  • Mads Krohn 211 posts 501 karma points c-trib
    1 week ago
    Mads Krohn
    0

    Definitely does :)

  • Stephen 766 posts 2260 karma points c-trib
    1 week ago
    Stephen
    0

    Comments...

    About Files

    For any kind of real-life, largish project, I have always used the Models Builder Visual Studio Extension. It builds models, and imports them right into a Visual Studio project. Generated files are grouped under a Models.mb placeholder file, so even though they are in the same folder as your own partials, in Visual Studio they can be hidden by collapsing Models.mb.

    That being said, being able to read partial classes from another folder... That should not be too difficult to offer it as an option - file a feature request on GitHub!

    Also note this: at the moment, you can have partials in another folder, but they would not get sent to Models Builder and attributes in these files will be ignored when generating.

    Any other mode (AppData, Dll) has its own problems. With AppData, Visual Studio does not include the files automatically, and there it little we can do about it - actually, Visual Studio does include files automatically for new .NET Core SDK projects, but not for traditional Umbraco projects.

    About Base Classes

    Can you elaborate on the situation with having to implement a base class in many places? Or an interface? There's already a feature request to configure the base class per alias... maybe we need more?

    Oh... and, there's work in progress to open the builder, so that you could tweak every step of the generation - including determining the base class - and adjust it to your needs.

  • Nathan Woulfe 368 posts 1383 karma points MVP 2x c-trib
    1 week ago
    Nathan Woulfe
    0

    Re base classes - I want to use a particular base class on a subset of my models. If that's already possible, awesome. An example would be even more awesome!

    I couldn't find how to set that for some models, only for all.

    EDIT: base class is fine, I meant I'd like to generate models into different namespaces.

  • Stephen 766 posts 2260 karma points c-trib
    1 week ago
    Stephen
    0

    Have created this issue on Models Builder's GitHub to discuss how models could be generated in different namespaces.

    Probably can be done, but need to understand how the decision of which namespace to use would be made. Want to comment on the issue?

Please Sign in or register to post replies

Write your reply to:

Draft