应该在哪里基础设施服务合同住在哪里? [英] Where should infrastructure services contracts live?

查看:231
本文介绍了应该在哪里基础设施服务合同住在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户界面应用程序基础设施的图层。

在我的基础设施的层取参考的的两者使用的应用程序的层注册服务界面的 Ninject

In my Infrastructure Layer take reference of Domain and Application Layer to register services interfaces of both using Ninject.

但我需要在我的应用程序的中的基础设施的一层又一层服务,然后我需要参考的基础设施的图层在我的应用的图层。

But I need in my Application Layer a service in Infrastructure Layer, then i need to reference the Infrastructure Layer in my Application Layer.

现在的问题是基础设施的层,进行参照的应用程序的层,当我引用的基础设施的层中的应用程序图层下面的错误显示:

The problem is Infrastructure Layer take reference to Application Layer and when I'll reference Infrastructure Layer in Application Layer the following error is show:

引用'基建'不能添加。 Addind这个项目作为一个基准将导致循环依赖。

A reference to 'Infrastructure' could not be added. Addind this project as a reference would cause a circular dependency.

我该如何解决这个问题?放的Ninject配置的应用程序的图层中的应用程序的图层?我认为这是不正确的,因为我得的基础设施的在我的应用程序执行的图层。

How I solve this? Put the Ninject Configuration of Application Layer in the Application Layer? I think this is not correct, because I'll have Infrastructure implementation in my Application Layer.

推荐答案

合同应当以消耗他们(域和应用程序)的层来定义,但在基础设施实施的基础设施服务。看看依赖倒置原则和的洋葱建筑。基础设施层应取决于应用程序和域。您的域名和应用程序不应该依赖于基础设施。他们应该依靠自己来定义的抽象。您可能会发现此<一个href="http://stackoverflow.com/questions/7306109/having-trouble-putting-real-world-logic-into-the-ddd-domain-layer/7311521#7311521">answer有趣。这种抽象的实际实施应该在应用程序启动时在所谓的成分根注入

Infrastructure services contracts should be defined in the layers that consume them (Domain and Application), but implemented in Infrastructure. Take a look at Dependency Inversion Principle and Onion Architecture. Infrastructure layer should depend on App and Domain. Your Domain and App should not depend on Infrastructure. They should depend on abstraction defined in their own terms. You may find this answer interesting. The actual implementation of this abstraction should be injected at the application startup in a so called Composition Root.

例如在你的应用程序,你可以定义和类似的界面:

For example in your Application you can define and interface like:

ICanNotifyUserOfSuccessfullRegistration

基础设施层将引用应用和使用SMTP或SMS类将实现这个接口:

The Infrastructure layer will reference Application and will implement this interface using SMTP or SMS classes:

class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration { ... }

后来,这个实现将被注入由DI容器的应用。应用程序将不会对基础设施的依赖,但是仍然会使用它,因此 Dependecny反转。我建议你​​阅读依赖注入的.NET ,即使您使用Java或其他栈。

Later on this implementation will be injected into Application by DI container. Application will not have a dependency on Infrastructure but will still use it, hence Dependecny Inversion. I recommend reading Dependency Injection in .NET, even if you use Java or other stacks.

这篇关于应该在哪里基础设施服务合同住在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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