Table of contents
  1. 1. Entity Instantiation

The modeller allows you to include one model within another model and define new relationships and properties in an additive manner.  Multiple models can include the same base model.  For example, many models could include a user model and then refer to the user from different classes.  Each model can still be used on its own but the augmented properties will not be available unless using the combined model.

Because each individual model is available by itself, we need to know which properties are available at compile time.  The available properties are defined by an set of interfaces for that model, one for each class.  The actual concrete classes underlying the interfaces changes depending on which full combined model you're using.  The interfaces inherit from each other, so you can pass them around freely between sub modules and their parents.

Here's a diagram that demonstrates the relationships.  Note the interface inheritance on the left, but the lack of inheritance of concrete classes on the right.  The relationships are designed this way due to the lack of multiple inheritance in .NET.  Note that the functionality in the concrete classes must be duplicated between the different models.  The modeller provides functionality to handle that duplication automatically.

SimpleIncludedModel.jpg

Entity Instantiation

Because the functionality in the concrete classes is duplicated across models, a concrete class that results from the generation of a nested model will not be the same class that is generated by the basic model.

In the class diagram above, for example, when the basic model is generated, it will produce classes that include
Basic.Namespace.Company
However, when the extended model is generated, it will produce classes that include
Extended.Namespace.Company
With the Basic.Namespace.Company class not appearing at all in the model.

As a result of this, if there is any reusable code that utilizes a nested model, it cannot refer to any concrete classes, only their interfaces.

This would seem to make instantiation a non-trivial exercise. For example, if we have shared code that creates a new company, one would be tempted to write:

var company = new Company(){
    CompanyName = "Pleasant Solutions"
};

However, this code will not execute at runtime in the nested model, as the persistance layer expects an Extended.Namespace.Company rather than the Basic.Namespace.Company that was passed. To resolve this, use EntityFactory.Create<>(). (Actually, scope.Repository.New<IEntity>() is the perferred pattern if you're using DataRepositories because it allows for different concrete types to be instantiated for different database connections.)

For example:

var company = EntityFactory.Create<ICompany>();
company.CompanyName = "Pleasant Solutions";
Tag page
You must login to post a comment.