首页 新闻 会员 周边

EF Core CodeFirst在 .net core中 用 代码生成工具来实现 接口的自动生成,希望大家给予意见,谢谢

0
悬赏园豆:50 [待解决问题]

我想根据EF Core CodeFirst 的实体类来自动生成接口,希望大家给予意见谢谢

想要自动实现的接口为:如下面代码

namespace ProjectCore.Repository.Contract
{
        public interface IDemoRepository<T>:IBaseRepository<T> where T :class,new()
          {

          }
}

往事随灬锋的主页 往事随灬锋 | 初学一级 | 园豆:152
提问于:2018-01-19 17:28
< >
分享
所有回答(2)
0

定义一个泛型的仓储基类实现 泛型的基接口就行了,没必要为每个实体类定义仓储接口。

西漠以西 | 园豆:1675 (小虾三级) | 2018-01-19 17:33

能说的具体点吗?我对这方面了解不深,谢谢

支持(0) 反对(0) 往事随灬锋 | 园豆:152 (初学一级) | 2018-01-21 23:01
0

可以配合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); #>

 

幻影gool | 园豆:257 (菜鸟二级) | 2018-04-24 20:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册