Skip to main content

Intermediate model

IntermediateModel has the same structure as normal Model class, but it is used for one certain purpose - to bypass some Automapper restrictions. The Rengine framework uses Automapper library to automatically map properties between different classes, mostly Model to Model and Model to Entity. Automapper doesn't allow to map circular references during mapping from a database. That's why we need to replace normal Model, that has circular reference, with IntermediateModel. IntermediateModel should be use on database layer only. Example:

public sealed class UserModel : RecordModel
{
public Optional<string>? Username { get; set; }

public Optional<LocalizedStringPropertyModel?>? FirstName { get; set; }

public Optional<LocalizedStringPropertyModel?>? LastName { get; set; }

public Optional<UserModel>? CreatedByUser { get; set; }
}

public sealed class UserIntermediateModel : RecordModel
{
public Optional<string>? Username { get; set; }

public Optional<LocalizedStringPropertyModel?>? FirstName { get; set; }

public Optional<LocalizedStringPropertyModel?>? LastName { get; set; }

public Optional<UserModel>? CreatedByUser { get; set; }
}

There are two user models: normal UserModel and a specific UserIntermediateModel. UserModel is used everywhere in the application. UserIntermediateModel is used for mapping purposes only. We need to introduce intermediate model, because we have a circular reference in CreatedByUser property. Take into consideration, that intermediate model is needed for database mappings only, not for usual in-memory mappings.