简体   繁体   English

依赖注入我的接口在哪里?

[英]Dependency Injection Where Does My Interface Go?

I am new to DI containers and have been doing some reading and I was wondering if you guys could help me out with my first sample project. 我是DI容器的新手,并且一直在做一些阅读,我想知道你们是否可以在我的第一个示例项目中为我提供帮助。

I have a MVC5 application separated into n tier modules. 我有一个MVC5应用程序,分为n层模块。 I have my presentation module injecting an IDataAdapter into a controller via a defaultcontrollerfactory. 我的演示模块通过defaultcontrollerfactory.IDataAdapter注入到控制器中defaultcontrollerfactory. This dataadapter is then passed via constructor injection into a service class that talks to the data access layer via the IDataAdapter. 然后,通过构造函数注入将该数据dataadapter传递到服务类中,该服务类通过IDataAdapter.与数据访问层通信IDataAdapter.

My question is, where is the IDataAdapter interface class supposed to be located, obvious the concrete class that implements this interface needs to be in the data access layer. 我的问题是,应该将IDataAdapter接口类放在哪里,显然实现该接口的具体类需要位于数据访问层中。 I could leave the interface in the DAL, but then I would need a hard reference between my view and dal, and that does not seem right. 我可以将接口留在DAL中,但是随后我需要在我的视图和dal之间进行严格的引用,这似乎是不对的。

So I made a separate project library to hold my container logic and data adapters, but now I am running into a circular dependency, where my DataAdapter needs to implement the IDataAdapter interface, but my unity registration file does not know what the DataAdapter concrete class is because it can't reference the DAL. 因此,我创建了一个单独的项目库来保存我的容器逻辑和数据适配器,但是现在我遇到了循环依赖关系,其中我的DataAdapter需要实现IDataAdapter接口,但是我的统一注册文件不知道DataAdapter具体类是什么因为它无法引用DAL。

While using dependency Injection, It would be good to have all interfaces in a separate assembly so that you can avoid any kind of circular dependency. 使用依赖注入时,最好将所有接口放在单独的程序集中,这样就可以避免任何形式的循环依赖。

High level diagram of recommended solution structure: 推荐解决方案结构的高级示意图:

单击此处查看推荐的解决方案结构的高级示意图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM