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

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

问题描述

我有UI应用领域基础设施层.

在我的基础设施层引用应用层,使用Ninject注册两者的服务接口.

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

但是我需要在我的 Application 层中的 Infrastructure 层中提供一个服务,然后我需要在我的 中引用 Infrastructure 层应用程序层.

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

问题是Infrastructure层引用Application层,什么时候我会在Application引用Infrastructure层> 图层显示如下错误:

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:

无法添加对基础设施"的引用.将此项目添加为引用会导致循环依赖.

我如何解决这个问题?把Application层的Ninject配置放在Application层?我认为这是不正确的,因为我将在我的应用程序层中实现基础设施.

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.

推荐答案

基础设施服务契约应该在使用它们的层(域和应用程序)中定义,但在基础设施中实现.看看依赖倒置原则洋葱架构.基础设施层应该依赖于 App 和 Domain.您的域和应用程序不应依赖于基础设施.它们应该依赖于用它们自己的术语定义的抽象.你可能会发现这个答案 有趣.这个抽象的实际实现应该在应用程序启动时注入一个所谓的 组合根.

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 容器注入到 Application 中.应用程序不会依赖于基础设施,但仍会使用它,因此 Dependecny Inversion.我建议阅读 .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天全站免登陆