Ways of keeping configuration code out of logic code using Dependency Injection

Configuration classes reduce cohension and increase coupling in the consumers. This is because there may be many settings that don’t relate to the one or two needed by your class, yet in order to fulfill the dependency, your implementation of IConfiguration must supply values for all of the accessors, even the irrelevant ones. It also … Read more

How do I use StructureMap with generic unclosed types using Scan with a “greedy” constructor

Are there any concrete implementations of IToCsvService? Or just the open type ToCsvService? ConnectImplementationsToTypesClosing is for connecting something like a concrete CustomerViewModelToCsvService to IToCsvService<>. If you want to connect open classes to open interfaces, you’ll need: For(typeof(IToCsvService<>)).Use(typeof(ToCsvService<>)); Here I’m connecting the open interface type to the open class type.

How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

The static stuff is going away. If your not using a Service Locator of some type you’re going to have implement your own “ObjectFactory” as referenced here: public static class ObjectFactory { private static readonly Lazy<Container> _containerBuilder = new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication); public static IContainer Container { get { return _containerBuilder.Value; } } private static Container … Read more

Difference between MEF and IoC containers (like Unity, Autofac, SMap, Ninject, Windsor.Spring.net, etc.)

Eventually what I have concluded about the MEF vs IoC container is as follows: MEF is preferred to be used when one has to deal with unknown types or a plugin based architecture. IoC containers are preferred to be used with known types. Moreover, MEF is an architectural solution for dependency injection Whereas, IoC containers … Read more

Passing constructor arguments when using StructureMap

I suggest declaring that with the StructureMap configuration. Using the slightly newer StructureMap code: For<IProductProvider>().Use<ProductProvider> .Ctor<string>(“connectionString”).Is(someValueAtRunTime); This way you don’t burden your client code from having to know the value and can keep your IoC configuration separate from your main code.

Comparing Castle Windsor, Unity and StructureMap

See here and here for a pretty thorough technical comparison of several IoC containers, although somewhat outdated by now (they’re from before Windsor 2.0) However, I don’t think there are really any vital features that Windsor offers and other containers don’t. Windsor, StructureMap, Spring.NET have all been around for several years and have been used … Read more

tech