-
Notifications
You must be signed in to change notification settings - Fork 3
3. How does it work
The Model and ModelFactory generation is a 2-step approach. First a typelib.config file is generated which contains information about the Umbraco DataType configuration and the Document Type configuration. The T4 templates will use this typelib.config file as input to generate the c# code. The typelib.config file is created in the App_Data folder.
The generation of the typelib.config file is best triggered by an Umbraco event. The umbraco.content.AfterRefreshContent event is a good candidate. This event is fired when you republish the entire site. After installing the UmbCodeGen package you have access to the DocumentTypeLibrary class which can be found in the UmbCodeGen.TypeLib namespace (located in the assembly UmbCodeGen.dll).
var typeLibrary = new DocumentTypeLibrary();
typeLibrary.Build();
The Build() method will generate the typelib.config file. Once the typelib.config file is generated, you can run the T4 templates by right clicking the ModelGen.tt file and select the Run Custom Tool option. The T4 templates are installed in the folder Models/Generated. You can rename this folder or move the T4 templates into another existing folder if you like, but it is recommended that the generated models are placed in a folder named Generated. The Name space of all generated models will be based on the folder structure, just as the default behavior of Visual Studio, except that the name space will not end with .Generated. This part will be stripped off so that you can add your custom partial classes directly into the Models folder and both classes will use the same name space.
Run both the ModelGen and the ModelFactory T4 template to generate the models and the ModelFactory.
The ModelGen T4 template will generate a strong typed model (POCO) for each Umbraco Document Type. All models inherit from the same abstract ModelBase and are defined as partial classes. Each property must be decorated with a PropertyAttribute specifying the property alias. The ModelFactory uses these attributes and reflection to map the Node properties to model properties. Example of a model:
[DocumentType("Movies")]
public partial class Movies : ModelBase
{
[Property("movies_header")]
public string Header { get; set; }
[Property("movies_intoText")]
public string IntoText { get; set; }
};
The ModelFactory class has 2 public static mehods:
public static T CreateModel<T>(INode node) where T : class, new()
public static void FillModel(object model, INode node)
The first method will create an instance of type T and fill the class properties with node values. The second method does the same except that you must pass an instance of the model which was created before.
The model should have public properties (public fields are not yet supported) that must be decorated with the PropertyAttribute. This attribute must define the alias of the node property. See the Movies model example above.
The DocumentType class attribute is not required. However, each generated class has one so it’s easy to see from which Umbraco Document Type the model originates.