1

我想将 DI 引入现有的大型代码库,该代码库具有许多继承和抽象层(以及各种方式的引用!)。在最低级别之一,我想使用 DI 将特定对象的生活方式控制为单例(当前实现使用 ThreadStatic 并且存在问题)。我是否需要从顶部一直向下注入并在容器中注册所有对象(这需要大量工作),还是有一种方法可以让我做这种快速而肮脏的风格(现在;)?我正在使用 C# 和 Castle Windsor。

4

1 回答 1

0

You could go for a service locator pattern that provides the container. It is considered a bit of a bad practice by many Is it bad to use servicelocation instead of constructor injection to avoid writing loads of factory classes but one could argue its better then nothing if the option for you is to have hard dependencies and buggy code. There are some situation that it is not considered that bad http://codeofrob.com/entries/service-location-is-not-always-evil.html

I would however read up a bit on the subject if I were you and see how it fits you needs and your application. DI/IoC debates tend to get a bit religious sometimes imho.

于 2012-04-19T07:32:49.037 回答