Last active
August 29, 2015 14:04
-
-
Save igorkulman/0353b26073fc11d3635f to your computer and use it in GitHub Desktop.
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
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | |
public class PerRequestAttribute : 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
container.PerRequest<MainViewModel>(): | |
container.PerRequest<AboutViewModel>(); | |
container.Singleton<SessionService>(); | |
container.RegisterSingleton(typeof(ISettingsService),null,typeof(SettingsService)); |
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
// 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()); | |
} | |
} | |
} |
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
// 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); | |
} | |
} | |
} |
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
[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