Forked from DerAlbertCom/ControllerRegistryConvention.cs
Created
March 4, 2011 23:28
-
-
Save mdarnall/855903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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