But I wanted to customize the template further to suit my requirements. One of the requirements was that all the entity classes should inherit from a base class. So to edit the template, first you need to install T4 editor extension (download it from here). This editor provides intelli-sense and syntax-hilighting.
Currently the entities generated from the model inherit from other entities only if that inheritance is defined in the model. For example, Employee inherits from Person. This is how the entity decleration looks in the original template:
partial class <#=code.Escape(entity)#><#=code.StringBefore(" : " ,code.Escape(entity.BaseType))#>
This ensures that the Employee class inherits from Person, or that any derived entity inherits from its base, in the generated code. But we now want to have every entity inherit from the new base class (BaseModel) unless the entities are already deriving from another base entity. In other words, Person should inherit directly from BaseModel, while Employee continues to inherit from Person (and therefore indirectly inherits BaseModel).
First, you’ll need to add the method, BaseTypeName, as shown below. Look for IsReadWriteAccessibleProperty method and add the following method above it.
string BaseTypeName(EntityType entity, CodeGenerationTools code)
{
return entity.BaseType == null ? "Namespace.BusinessEntities.BaseModel" :
code.Escape(entity.BaseType);
}
Now you can modify the code shown before, where the entity declaration is made to call the BaseTypeName method. As result, the entity will inherit from either BaseModel or its base type as defined in the model.
partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", BaseTypeName(entity, code))#>
Ensure the entities can locate the BaseModel class. Once you save the template, the entities should get re-generated again based on our customised template.
No comments:
Post a Comment