重用DbContext接口而不进行多重继承

时间:2015-06-22 01:35:10

标签: c# entity-framework

我为我的数据库中的所有表和存储过程定义了一个接口。我们的想法是,这个界面将用于不同 DbContexts的多个解决方案,这些解决方案具有不同的继承层次结构,并且没有共同的祖先。

 public interface IMyDbContext : IDisposable
 {
    IDbSet<Reference_CountryModel> Reference_Country { get; set; }
    List<UspGetCountryListReturnDto> UspGetCountryList(out int aProcResult);
    List<UspGetBrandByWebsiteListReturnDto> UspGetBrandByWebsiteList(DbContext aDbContext, short? aWebsiteId, out int aProcResult)
 }

所以我们假设我的第一个DB上下文是:

public class MyFirstDbContext : DbContext, IMyDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new Reference_CountryConfiguration()); 
    }

    public IDbSet<Reference_CountryModel> Reference_Country { get; set; }
    public List<UspGetCountryListReturnDto> UspGetCountryList(out int aProcResult)
    {
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetCountryListReturnDto> procResultData = Database.SqlQuery<UspGetCountryListReturnDto>("EXEC @procResult = [dbo].[usp_GetCountryList] ", procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }

    public List<UspGetBrandByWebsiteListReturnDto> UspGetBrandByWebsiteList(DbContext aDbContext, short? aWebsiteId, out int aProcResult)
    {
        SqlParameter websiteIdParam = new SqlParameter {ParameterName = "@WebsiteID", SqlDbType = SqlDbType.SmallInt, Direction = ParameterDirection.Input, Value = aWebsiteId.GetValueOrDefault()};
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetBrandByWebsiteListReturnDto> procResultData =
            aDbContext.Database.SqlQuery<UspGetBrandByWebsiteListReturnDto>("EXEC @procResult = [dbo].[usp_GetBrandByWebsiteList] @WebsiteID", websiteIdParam, procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }
}

和我的第二个数据上下文(AbpDbContext来自DbContext)是:

public class MySecondDbContext : AbpDbContext, IMyDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new Reference_CountryConfiguration()); 
    }

    public IDbSet<Reference_CountryModel> Reference_Country { get; set; }
    public List<UspGetCountryListReturnDto> UspGetCountryList(out int aProcResult)
    {
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetCountryListReturnDto> procResultData = Database.SqlQuery<UspGetCountryListReturnDto>("EXEC @procResult = [dbo].[usp_GetCountryList] ", procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }
     public List<UspGetBrandByWebsiteListReturnDto> UspGetBrandByWebsiteList(DbContext aDbContext, short? aWebsiteId, out int aProcResult)
    {
        SqlParameter websiteIdParam = new SqlParameter {ParameterName = "@WebsiteID", SqlDbType = SqlDbType.SmallInt, Direction = ParameterDirection.Input, Value = aWebsiteId.GetValueOrDefault()};
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetBrandByWebsiteListReturnDto> procResultData =
            aDbContext.Database.SqlQuery<UspGetBrandByWebsiteListReturnDto>("EXEC @procResult = [dbo].[usp_GetBrandByWebsiteList] @WebsiteID", websiteIdParam, procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }
}

}

正如您所看到的,代码完全重复。因此,为了减少代码,我可以创建一个辅助类,如:

public static class MyDbContextHelper
{

    public static void ModelCreating(DbModelBuilder aModelBuilder)
    {
        aModelBuilder.Configurations.Add(new Reference_CountryConfiguration());
    }

    public static List<UspGetBrandByWebsiteListReturnDto> UspGetBrandByWebsiteList(DbContext aDbContext, short? aWebsiteId, out int aProcResult)
    {
        SqlParameter websiteIdParam = new SqlParameter {ParameterName = "@WebsiteID", SqlDbType = SqlDbType.SmallInt, Direction = ParameterDirection.Input, Value = aWebsiteId.GetValueOrDefault()};
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetBrandByWebsiteListReturnDto> procResultData =
            aDbContext.Database.SqlQuery<UspGetBrandByWebsiteListReturnDto>("EXEC @procResult = [dbo].[usp_GetBrandByWebsiteList] @WebsiteID", websiteIdParam, procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }

    public List<UspGetCountryListReturnDto> UspGetCountryList(out int aProcResult)
    {
        SqlParameter procResultParam = new SqlParameter {ParameterName = "@procResult", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output};

        List<UspGetCountryListReturnDto> procResultData = Database.SqlQuery<UspGetCountryListReturnDto>("EXEC @procResult = [dbo].[usp_GetCountryList] ", procResultParam).ToList();

        aProcResult = (int) procResultParam.Value;
        return procResultData;
    }

然后将实现IMyDbContext的类修改为:

public class MyFirstDbContext : DbContext, IMyDbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        MyDbContextHelper.ModelCreating(modelBuilder);
    }

    public IDbSet<Reference_CountryModel> Reference_Country { get; set; }
    public List<UspGetCountryListReturnDto> UspGetCountryList(out int aProcResult)
    {
         return MyDbContextHelper.UspGetCountryList(this, aProcResult);  
    }
}

从好处来看,实现仅包含在MyDbContextHelper中。

在实现IMyDbContext的每个类的缺点和界面更改中,我仍然需要:

  1. 添加数据集时:添加IDbSet&lt;&gt;特性
  2. 添加新方法时:在helper中实现方法,添加stub方法以调用帮助程序
  3. 我没有看到任何远离1的方式。&amp; 2.?有没有人有任何改进或建议可以进一步减少IMyDbContext接口更改时必须编写的代码量?任何人都可以看到上述方法的任何问题吗?

0 个答案:

没有答案