Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mdarnall/855903 to your computer and use it in GitHub Desktop.
Save mdarnall/855903 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace TransCore.FMWebMobile.DependencyResolution
{
internal class ControllerRegistryConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
if (!type.IsAbstract && typeof(IController).IsAssignableFrom(type))
{
registry.AddType(type, type);
}
}
}
}
using System.Web.Mvc;
namespace TransCore.FMWebMobile.DependencyResolution
{
internal static class RegisterStructureMap
{
public static void Execute()
{
var container = new StructureMap.Container();
container.Configure(c =>
{
c.Scan(x =>
{
x.AssembliesFromApplicationBaseDirectory();
x.WithDefaultConventions();
x.AddAllTypesOf<ModelValidatorProvider>();
x.AddAllTypesOf<ModelMetadataProvider>();
x.AddAllTypesOf<ValueProviderFactory>();
x.AddAllTypesOf<IModelBinderProvider>();
x.AddAllTypesOf<IControllerActivator>();
x.AddAllTypesOf<IViewPageActivator>();
x.AddAllTypesOf<IFilterProvider>();
x.With(new ControllerRegistryConvention());
x.LookForRegistries();
});
// inject dependencies that are public setters
c.SetAllProperties(x =>
x.TypeMatches(
type => container.Model.HasImplementationsFor(type)));
});
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
FilterProviders.Providers.Add(new StructureMapFilterAttributeFilterProvider(container));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using StructureMap;
namespace TransCore.FMWebMobile.DependencyResolution
{
internal class StructureMapDependencyResolver : IDependencyResolver
{
private readonly IContainer container;
public StructureMapDependencyResolver(IContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
var instance = container.TryGetInstance(serviceType);
if (instance == null && !serviceType.IsAbstract)
{
instance = AddTypeAndTryGetInstance(serviceType);
}
return instance;
}
private object AddTypeAndTryGetInstance(Type serviceType)
{
container.Configure(c => c.AddType(serviceType, serviceType));
return container.TryGetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return container.GetAllInstances(serviceType).Cast<object>();
}
}
}
using System.Collections.Generic;
using System.Web.Mvc;
using StructureMap;
namespace TransCore.FMWebMobile.DependencyResolution
{
internal class StructureMapFilterAttributeFilterProvider : FilterAttributeFilterProvider
{
private readonly IContainer container;
public StructureMapFilterAttributeFilterProvider(IContainer container)
{
this.container = container;
}
protected override IEnumerable<FilterAttribute> GetActionAttributes(
ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var attributes = base.GetActionAttributes(controllerContext, actionDescriptor);
foreach (var filterAttribute in attributes){
container.BuildUp(filterAttribute);
}
return attributes;
}
protected override IEnumerable<FilterAttribute> GetControllerAttributes(
ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var attributes = base.GetControllerAttributes(controllerContext, actionDescriptor);
foreach (var filterAttribute in attributes){
container.BuildUp(filterAttribute);
}
return attributes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment