In this tutorial, you will learn how you can use the Umbraco ModelBuilder using DLL mode with multiple class libraries. Out of the box, the ModelsBuilder, when set in DLL mode, will generate strongly-typed models within your web project (if you are not up-to-speed with ModelsBuilder I suggest you start here)

Architecturally, this can cause a problem. It's a generally accepted principle that tight-coupling and monoliths should be avoided. One way you can split your code up is by using class libraries. Instead of adding all your code within your web application project, your code is separated into one or more class libraries. If you follow a domain-driven design principle and within your website you have a backend membership portal, with a domain-driven design you could put all your membership related code within its own class library so it's grouped together. If you try this and you're using the DLL mode, you will soon notice a circular dependency issue. Your document types models are in the web application, your membership class library needs to access them, so you add a project reference from the membership portal to your web application. If you ever want your membership code to appear on the website, the web application project will need to reference the membership library. If you try this, Visual Studio will complain and prevent you from doing this, the answer...

The way around this is to config ModelBuilder to generate the models into a separate class library, just for the ModelBuilder files. You can then reference the models in both web application and membership portal. Using a third library breaks the circular dependency issue and you can then access your strongly-typed models throughout your code base. You will no longer be limited to only using your models within your web application and you will be able to create a more loosely coupled system.

Hopefully, you understand the benefits by now. Let's get on with making it happen.

Create A Class Library

In your solution create an empty class library. You can call it whatever you want, however, I tend to call mine 'Umbraco.Web.PublishedContentModels' to match the default namespace the ModelsBuilder generates.

In order for the models to compile you will need to make sure you add a reference to System.web:

You will also need to make sure both Nuget packages, Umbraco.Core and Umbraco.ModelsBuilder are installed on the project:

Configuring ModelBuilder

In your main web.config remove the existing model builder settings:

Replace with this:

That should be it.

Generate Models

Within your Umbraco backend, go to the 'developer' section, click on the 'Models Builder' tab.

Click generate models.

Now in your models class library, ensure the show hidden file option is selected then click the reload button. You should see a lot of class files with a .generated.cs extension. Add these to your project:

Within Visual Studio, select all these files, right-click and select 'Include in project'. If everything goes well you should now be able to build the project successfully. You can now reference this class library in your website project, your membership project and anywhere else you want to.