Skip to content

Instantly share code, notes, and snippets.

@suresk
Created December 15, 2011 23:41
Show Gist options
  • Save suresk/1483519 to your computer and use it in GitHub Desktop.
Save suresk/1483519 to your computer and use it in GitHub Desktop.
Unity component locator, and sample
public class AdminAuthorizeAttribute : AuthorizeAttribute
{
[Dependency]
public AccountHelper AccountHelper { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
Account account = AccountHelper.GetCurrentAccount();
if (account == null) return false;
return account.Admin;
}
}
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
{
}
}
[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