洋葱架构和DI容器注册依赖 [英] Onion Architecture and Registering Dependencies in DI Container

查看:202
本文介绍了洋葱架构和DI容器注册依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读了关于洋葱的架构,我有什么,我认为是关于集依赖应该如何安排了DI容器能够连线一切了一个简单的问题。

I have been reading up on the Onion architecture, and I have what I think is a simple question about how assembly dependencies should be arranged for a DI container to be able to wire everything up.

假定有如下结构非常简单的解决方案:

Assume a very simple solution that has the following structure:

UI => BL&下; = DAL

UI => BL <= DAL

因此,用户界面和DAL引用BL,但没有相互的知识。

So the UI and DAL reference the BL, but have no knowledge of each other.

此外,假设对BL具有称为IDatabaseService的接口,这是由DALDatabaseService的DAL实现的。

Also assume the BL has an interface called IDatabaseService, which is implemented in the DAL by DALDatabaseService.

容器会(大概)可以在UI的入口点配置。由于UI已经没有了DAL的知识,怎么能注册IDatabaseService解析到DALDatabaseService?

The container would (presumably) be configured in the UI's entry point. Since the UI has no knowledge of the DAL, how can it register IDatabaseService to resolve to DALDatabaseService?

推荐答案

在大多数情况下这通过使容器知道DAL(通过配置),并在同一目录中的UI或其他众所周知的位置(海关总署等),粘DAL的组件完成。在Spring.NET你在Spring配置文件中添加DAL的类型。在温莎城堡,您可以包括在具有在IoC容器中注册的类型,并在UI告诉容器要去找某目录下的所有组件的每个DLL实现IWindsorInstaller一类。

Most of the time this is done by making the container aware of the DAL (via configuration) and sticking the DAL's assemblies in the same directory as the UI or other well-known location (the GAC, etc). In Spring.NET you'd add the DAL's types in the spring configuration file. In Castle Windsor, you can include a class implementing IWindsorInstaller in each DLL that has types to be registered in the IoC container, and in the UI tell the container to go find all components in a certain directory.

这篇关于洋葱架构和DI容器注册依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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