While integrating StructureMap.MVC5 to an ASP.Net MVC5 web application, realized that it uses 3.1 version of SM and not 4+. Then tried taking the files included in this Nuget and changing it for SM4, but a lot of code was there and several incompatible calls across SM3.1 and SM4.
With that, I ended up writing a simple IoC as below. Looking for advise on its shortfalls and what inefficiencies this have compared to the Nuget version linked here.
Define Default Registry
public class DefaultRegistry : Registry
{
public DefaultRegistry() {
Scan(
scan => {
scan.Assembly("MyAssembly");
scan.WithDefaultConventions();
});
For<IContext<SomeClass>>().Use<MyContext>();
}
}
Create a Static Container
public static class IoC {
private static IContainer container = new Container(c => c.AddRegistry<DefaultRegistry>());
public static IContainer Container { get { return container; } }
}
Override the Controller Factory
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return (IController)IoC.Container.GetInstance(controllerType);
}
}
Register in Global.asax
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}
This works, but worried that am over-simplifying it and this might introduce other issues. Looking for insights into problems with this approach.