Created
December 15, 2011 23:41
-
-
Save suresk/1483519 to your computer and use it in GitHub Desktop.
Unity component locator, and sample
This file contains hidden or 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
namespace SmartGigs | |
{ | |
public static class Bootstrapper | |
{ | |
public static void Initialise() | |
{ | |
var container = BuildUnityContainer(); | |
DependencyResolver.SetResolver(new UnityDependencyResolver(container)); | |
} | |
private static IEnumerable<Type> FindComponentsForAssembly(Assembly assembly) | |
{ | |
foreach(Type type in assembly.GetTypes()) { | |
if (type.GetCustomAttributes(typeof(Component), true).Length > 0) | |
{ | |
yield return type; | |
} | |
} | |
} | |
private static IEnumerable<Type> FindComponents(params Assembly[] assemblies) | |
{ | |
var types = new List<Type>(); | |
foreach(Assembly assembly in assemblies) | |
{ | |
types.AddRange(FindComponentsForAssembly(assembly)); | |
} | |
return types; | |
} | |
private static void RegisterTypes(IEnumerable<Type> types, UnityContainer container) | |
{ | |
foreach(Type type in types) | |
{ | |
container.RegisterType(type, type); | |
} | |
} | |
private static IUnityContainer BuildUnityContainer() | |
{ | |
var oldProvider = FilterProviders.Providers.Single( | |
f => f is FilterAttributeFilterProvider | |
); | |
FilterProviders.Providers.Remove(oldProvider); | |
var container = new UnityContainer(); | |
var provider = new UnityFilterAttributeFilterProvider(container); | |
FilterProviders.Providers.Add(provider); | |
// register all your components with the container here | |
// e.g. container.RegisterType<ITestService, TestService>(); | |
RegisterTypes(FindComponentsForAssembly(Assembly.GetExecutingAssembly()), container); | |
container.RegisterControllers(); | |
return container; | |
} | |
} | |
public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider | |
{ | |
private IUnityContainer _container; | |
public UnityFilterAttributeFilterProvider(IUnityContainer container) | |
{ | |
_container = container; | |
} | |
protected override IEnumerable<FilterAttribute> GetControllerAttributes( | |
ControllerContext controllerContext, | |
ActionDescriptor actionDescriptor) | |
{ | |
var attributes = base.GetControllerAttributes(controllerContext, | |
actionDescriptor); | |
foreach (var attribute in attributes) | |
{ | |
_container.BuildUp(attribute.GetType(), attribute); | |
} | |
return attributes; | |
} | |
protected override IEnumerable<FilterAttribute> GetActionAttributes( | |
ControllerContext controllerContext, | |
ActionDescriptor actionDescriptor) | |
{ | |
var attributes = base.GetActionAttributes(controllerContext, | |
actionDescriptor); | |
foreach (var attribute in attributes) | |
{ | |
_container.BuildUp(attribute.GetType(), attribute); | |
} | |
return attributes; | |
} | |
} | |
public class Component : System.Attribute | |
{ | |
} | |
} |
This file contains hidden or 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
[Component] | |
public class GitHubService | |
{ | |
[Dependency] | |
public OauthTokenService TokenService { get; set; } | |
} | |
[Component] | |
public class OauthTokenService | |
{ | |
[Dependency] | |
public SmartGigsDbContext Db { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment