Created
September 27, 2018 15:19
-
-
Save KarateJB/652fe3397269284f85f07652fccc0299 to your computer and use it in GitHub Desktop.
[Entity Framework DB-first] T4 for creating CRUD service class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" debug="true" hostspecific="true"#> | |
<#@ assembly name="System.Core" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Diagnostics" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ include file="EF.Utility.CS.ttinclude"#> | |
<#@ output extension=".cs" #> | |
<# | |
// Formatting helper for code | |
CodeGenerationTools code = new CodeGenerationTools(this); | |
// object for creating entity information | |
MetadataLoader loader = new MetadataLoader(this); | |
// TODO: NEED TO PROVIDE EDMX FILE LOCATION | |
string inputFile = string.Empty; | |
var currentPath = this.Host.ResolvePath("../Models"); | |
string[] files = System.IO.Directory.GetFiles(currentPath, "*.edmx"); | |
if(files==null || files.Length <=0) | |
return string.Empty; | |
else | |
inputFile = files[0]; | |
// File generation suffix | |
string suffix = "Service"; | |
// Meta data information for the conceptual model | |
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); | |
// Suggested namespace | |
string namespaceName = code.VsNamespaceSuggestion();// + suffix; | |
// File generator according to different section | |
EntityFrameworkTemplateFileManager fileManager = | |
EntityFrameworkTemplateFileManager.Create(this); | |
// Loop through each entity type | |
foreach (EntityType entity in | |
ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) | |
{ | |
// File name for data annotation file | |
string fileName = entity.Name + suffix + ".cs"; | |
// Check for file existence, If it does not | |
// exist create new file for data annotation | |
if (!DoesFileExist(fileName)) | |
{ | |
// Header for file | |
string modelNamespace= string.Concat(code.EscapeNamespace(namespaceName).ToString().Replace(".Service",".Models")); | |
writeHeader(fileManager, modelNamespace); | |
// Create new file | |
fileManager.StartNewFile(fileName); | |
// Add namespaces into file | |
BeginNamespace(namespaceName, code); | |
#> | |
/// <summary> | |
/// <#=code.Escape(entity)#> Service | |
/// </summary> | |
<#=Accessibility.ForType(entity)#> class <#=code.Escape(entity)#>Service<T> : JB.Infra.Util.EF.Service.BaseDalService<T> where T : <#=code.Escape(entity)#> | |
{ | |
public <#=code.Escape(entity)#>Service(DbContext dbContext):base(dbContext) | |
{ | |
} | |
} | |
<# | |
// End namespace | |
EndNamespace(namespaceName); | |
} | |
else | |
{ | |
// Write with original file | |
fileManager.StartNewFile(fileName); | |
this.Write(OutputFile(fileName)); | |
} | |
} | |
fileManager.Process(); | |
#> | |
<#+ | |
// Initialize header | |
void writeHeader(EntityFrameworkTemplateFileManager fileManager, params string[] extraUsings) | |
{ | |
fileManager.StartHeader(); | |
#> | |
using System.Data.Entity; | |
<#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#> | |
<#+ | |
fileManager.EndBlock(); | |
} | |
// Add namespace | |
void BeginNamespace(string namespaceName, CodeGenerationTools code) | |
{ | |
// Generate region | |
CodeRegion region = new CodeRegion(this); | |
// Check for namespace value | |
if (!String.IsNullOrEmpty(namespaceName)) | |
{ | |
#> | |
namespace <#=code.EscapeNamespace(namespaceName)#> | |
{ | |
<#+ | |
// Add indent | |
PushIndent(CodeRegion.GetIndent(1)); | |
} | |
} | |
// End namespace | |
void EndNamespace(string namespaceName) | |
{ | |
if (!String.IsNullOrEmpty(namespaceName)) | |
{ | |
PopIndent(); | |
#> | |
} | |
<#+ | |
} | |
} | |
#> | |
<#+ | |
// Check for file existence | |
bool DoesFileExist(string filename) | |
{ | |
return File.Exists(Path.Combine(GetCurrentDirectory(),filename)); | |
} | |
// Get current folder directory | |
string GetCurrentDirectory() | |
{ | |
return System.IO.Path.GetDirectoryName(this.Host.TemplateFile); | |
} | |
// Get content of file name | |
string OutputFile(string filename) | |
{ | |
using(StreamReader sr = | |
new StreamReader(Path.Combine(GetCurrentDirectory(),filename))) | |
{ | |
return sr.ReadToEnd(); | |
} | |
} | |
// Get friendly name for property names | |
string GetFriendlyName(string value) | |
{ | |
return Regex.Replace(value, | |
"([A-Z]+)", " $1", | |
RegexOptions.Compiled).Trim(); | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment