Last active
August 1, 2017 18:55
-
-
Save AlbertoMonteiro/daca01765c7ac4f630a2374a8f3db96a to your computer and use it in GitHub Desktop.
Simple generator of EntityTypeConfiguration
This file contains hidden or 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
public static class FluentEFTypeMappingGenerator | |
{ | |
private static bool _theKeyIsAlrearyWriten; | |
private static XDocument _documentationXml; | |
public static string GenerateForClass<T>() | |
{ | |
var mainType = typeof(T); | |
var assemblyLocation = mainType.Assembly.Location; | |
var xmlFileDocumentation = Path.ChangeExtension(Path.Combine(Path.GetDirectoryName(assemblyLocation), Path.GetFileName(assemblyLocation)), "xml"); | |
if (File.Exists(xmlFileDocumentation)) | |
_documentationXml = XDocument.Load(xmlFileDocumentation); | |
_theKeyIsAlrearyWriten = false; | |
var sb = new StringBuilder(); | |
foreach (var propertyInfo in mainType.GetProperties()) | |
{ | |
if (!propertyInfo.CanRead || !propertyInfo.CanRead) continue; | |
var propertyType = propertyInfo.PropertyType; | |
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) | |
propertyType = propertyType.GetGenericArguments()[0]; | |
HandleStructs(sb, propertyType, propertyInfo.Name, mainType.FullName); | |
sb.AppendLine(); | |
} | |
return sb.ToString(); | |
} | |
private static void HandleStructs(StringBuilder sb, Type propertyType, string propertyName, string mainTypeFullName) | |
{ | |
if (propertyType == typeof(long) && !_theKeyIsAlrearyWriten) | |
{ | |
sb.AppendLine($"HasKey(x => x.{propertyName});"); | |
sb.AppendLine(); | |
} | |
sb.AppendLine($"Property(x => x.{propertyName})"); | |
var columnName = "DEFINE NAME"; | |
var strLength = "35"; | |
var pres = "20"; | |
var @decimal = "2"; | |
if (_documentationXml != null) | |
{ | |
var memberNode = _documentationXml.Root.DescendantsAndSelf($"member").FirstOrDefault(e => e.FirstAttribute.Value == $"P:{mainTypeFullName}.{propertyName}"); | |
var pattern = @"(?<propName>(\d\.?)+)\s*\n*(Texto com (?<strLength>\d+) caracteres)?\s*\n*(Decimal com (?<pres>\d+) dígitos e (?<deci>\d+) casas após a vírgula.)?"; | |
var text = memberNode?.Value.Trim(); | |
if (!string.IsNullOrWhiteSpace(text)) | |
{ | |
var match = Regex.Match(text, pattern); | |
if (match.Success) | |
{ | |
columnName = match.Groups["propName"].Value; | |
strLength = !string.IsNullOrWhiteSpace(match.Groups["strLength"].Value) ? match.Groups["strLength"].Value : strLength; | |
pres = !string.IsNullOrWhiteSpace(match.Groups["pres"].Value) ? match.Groups["pres"].Value : pres; | |
@decimal = !string.IsNullOrWhiteSpace(match.Groups["deci"].Value) ? match.Groups["deci"].Value : @decimal; | |
} | |
} | |
} | |
sb.Append($" .HasColumnName(\"{columnName}\")"); | |
if (propertyType == typeof(long) && !_theKeyIsAlrearyWriten) | |
{ | |
sb.Append($"\n .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)"); | |
_theKeyIsAlrearyWriten = true; | |
} | |
if (propertyType == typeof(string)) | |
sb.Append($"\n .HasMaxLength({strLength})"); | |
if (propertyType == typeof(decimal)) | |
sb.Append($"\n .HasPrecision({pres}, {@decimal})"); | |
sb.AppendLine($";"); | |
} | |
} |
This file contains hidden or 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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(FluentEFTypeMappingGenerator.GenerateForClass<Entity_100_02>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment