Replace Spring.Net IoC with another Container (e.g

2019-06-21 05:57发布

I'm curious to know if it's possible to replace Spring.Net's built-in IoC container with Ninject. We use Ninject on my team for IoC in our other projects so I would like to continue using that container if possible.

Is this possible? Has anyone written a Ninject-Spring.Net Adapter??

Edit

I like many parts of the Spring.Net package (the data access, transactions, etc.) but I don't really like the dependency injection container. I would like to replace that with Ninject

Thanks

3条回答
爷、活的狠高调
2楼-- · 2019-06-21 06:33

I can't talk specifically about converting Spring.NET to Ninject, but in general, all application code should be written to be DI Container-agnostic.

The best way to think about DI Containers is the Hollywood Principle. In DI terms, it becomes, don't call the DI Container, it'll call you.

In other words, the best application of DI is to use simple patterns such as Constructor Injection and Abstract Factory.

Most DI Containers worth their salt inherently understand these patterns, so no special, DI Container-specific, jumping through hoops should be needed.

This also means that ideally, you should only have DI Container-specific code in a single file in your application. This place is called the Composition Root, and this is where the DI Container wires up the entire object graph and gets out of the way.

If you follow this principle, you can easily exchange one DI Container for another.

The following posts have more details:

查看更多
萌系小妹纸
3楼-- · 2019-06-21 06:43

Jeffrey, can you please provide an example of what you are trying to do? I do not see your point, why/where/how you want to mix the 2 containers. If your code is entirely container-agnostic, then you won't have any problems to use either container for doing the wiring.

查看更多
Viruses.
4楼-- · 2019-06-21 06:49

I meant everything I said in my other answer. However, I also realize that if you currently use Spring.NET as a Service Locator (i.e. you have code sprinkled all over your code base that queries the container), that answer may not be very helpful.

If this is the case, you may find the Common Service Locator project helpful. It is an open source project that attempts to abstract away specific Service Locators, hiding them all behind a common interface.

While they don't seem to have a Ninject implementation, they do have a Spring.NET implementation, so maybe that can take you halfway there.

For the record, I consider Service Locator an anti-pattern, and find that the Common Service Locator is the wrong answer to the wrong problem. In my eyes, it is utterly redundant, but it may be helpful to you as an intermediate step.

查看更多
登录 后发表回答