|
OpenAccess ORM version
|
2011.2.908.1
|
| .NET version |
3.5+ |
| Visual Studio version |
2010
|
| Programming language |
C# and VB |
PROJECT DESCRIPTION
Sample Code Generation files for creating a Domain Model in which the entities all inherit from a base class.
It can be helpful to have all of your entities inherit from a base class in order to create some basic functionality for every entity or to write constrained generic functions. In this code library sample, the base class does not have any content, but you can create another partial class with the same name in order to add functionality. Do not modify the EntitiesBase.generated.[cs/vb] or it will be overwritten.
Attached is a project with all of the code generation files needed to implement this for C# and VB. The code generation files are in the root folder and there are sample projects to demonstrate it. Please note that you will need to update the location before updating the rlinq files. To do this, open the domain model with an XML editor, find the following XML paths and update the node values (representing directories and files) to match your local ones:
DomainModel \ ModelSettings \ CodeGenerationSettings \ OutputPath - the project folder
DomainModel \ ModelSettings \ CodeGenerationSettings \ CustomTemplateFileName - location of the DefaultTemplate[Language].tt file
DomainModel \ ModelSettings \ SchemaUpdateSetting \ DeploymentDirectory - the project folder
Below are the individual functions updated in each
C# template file:
General.ttinclude
const string entitiesBaseClass = "EntitiesBase"; //change this to whatever you would like the base class to be named
private string GetClassSignature(Telerik.OpenAccess.CodeGeneration.CodeClass codeClass)
{
string[] modifierParts = new string[6];
modifierParts[0] = codeClass.AccessModifierString;
modifierParts[1] = codeClass.InheritanceModifierString;
modifierParts[2] = codeClass.PartialReservedWordString;
modifierParts[3] = codeClass.ClassReservedWordString;
modifierParts[4] = codeClass.Name;
modifierParts[5] = codeClass.ImplementedTypesString;
string classSignature = string.Empty;
if (this.generatedContext != codeClass && entitiesBaseClass != null && codeClass.Name != entitiesBaseClass)
{
if (string.IsNullOrEmpty(modifierParts[5]))
modifierParts[5] = ": " + entitiesBaseClass;
}
for (int i = 0; i < modifierParts.Length; i++)
{
modifierParts[i] = modifierParts[i].Trim();
if (string.IsNullOrEmpty(modifierParts[i]))
continue;
classSignature = string.Concat(classSignature, " ", modifierParts[i]);
}
classSignature = classSignature.Trim();
return classSignature;
}
MainCS.ttinclude
//this code is in the root of the file:
foreach(Telerik.OpenAccess.CodeGeneration.CodeNamespace @namespace in this.codegenModel.Namespaces)
{
if (@namespace.Classes != null && @namespace.Classes.Count > 0 && entitiesBaseClass != null)
{
//this.Warning("Generating Base Class");
GenerateBaseClass(@namespace, @namespace.Classes[0]);
}
foreach(Telerik.OpenAccess.CodeGeneration.CodeClass @class in @namespace.Classes)
{
this.GeneratePerEntityFileBlock(@class as Telerik.OpenAccess.CodeGeneration.CodeElement, @namespace, defaultExtension);
}
foreach(Telerik.OpenAccess.CodeGeneration.CodeInterface @interface in @namespace.Interfaces)
{
if (@interface.IsInterface)
{
this.GeneratePerEntityFileBlock(@interface as Telerik.OpenAccess.CodeGeneration.CodeElement, @namespace, defaultExtension);
}
}
}
//add at end:
private void GenerateBaseClass(Telerik.OpenAccess.CodeGeneration.CodeNamespace ns, Telerik.OpenAccess.CodeGeneration.CodeClass firstEntity)
{
this.templateContext.StartNewFileBlock(Path.ChangeExtension(entitiesBaseClass, defaultExtension), ns.Name);
string entityNamespace = string.Empty;
entityNamespace = !string.IsNullOrEmpty(ns.Name) ? ns.Name : this.GetEntityNamespace(firstEntity as Telerik.OpenAccess.CodeGeneration.CodeInterface);
if(this.generateMultipleFiles)
{
this.GenerateUsings(firstEntity as Telerik.OpenAccess.CodeGeneration.CodeInterface);
}
if (!String.IsNullOrEmpty(entityNamespace) && entityNamespace != this.metaModel.RootNamespace)
{#>
namespace <#=entityNamespace #>
{
<#+
this.PushIndent("\t");
}
#>
public abstract partial class <#=entitiesBaseClass #>
{
}
<#+
if (!String.IsNullOrEmpty(entityNamespace) && entityNamespace != this.metaModel.RootNamespace)
{
PopIndent();
#>
}
<#+ }
this.templateContext.EndBlock();
}
Below are the individual functions updated in each
VB template file:
General.ttinclude
const string entitiesBaseClass = "EntitiesBase";
private void GenerateClassSignature(Telerik.OpenAccess.CodeGeneration.CodeClass codeClass)
{
string[] modifierParts = new string[6];
modifierParts[0] = codeClass.AccessModifierString;
modifierParts[1] = codeClass.InheritanceModifierString;
modifierParts[2] = codeClass.PartialReservedWordString;
modifierParts[3] = codeClass.ClassReservedWordString;
modifierParts[4] = codeClass.Name;
modifierParts[5] = codeClass.ImplementedTypesString;
string classSignature = string.Empty;
for (int i = 0; i < modifierParts.Length - 1; i++)
{
modifierParts[i] = modifierParts[i].Trim();
if (string.IsNullOrEmpty(modifierParts[i]))
continue;
classSignature = string.Concat(classSignature, " ", modifierParts[i]);
}
classSignature = classSignature.Trim();
this.WriteLine(classSignature);
PushIndent("\t");
if (this.generatedContext != codeClass && entitiesBaseClass != null && codeClass.Name != entitiesBaseClass)
{
this.WriteLine("Inherits " + entitiesBaseClass);
}
this.WriteLine(modifierParts[5]);
PopIndent();
}
MainCs.ttinclude
//update in root of file:
foreach(Telerik.OpenAccess.CodeGeneration.CodeNamespace @namespace in this.codegenModel.Namespaces)
{
//this.Warning(@namespace.Classes.Count.ToString());
//this.Warning(entitiesBaseClass);
if (@namespace.Classes != null && @namespace.Classes.Count > 0 && entitiesBaseClass != null)
{
//this.Warning("Generating Base Class");
GenerateBaseClass(@namespace, @namespace.Classes[0]);
}
foreach(Telerik.OpenAccess.CodeGeneration.CodeClass @class in @namespace.Classes)
{
this.GeneratePerEntityFileBlock(@class as Telerik.OpenAccess.CodeGeneration.CodeElement, @namespace, defaultExtension);
}
foreach(Telerik.OpenAccess.CodeGeneration.CodeInterface @interface in @namespace.Interfaces)
{
if (@interface.IsInterface)
{
this.GeneratePerEntityFileBlock(@interface as Telerik.OpenAccess.CodeGeneration.CodeElement, @namespace, defaultExtension);
}
}
}
//add:
private void GenerateBaseClass(Telerik.OpenAccess.CodeGeneration.CodeNamespace ns, Telerik.OpenAccess.CodeGeneration.CodeClass firstEntity)
{
this.templateContext.StartNewFileBlock(Path.ChangeExtension(entitiesBaseClass, defaultExtension), ns.Name);
string entityNamespace = string.Empty;
entityNamespace = this.GetEntityActualNamespace(firstEntity as Telerik.OpenAccess.CodeGeneration.CodeInterface);
if(this.generateMultipleFiles)
{
this.GenerateUsings(firstEntity as Telerik.OpenAccess.CodeGeneration.CodeInterface);
}
if (!String.IsNullOrEmpty(entityNamespace) && entityNamespace != this.metaModel.RootNamespace)
{#>
Namespace <#=entityNamespace #>
<#+
this.PushIndent("\t");
}
#>
Partial Public MustInherit Class <#=entitiesBaseClass #>
End Class
<#+
if (!String.IsNullOrEmpty(entityNamespace) && entityNamespace != this.metaModel.RootNamespace)
{#>
End Namespace
<#+ }
this.templateContext.EndBlock();
}
Hope this helps.