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

StructureMap Auto registration for generic types using Scan

There is an easier way to do this. Please see this blog posting for details: Advanced StructureMap: connecting implementations to open generic types public class HandlerRegistry : Registry { public HandlerRegistry() { Scan(cfg => { cfg.TheCallingAssembly(); cfg.IncludeNamespaceContainingType<FakeRepositories.FakeClientRepository>(); cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>)); }); } } Doing it this way avoids having to create your own ITypeScanner or conventions.

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.

Null User on HttpContext obtained from StructureMap

I haven’t worked with OWIN, but when hosting in IIS integrated mode the HttpContext is not populated until after the HttpApplication.Start event is complete. In terms of DI, this means that you cannot rely on using properties of HttpContext in any constructor. This makes sense if you think about it because the application should be … Read more

Constructor Dependency Injection WebApi Attributes

Yes, it is possible. You (like most people) are being thrown by Microsoft’s marketing of Action Filter Attributes, which are conveniently put into a single class, but not at all DI-friendly. The solution is to break the Action Filter Attribute into 2 parts as demonstrated in this post: An attribute that contains no behavior to … Read more

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