如何在依赖依赖解析 [英] How to resolve Dependency within Dependency

查看:270
本文介绍了如何在依赖依赖解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个项目在溶液


  • DAL_Project

  • BLL_Project

  • Interface_Project

  • WebApi_Project

Interface_Project 有两个接口 ICar_DAL ICar_BLL

DAL_Project 有一个类的 Car_DAL 实现的 ICar_DAL

BLL_Project 有一个类的 Car_BLL 实现的 ICar_BLL 及其构造函数中的 ICar_DAL

WebApi_Project 有一个API控制器的 CarApiController 及其构造函数中的 ICar_BLL

的WebAPI控制器的构造函数的依赖分辨率由Unity.WebApi在Bootstrapper.cs使用该做的:

  container.RegisterType< ICar_BLL,Car_BLL>();

如果我Car_BLL进一步并未在其构造函数需要ICar_DAL这会工作。

,使其工作我可以做一些事情是这样的:

  container.RegisterType< ICar_BLL,Car_BLL>();
container.RegisterType< ICar_DAL,Car_DAL>();

但是这将意味着我需要添加引用 DAL_Project 在我的 WebApi_Project 这是我绝对不想做的事。 DAL_Project 只能由被称为 BLL_Project

我怎样才能解决这个问题呢?


解决方案

  

但是这将意味着我需要在此提及DAL_Project我
  WebApi_Project这是我绝对不想做的事。


哦,你好像对依赖应该怎么做,如果你不想做了一些误解。该DI容器在应用程序中的最外层,这实际上是主机配置。它也被称为 <青霉>组合物根的 。你的情况,这是你的Web API的宿主应用程序。如果你正在使用ASP.NET来承载你的Web API那么这是应该做的组成根部和引用的所有其他潜在项目的正确的地方。

亲自复杂的项目我倾向于有一个 ProjectName.Composition 类库,供应我作为一个的成分根的。这是我配置我的DI容器,这是引用所有其他项目 - 堂妹显然是为了配置你需要的所有相关项目和实现您的DI根。这 .Composition 组件,然后在主机应用程序和 Bootstrapper.Initialize 方法调用中的初始化方法引用托管应用程序。


  • 在ASP.NET主机的情况下,这将是的Application_Start 的Global.asax

  • 在一个桌面应用程序或自主机将是入口点的方法的情况下。

I have 4 Projects in a solution

  • DAL_Project
  • BLL_Project
  • Interface_Project
  • WebApi_Project

Interface_Project has two interfaces ICar_DAL and ICar_BLL

DAL_Project has a class Car_DAL that implements ICar_DAL

BLL_Project has a class Car_BLL that implements ICar_BLL and its constructor takes in ICar_DAL

WebApi_Project has an api controller CarApiController and its constructor takes in ICar_BLL

the dependency resolution of WebApi Controller's constructor is done by Unity.WebApi using this in Bootstrapper.cs:

container.RegisterType<ICar_BLL, Car_BLL>();

this would have worked if my Car_BLL further didn't require ICar_DAL in its constructor.

to make it work i can do some thing like this:

container.RegisterType<ICar_BLL, Car_BLL>();
container.RegisterType<ICar_DAL, Car_DAL>();

but that would mean that i need to add reference to DAL_Project in my WebApi_Project which is something i would never want to do. DAL_Project should only be referred by BLL_Project

How can i solve this issue?

解决方案

but that would mean that i need to add reference to DAL_Project in my WebApi_Project which is something i would never want to do.

Oh you seem to have some misunderstanding about how Dependency should be done if you don't want to do that. The DI container is configured in the outermost layer of your application which is actually the host. It is also referred to as the Composition Root. In your case this is the hosting application of your Web API. If you are using ASP.NET to host your Web API then this is the right place to do the composition root and reference all the other underlying projects.

Personally in complex project I tend to have a ProjectName.Composition class library which serves me as a Composition root. this is where I configure my DI container and this is the project that references all the others - coz obviously in order to configure your DI root you need all the dependent projects and implementations. This .Composition assembly is then references in the hosting application and the Bootstrapper.Initialize method called in the Initialize method of the hosting application.

  • In the case of ASP.NET host that would be Application_Start in Global.asax
  • In case of a desktop application or a self-host that would be the Main method which is the entry point.

这篇关于如何在依赖依赖解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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