Skip to content

Instantly share code, notes, and snippets.

@igorkulman
Last active August 29, 2015 14:04
Show Gist options
  • Save igorkulman/0353b26073fc11d3635f to your computer and use it in GitHub Desktop.
Save igorkulman/0353b26073fc11d3635f to your computer and use it in GitHub Desktop.
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class PerRequestAttribute : Attribute
{
}
container.PerRequest<MainViewModel>():
container.PerRequest<AboutViewModel>();
container.Singleton<SessionService>();
container.RegisterSingleton(typeof(ISettingsService),null,typeof(SettingsService));
// Automatically registers all view models and services
foreach (var type in (typeof(MainViewModel).GetTypeInfo().Assembly.DefinedTypes)
.Union((typeof(ContactPointsService).GetTypeInfo().Assembly.DefinedTypes)))
{
if (type.IsClass && !type.IsAbstract)
{
// Non-inherited first, inherited second
if (type.GetCustomAttributes(inherit: false).OfType<SingletonAttribute>().Any())
{
container.RegisterSingleton(type.ImplementedInterfaces.Count()==1 ? type.ImplementedInterfaces.First() : type.AsType(), null, type.AsType());
}
else if (type.GetCustomAttributes(inherit: false).OfType<PerRequestAttribute>().Any())
{
container.RegisterPerRequest(type.ImplementedInterfaces.Count()==1 ? type.ImplementedInterfaces.First() : type.AsType(), null, type.AsType());
}
else if (type.GetCustomAttributes(inherit: true).OfType<SingletonAttribute>().Any())
{
container.RegisterSingleton(type.ImplementedInterfaces.Count() == 1 ? type.ImplementedInterfaces.First() : type.AsType(), null, type.AsType());
}
else if (type.GetCustomAttributes(inherit: true).OfType<PerRequestAttribute>().Any())
{
container.RegisterPerRequest(type.ImplementedInterfaces.Count() == 1 ? type.ImplementedInterfaces.First() : type.AsType(), null, type.AsType());
}
}
}
// Automatically registers all types from the current assembly that have correct attribute
foreach (var type in (Assembly.GetExecutingAssembly().GetTypes())
.Union(Assembly.Load("business logic assembly name").GetTypes()))
{
if (type.IsClass && !type.IsAbstract)
{
var interfaces = type.GetInterfaces();
// Non-inherited first, inherited second
if (type.GetCustomAttributes(inherit: false).OfType<SingletonAttribute>().Any())
{
container.RegisterSingleton(interfaces.Count() == 1 ? interfaces.First() : type, null, type);
}
else if (type.GetCustomAttributes(inherit: false).OfType<PerRequestAttribute>().Any())
{
container.RegisterPerRequest(interfaces.Count()==1 ? interfaces.First() : type, null, type);
}
else if (type.GetCustomAttributes(inherit: true).OfType<SingletonAttribute>().Any())
{
container.RegisterSingleton(interfaces.Count() == 1 ? interfaces.First() : type, null, type);
}
else if (type.GetCustomAttributes(inherit: true).OfType<PerRequestAttribute>().Any())
{
container.RegisterPerRequest(interfaces.Count() == 1 ? interfaces.First() : type, null, type);
}
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class SingletonAttribute : Attribute
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment