Trên thực tế, sử dụng các lớp học phần để thực hiện giao diện thực sự giải quyết vấn đề của bạn nếu bạn có chỉ là một vài bảng (thực thể) mà bạn muốn map.But khi bạn cần thực hiện điều này với nhiều thực thể hơn hoặc thậm chí nhiều giao diện hơn, bạn có thể sử dụng mẫu T4 được EF sử dụng để tạo các lớp và sau đó thực hiện giao diện trực tiếp tại các lớp được tạo tự động POCO, không cần công việc thủ công.
Tôi đã tự làm điều này, chúng tôi đã tạo giao diện ISimpleAuditable, rất giống với giao diện của bạn và T4 kiểm tra xem bảng có đúng trường hay không và nếu có, nó sẽ thêm mã triển khai giao diện vào lớp.
Chúng tôi đã tạo ra một tập tin Include.tt có mã như thế này:
public static string GetTypeInterfaces (EntityType tổ chức) { chuỗi giao diện = String.Empty;
if(IsNome(entity))
{
if(IsIdentifiableNumeric(entity))
interfaces = "IEntity<int>";
if(IsIdentifiableText(entity))
interfaces = "IEntity<string>";
if (interfaces == String.Empty)
interfaces = "INome";
if(IsSimpleAuditable(entity))
if (interfaces==String.Empty)
interfaces = "ISimpleAuditable<string>";
else
interfaces += ", ISimpleAuditable<string>";
}
else
{
if(IsIdentifiableNumeric(entity))
interfaces = "IIdentifiable<int>";
if(IsIdentifiableText(entity))
interfaces = "IIdentifiable<string>";
if(IsSimpleAuditable(entity))
if (interfaces==String.Empty)
interfaces = "ISimpleAuditable<string>";
else
interfaces += ", ISimpleAuditable<string>";
}
if (interfaces != string.Empty)
if (entity.BaseType !=null)
interfaces = string.Format(", {0}", interfaces);
else
interfaces = string.Format(": {0}", interfaces);
return interfaces;
}
Mã T4 trông giống như sau:
<#@ template language="C#" debug="true" hostspecific="true"#>
<#@ import namespace="System.Diagnostics" #>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ include file="Winsys.Sandstone.Data.ttinclude"#><#@
output extension=".cs"#><#
const string inputFile = @"Winsys.Sandstone.Data.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
var itemCollection = new EdmMetadataLoader(textTransform.Host, TextTransform.Errors).CreateEdmItemCollection(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
{
return string.Empty;
}
WriteHeader(codeStringGenerator, fileManager);
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
#>
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#
var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
var complexProperties = typeMapper.GetComplexProperties(entity);
if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
{
#>
public <#=code.Escape(entity)#>()
{
<#
foreach (var edmProperty in propertiesWithDefaultValues)
{
#>
this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
<#
}
foreach (var navigationProperty in collectionNavigationProperties)
{
#>
this.<#=code.Escape(navigationProperty)#> = new List<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
<#
}
foreach (var complexProperty in complexProperties)
{
#>
this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
<#
}
#>
}
<#
}
var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
foreach (var edmProperty in simpleProperties)
{
#>
<#=codeStringGenerator.Property(edmProperty)#>
<#
}
}
if (complexProperties.Any())
{
#>
<#
foreach(var complexProperty in complexProperties)
{
#>
<#=codeStringGenerator.Property(complexProperty)#>
<#
}
}
var navigationProperties = typeMapper.GetNavigationProperties(entity);
if (navigationProperties.Any())
{
#>
<#=WinsysGenerator.GetSimpleAuditable(entity)#>
<#
foreach (var navigationProperty in navigationProperties)
{
#>
<#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
}
}
#>
Lưu ý rằng đây không phải là toàn bộ T4, nhưng bạn có thể lấy ý tưởng
Nguồn
2013-06-18 15:37:49
http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete -type-tpc-and-select-strategy-guidelines.aspx –