【问题标题】:DI with Repository and Services in different DLLs, keeping separation of concernsDI 与不同 DLL 中的存储库和服务,保持关注点分离
【发布时间】:2012-11-10 19:35:08
【问题描述】:

我有一个包含以下项目的分层应用程序:

  • DAL(将 EntityFramework 与存储库一起使用)
  • DAL.Model(包含实体,并被所有其他实体引用)
  • 服务
  • UI(在 wpf 中)

基础存储库如下所示:

public abstract class RepositoryBase<T> where T : class
{
    private readonly MyContext context;
    private readonly IDbSet<T> dbSet;

    protected RepositoryBase(MyContext dataContext)
    {
        context = dataContext;
        dbSet = context.Set<T>();
    }

    protected MyContext Context
    {
        get { return context; }
    }

    **And a series of virtual methods for Add, Delete, etc.
}

所有存储库都扩展了这个,例如:

public class MarketRepository : RepositoryBase<Market>
{
    public MarketRepository(MyContext dataContext) : base(dataContext)
    {

    }

    public IEnumerable<Market> GetAllMarkets()
    {
        return this.Context.Markets.ToList<Market>();
    }
}

服务如下所示:

public class MarketService
{
    IMarketRepository _marketRepository;

    public MarketService(IMarketRepository marketRepository)
    {
        _marketRepository = marketRepository;
    }

    public IEnumerable<Market> GetAllMarkets()
    {
        return _marketRepository.GetAllMarkets();
    }
}

我想要实现的是 UI 层只有对服务层的引用,服务层只有 DAL 层(并且所有这些都到模型,实体所在的模型)使用 DI(现在我正在使用 Unity)。

问题是,在我的 UI 容器中,我只想这样做

unity.RegisterType<IMarketService, MarketService>();

并且不必对存储库也这样做,因为这样 UI 层将依赖于 DAL 层。

我想过给服务类添加一个无参数的构造函数,比如:

public MarketService() : this(new MarketRepository(*What would I put here?)) { }

但是我失去了接口提供的抽象,而且我不知道如何处理存储库需要作为参数的 MyContext;如果我传递一个新的,那么我需要引用 DAL。

我是否应该更改我的存储库以在构造函数中创建一个新的 MyContext,而不是将其作为参数获取?

我如何重构我的架构以使其正常工作且依赖最少?

【问题讨论】:

  • 你为什么要创建存储库? DbSet 已经是一个存储库。

标签: c# architecture dependency-injection unity-container repository-pattern


【解决方案1】:

在配置 DI 时,您应该遵循相同的模式 - UI 引导程序初始化服务,服务初始化 DAL。 (使用 autofac 或 ninject 您可以使用模块来实现这一点。使用 unity 您应该模拟模块)。

在伪代码中类似于

//ui
void UILayer.ConfigureUnity(unity)
{
    ServiceLayer.ConfigureUnity(unity)
}
//services
void ServiceLayer.ConfigureUnity(unity)
{
    DAL.ConfigureUnity(unity)
    unity.RegisterType<IMarketService, MarketService>();

}
//dal
void DAL.ConfigureUnity(unity)
{
    unity.RegisterType<IMarketRepository, MarketRespository>();
    unity.RegisterType<MyContext, MyContext>(); //not sure exact syntax - just register type for 'new Type()' activator.
}

【讨论】:

    【解决方案2】:

    在创建实例时添加外部依赖项作为参数是可行的方法。
    我认为您应该让自己更加熟悉配置 Unity 的不同方式,以便解决依赖关系。
    您能否详细说明在使用依赖注入框架时为什么要创建存储库?

    【讨论】:

    • 我在数据访问层创建存储库作为访问数据的抽象;服务使用它们来操作数据。这样我就可以模拟和测试存储库,并更轻松地更改它的实现。
    【解决方案3】:

    您的 IoC 容器应该支持使用来自外部配置文件的字符串进行依赖注入。这样您就不会对映射进行硬编码。 Structuremap 在这方面做得很好,所以我相信其他 IoC 也会。

    【讨论】:

    • 是的,我可以在 app.config 中进行设置。但是,我仍然需要 UI 来引用 DAL 程序集,这是我试图避免的。
    • 不一定是 app.config。您可以定义配置方法。总会有需要做胶水工作的地方。现在的人们太沉迷于最佳实践。事实上,它实际上阻碍了人们的发展进程。
    【解决方案4】:

    好吧,我相信引导程序在应用程序的更高级别配置依赖项。因为它通常是 UI 项目,如果它需要引用其他程序集,就这样吧。如果你不喜欢你的 UI 项目管理它,那么创建一个引导程序项目来负责让你的应用程序运行并将你的 UI 类分离到另一个中。

    【讨论】:

    • 我宁愿避免再有一个额外的项目来处理这个问题......但我同意,如果我找不到任何其他方式,我会将所有对 UI 的引用添加为最后一个资源。仍然必须有更好的方法来做我所暴露的事情。
    • 我认为这里没有“更好的方法”。正如 Arthur 所说,您必须创建一个独立的项目来管理依赖关系布线,或者让现有层之一完成所有工作。大多数时候,推荐的方法(包括写过整本书的人 - 请参阅blog.ploeh.dk/2011/07/28/CompositionRoot.aspx)是将逻辑放在应用程序入口点中,这通常相当于表示层。
    • 谢谢 guillaume31,这就是我要去的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    相关资源
    最近更新 更多