引入IoC容器,以传统code [英] Introducing an IoC Container to Legacy Code

查看:133
本文介绍了引入IoC容器,以传统code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写内部使用我公司将使用国际奥委会通过依赖注入了新的.NET库。当然,这个库将是更容易使用,如果我们使用IoC容器来解决实例。

I'm writing a new .NET library for internal use at my company that will use IoC through Dependency Injection. Naturally, this library will be much easier to use if we use an IoC Container to resolve instances.

不过,code表示将打电话到该库目前并不使用依赖注入任何类型的,和重构传统code使用DI超出范围为我的项目。那么,什么是开始使用的容器,这种传统的code,以获得我的新库情况下,最好的方法是什么?

However, the code that will be making calls into this library does NOT currently use Dependency Injection of any sort, and refactoring the legacy code to use DI is out of scope for my project. So, what's the best way to start using a Container in this legacy code to get instances from my new library?

如果可能的话,我想,以防止乱抛垃圾说传统的code与取其IoC容器我选择硬引用。因为我是比较新的DI,这是有点可能,我们会改变主意的,我们希望在某些时候要使用的容器。

If possible, I'd like to keep from littering said legacy code with hard references to whichever IoC container I choose. Since I'm relatively new to DI, it's somewhat likely that we'll change our mind on which Container we want to use at some point.

如果我换的东西像 CommonServiceLocator 在codePLEX库我的容器,这将是一个合理的进场?

If I wrap my container in something like the CommonServiceLocator library on CodePlex, would that be a reasonable approach?

有其他人做了什么?

推荐答案

您可以使用一个门面/代理模式,从遗留的容器隐藏DI容器。你基本上是硬连线的遗产留给你实现,这将了解DI容器的自定义类。现在,如果您修改DI你更新你的外立面没有遗留code。

You can use a facade / proxy pattern to hide the DI Container from your legacy container. You are essentially hardwiring your legacy to a custom class that you implement which will know about the DI container. Now if you modify your DI you update your facades not your legacy code.

我没有做了很多的研究,公共服务定位​​器,但它是premise可能是一个很好的解决方案。你可能想从旧code完全地配合你的门面给CSL,这将隐藏DI概念。

I've not done a lot of research into Common Service Locator but it's premise might be a good solution. You might want to tie your facade to the CSL, this will hide the DI concept completly from your legacy code.

这篇关于引入IoC容器,以传统code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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