Dependency injection in BAL without adding a project reference to DAL

拥有回忆 提交于 2019-12-11 06:36:18

问题


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 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>
{

}

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. 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?

  2. 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.


回答1:


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

  1. Have you tried to mock them when writing unit tests?
  2. They are what we call leaky abstractions.

Your questions:

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?

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.

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.

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.




回答2:


You never set up your container in a low level layer. Rather, you have a composition root somewhere up the hierarchy. In case of a desktop application, the Main is a good candidate. In a web application it would be the Global.asax's Application_Start.



来源:https://stackoverflow.com/questions/12555241/dependency-injection-in-bal-without-adding-a-project-reference-to-dal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!