在BAL中进行依赖注入而无需向DAL添加项目引用 [英] Dependency injection in BAL without adding a project reference to DAL

查看:135
本文介绍了在BAL中进行依赖注入而无需向DAL添加项目引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与发布的问题有关

This is related to question posted here. My Core project has following .

public interface IRepository<T> : IDisposable  
{  
    IQueryable<T> All { get; }  
    IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);  
    TEntity Find(int id);  
    void InsertOrUpdate(T entity);  
    void Delete(int id);  
    void Save();  
}  

public class Customer  
{  
    public int Id { get; set; }  
    public string FirstName { get; set; }  
    public string LastName { get; set; }   
} 

DAL具有CustomerContext和CustomerRepository.该项目取决于实体框架.

DAL has CustomerContext and CustomerRepository. This project depends upon Entity Framework.

public class CustomerContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}

public class CustomerRepository : IRepository<Customer>
{

}

接下来是我的BAL.这是一个类库项目.我需要在客户存储库上执行一些操作,但是我想这样做而不直接添加对DAL的依赖.我正在尝试使用ninject通过DI进行操作.我在IRepository和CustomerRepository之间设置绑定,如下所示.

Next comes my BAL. This is a class library project. I need to perform some actions on customer repository but I want to do it without adding dependency on DAL directly. I am trying to do via DI using ninject. I am setting up binding between IRepository and CustomerRepository as follows.

var Kernel = new StandardKernel();  
Kernel.Bind<IRepository>().To<CustomerRepository>();  

  1. 让我说我有一个UI应用程序,它将从BAL调用一些API.将IRepository绑定到CustomerRepository的上面确切的代码应该放在哪里?有什么方法可以通过App.config进行绑定吗?

  1. Let' say I have a UI application that will call some API from BAL. Where exactly above code for binding IRepository to CustomerRepository should be placed? Is there any way of doing this binding via App.config?

如您所见,如果我将其放在BAL中的某个位置,则由于我使用的是在DAL层中定义的CustomerRepository,因此必须添加对DAL项目的引用.

As you can see if I put this somewhere in BAL, then I will have to add a reference to DAL project because I am using CustomerRepository which is defined in DAL layer.

推荐答案

首先:请勿使用公开IQueryable<TEntity>的通用存储库.

First of all: Do not use generic repositories which expose IQueryable<TEntity>.

  1. 您在编写单元测试时是否尝试过模拟它们?
  2. 我们称之为泄漏抽象.

您的问题:

让我说我有一个UI应用程序,它将从BAL调用一些API.将IRepository绑定到CustomerRepository的上面确切的代码应该放在哪里?有什么方法可以通过App.config进行绑定吗?

Let' say I have a UI application that will call some API from BAL. Where exactly above code for binding IRepository to CustomerRepository should be placed? Is there any way of doing this binding via App.config?

为什么不能从UI项目向DAL添加依赖项?由于您将在发布项目和/或创建安装程序包时自动包含DAL,因此还将简化安装.

Why can't you add a dependency to your DAL from your UI project? It will also simplify the installation since the DAL will automatically be included when you publish the project and/or create a setup package.

如您所见,如果我将其放在BAL中的某个位置,那么我将不得不添加对DAL项目的引用,因为我使用的是在DAL层中定义的CustomerRepository.

As you can see if I put this somewhere in BAL, then I will have to add a reference to DAL project because I am using CustomerRepository which is defined in DAL layer.

不要在BAL中创建参考或配置. UI项目是根.因此,应将任何配置添加到其中.

Don't create a reference or configuration in the BAL. It's the UI project which is the root. So any configuration should be added into it.

我通常在每个项目中创建一个合成根,然后从启动项目中在每个程序集中调用该根.这意味着启动项目只需要知道我们获得了哪些项目,而不必知道它们包含什么.

I usually create a composition root in every project and then invoke the root in every assembly from my startup project. That means that the startup project only has to know which projects we got, but not what they contain.

这篇关于在BAL中进行依赖注入而无需向DAL添加项目引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆