Skip to content

Instantly share code, notes, and snippets.

@DerAlbertCom
Created November 22, 2010 10:54
Show Gist options
  • Save DerAlbertCom/709811 to your computer and use it in GitHub Desktop.
Save DerAlbertCom/709811 to your computer and use it in GitHub Desktop.
Simple Registration of Services and Controller for ASP.MVC 3 with StructureMap
using System;
using System.Web.Mvc;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace NewInMVC3.Infrastructure
{
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 NewInMVC3.Infrastructure
{
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();
});
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 NewInMVC3.Infrastructure
{
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.Web.Mvc;
using StructureMap;
namespace NewInMVC3.Infrastructure
{
internal class StructureMapFilterAttributeFilterProvider : FilterAttributeFilterProvider
{
private readonly IContainer container;
public StructureMapFilterAttributeFilterProvider(IContainer container)
{
this.container = container;
}
protected override System.Collections.Generic.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 System.Collections.Generic.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