我想根据EF Core CodeFirst 的实体类来自动生成接口,希望大家给予意见谢谢
想要自动实现的接口为:如下面代码
namespace ProjectCore.Repository.Contract
{
public interface IDemoRepository<T>:IBaseRepository<T> where T :class,new()
{
}
}
定义一个泛型的仓储基类实现 泛型的基接口就行了,没必要为每个实体类定义仓储接口。
能说的具体点吗?我对这方面了解不深,谢谢
可以配合t4模板做代码生成
<#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.xml" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.Data.SqlClient" #> <#@ import namespace="System.Data" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@include file="..\Unreal.CoDragon.Data\Manager.ttinclude"#> <# var manager = Manager.Create(Host, GenerationEnvironment); string solutionsPath = Host.ResolveAssemblyReference("$(SolutionDir)"); string projectPath = Host.ResolveAssemblyReference("$(ProjectDir)"); #> <# var tables = new List<string>(); var regex = new System.Text.RegularExpressions.Regex(@"EntityType=""CoDragonModel\.(.+?)"""); var matchs = regex.Matches(System.IO.File.ReadAllText(projectPath + "../Unreal.CoDragon.Data/Model.edmx.diagram")); foreach (System.Text.RegularExpressions.Match match in matchs) { tables.Add(match.Groups[1].Value); } #> using System; using System.Collections.Generic; using Unreal.CoDragon.Data; using Unreal.CoDragon.Interface; namespace Unreal.CoDragon.Repository.Repositorys { <# foreach(var tb_name in tables) { #> /// <summary> /// <#=tb_name#> 仓库 /// </summary> public partial class <#=tb_name#>Repository { } <# } #> } <# foreach(var tb_name in tables) { manager.StartNewFile(@"Repositorys\" + tb_name + "Repository.cs"); #> using System; using System.Collections.Generic; using Unreal.CoDragon.Data; using Unreal.CoDragon.Interface; namespace Unreal.CoDragon.Repository.Repositorys { /// <summary> /// <#=tb_name#> 仓库 /// </summary> public partial class <#=tb_name#>Repository : RepositoryBase<<#=tb_name#>>,I<#=tb_name#>Repository { } } <# } #> <# manager.Process(true); #>