基础设施服务合同应该放在哪里?

发布于 2024-12-07 04:43:33 字数 468 浏览 2 评论 0原文

我有UI应用程序基础设施层。

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

但我需要在我的应用程序层中提供基础设施层中的服务,然后我需要在我的应用程序中引用基础设施层em> 层。

问题是基础设施层引用了应用程序层,当我在应用程序层中引用基础设施层时显示以下错误:

无法添加对“基础设施”的引用。添加此项目作为引用会导致循环依赖。

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

I have UI, Application, Domain and Infrastructure Layers.

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:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲在坟头点根烟 2024-12-14 04:43:33

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

例如,在您的应用程序中,您可以定义和接口如下:

ICanNotifyUserOfSuccessfullRegistration

基础设施层将引用应用程序,并将使用 SMTP 或 SMS 类实现此接口:

class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration { ... }

稍后此实现将通过 DI 容器注入到应用程序中。应用程序不会依赖于基础设施,但仍会使用它,因此依赖反转。我建议您阅读 .NET 中的依赖注入,即使您使用 Java 或其他堆栈。

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

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

class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration { ... }

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.

清泪尽 2024-12-14 04:43:33

听起来你的层要么耦合得太紧,要么边界错误。您可以通过引入存在于自己项目中并且可以被其他项目引用的接口来解耦层。

Sounds like your layers are either too tightly coupled or have the wrong boundaries. You can decouple the layers by introducing interfaces that live in their own project and can be referenced by the others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文