Extending Model Classes

Table of contents
  1. 1. Constructor Logic
  2. 2. Things to Note

The modeller generates partial classes, so in general extending the classes with your own functionality is very straightforward.  As well, there's a "Generates Double Derived" setting that allows you to subclass and modify properties.  For constructors, you can use the [PartialClassInitializer] attribute to help you out.

Aside from the basic techniques, the modeller also helps you deal with nested models by copying code automatically when generating.  Suppose you have a model that describes a company structure with employees.

CompanyModel.pmodel

The model generates the following files automatically:

CompanyModel.cs
CompanyModel.partial.cs 

The CompanyModel.cs file contains the generated model code and at this point the CompanyModel.partial.cs file will have nothing.  Now let's say that we include the CompanyModel into a different model.  We end up with the following files.

MyApplicationModel.pmodel
MyApplicationModel.cs
MyApplicationModel.partial.cs 

The main .cs file contains the classes and interfaces generated by the model and the partial.cs file is again empty.

Since the concrete classes are not inherited between models, normally when you extend a class in the company model, for example partial class Employee, the partial code you have written will be  missing when you use MyApplicationModel.  Thankfully, the modeller has a mechanism to deal with this.  When you generate, it also looks for ".user.cs" files to include into combined models.  To utilize it, we would add a file like this to the company model folder.

CompanyModel.Employee.user.cs

Alternatively, you can combine all your classes into one file via CompanyModel.user.cs.

Now, when you generate MyApplicationModel, the partial class and interface code from CompanyModel is included in MyApplicationModel.partial.cs.

So, to summarize.

CompanyModel.pmodel            (Model with Employee class)
CompanyModel.cs                (Generated code with partial Employee class)
CompanyModel.partial.cs        (Empty file)
CompanyModel.Employee.user.cs  (Your partials for Employee and IEmployee)
ApplicationModel.pmodel        (Model that includes CompanyModel.pmodel)
ApplicationModel.cs            (Generated code from application model)
ApplicationModel.partial.cs    (Contains duplicated code from CompanyModel.Employee.user.cs

 

Constructor Logic

Since the model classes have their own constructors, you can't define another constructor in the partial class.  To help you work around this, the modeller will generate code in the regular constructor that calls any method marked with [PartialClassInitializer].

For example, you might write something like this in your .user.cs file:

partial interface ISomething
{
}

partial class SomethingBase
{
    [PartialClassInitializer]
    public virtual void Initialize()
    {
        // This method will be called whne the SomethingBase constructor is invoked.
    }
}

partial class Something
{
}

Things to Note

You can't use a datascope within a PartialClassInitializer (see: PLIB-1004).

 

Tag page
You must login to post a comment.