使用依赖注入进行内部操作的类库的最佳实践是什么? [英] What are the best practices for class libraries using dependency injection for internal operations?

查看:9
本文介绍了使用依赖注入进行内部操作的类库的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建足够复杂以使用内部依赖注入的类库时,我应该注意什么?

What should I be careful about when building a class library complex enough to use internal dependency injection?

假设它将使用 Castle Windsor(作为示例),考虑到该库将由简单的控制台应用程序(没有 DI)使用,Web 表单使用相同的容器 (Castle Windsor),以及使用不同容器 (NInject) 的网络应用?

Assuming that it will use Castle Windsor (as an example), what would be the best place/method to configure the container, given that the library will be used by simple console application (with no DI), web forms using the same container (Castle Windsor), and web apps using a different container (NInject)?

推荐答案

我会使用外观模式 此处:在库中,在执行容器初始化的公共类上公开一个公共方法(例如一个简单的 Initialize()),并且仅在库内部使用 Castle Windsor,以便图书馆客户甚至不知道你在使用它.

I would use the facade pattern here: in the library, expose a public method on a public class that does the container initialization (such as a simple Initialize()), and use Castle Windsor only internally within the library, so that the library clients don't even know that you are using it.

这篇关于使用依赖注入进行内部操作的类库的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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