Is it even possible to setup MVC3 to use the DependencyResolver in order to get a custom ModelMetadataProvider or ModelValidatorProvider? Cause at this point I can't get it to work what so ever via the DependencyResolver. If I set it explicitly via code in the global.asax it works perfectly, but IoC just silently ignores my code. I'm using the StructureMap package from NuGet so it's nothing fancy.
My current hookups via global.asax Global.asax.cs
ModelMetadataProviders.Current = new RedSandMetadataProvider(ModelMetadataProviders.Current);
ModelValidatorProviders.Providers.Add(new RedSandValidatorProvider((IUnitOfWork)DependencyResolver.Current.GetService(typeof (IUnitOfWork))));
These work perfectly. I have to pass the current ModelMetaDataProvider as a constructor to my custom one because you can only have one ModelMetaDataProvider hooked up at a time. So I handle the calls I need to by inspecting method parameters and let the rest fall through to the base implementation.
My ModelValidatorProviders uses a IUnitOfWork object which has a Session property populated by nHibernate. I do this because I need to determine what validation rules are defined in the database based off the property being validated.
Again, these both work. But everytime I try to get them setup using StructureMap so they would be available to the DependencyResolver I can't get the desired result. Has anyone else done this before and gotten it to actually work fully? Is there something fancy I need to do because of the parameters on the constructors? Is there a specific life-cycle that must set in StructureMap's registration of these types? I've looked everywhere for examples of this but they are all either referring to betas or release candidates of MVC3 that don't apply to the final version, or are articles that say that it's possible but don't actually prove it with an example.
I would REALLY appreciate anyone's help with this because I'm pulling my hair out over how simple this should be, that all the resources on the net SAY it's possible, but I can't for the life of replicate any of their claims.
Update
I was using StructureMap.MVC3 1.0.5, I just updated to 1.0.6 after noticing there was an update, however there doesn't seem to be much difference between the versions?
My StructureMap Setup
public static IContainer Initialize() {
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
//scan.AssembliesFromApplicationBaseDirectory(); //Would this let us setup dependancy injection to dynamically load plugins?
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.LookForRegistries();
});
//x.For<IExample>().Use<Example>();
//x.For<ITempDataProvider>().Use( new CookieTempDataProvider(HttpContext.Current.Request.RequestContext.HttpContext));
//x.For<ModelMetadataProvider>().Singleton().Use(new RedSandMetadataProvider(ModelMetadataProviders.Current));
//x.For<ModelValidatorProvider>().Use<RedSandValidatorProvider>();
});
return ObjectFactory.Container;
}
I've letting the Dependency Resolver be set by the Start() method added by the package using WebActivator.
You can see the lines I was trying to use for registering my metadata and validator providers commented out. I don't know if the way I was doing it were correct or not. I added the call to scan for registries because I have a Registry to configure and add nhibernate to structuremap's container.