private static void RegisterTypes(UnityContainer unityContainer, params AssemblyReferenceBase[] assemblyReferences) { // First validate that there aren't 2 assemblies with the same ImplementationType var duplicateImplementationTypes = assemblyReferences.GroupBy(r => r.ImplementationType).Where(r => r.Count() > 1); if (duplicateImplementationTypes.Any()) { throw new Exception(duplicateImplementationTypes.Select(d => String.Format("Both {1} are registered as ImplementationType '{0}', there can be only a single assemble registered with any given ImplementationType.", d.Key, d.Select(r => "'" + r.GetType().Name + "'").Aggregate((s1, s2) => s1 + " and " + s2))) .Aggregate((s1, s2) => s1 + "\r\n-----------------------------------\r\n" + s2)); } // Get all types that implement IAction var actionImplementations = assemblyReferences .SelectMany(r => AllClasses.FromAssemblies(r.GetType().Assembly) .Where(t => typeof(IAction).IsAssignableFrom(t)) .Select(t => new { Type = t, r.ImplementationType })); // Get all interfaces from those types that inherit from IAction var actionsToRegister = actionImplementations .SelectMany(t => WithMappings.FromAllInterfaces(t.Type) .Where(i => i != typeof(IAction) && typeof(IAction).IsAssignableFrom(i)) .Concat(new[] { t.Type }) // We also want to register the type directly .Select(i => new { Interface = i, t.Type, t.ImplementationType })); // Group the actions together and sort the implementations by priority var actionsGroupedByPriority = actionsToRegister .GroupBy(i => i.Interface) .Select(i => new { Interface = i.Key, Implementations = i.OrderBy(t => ImplementationTypePriorities.Value[t.ImplementationType]).ToList() }); foreach (var action in actionsGroupedByPriority) { // Register the default implementation (the implementation with the highest priority) unityContainer.RegisterType(action.Interface, action.Implementations.First().Type); foreach (var implementation in action.Implementations) { // Register all implementations as named registrations unityContainer.RegisterType(action.Interface, implementation.Type, implementation.ImplementationType.ToString()); } } }
public void WhenMappedToAllInterfacesIsNull() { IUnityContainer container = new UnityContainer(); AssertHelper.ThrowsException <ArgumentNullException>(() => container.RegisterTypes( AllClasses.FromAssemblies( Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)), Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))), (t) => WithMappings.FromAllInterfaces((null)), WithName.TypeName, WithLifetime.ContainerControlled)); }
public void GetsAllInterfaces() { WithMappings.FromAllInterfaces(typeof(TypeWithoutInterfaces)).AssertHasNoItems(); WithMappings.FromAllInterfaces(typeof(DisposableType)).AssertHasNoItems(); WithMappings.FromAllInterfaces(typeof(TestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable)); WithMappings.FromAllInterfaces(typeof(AnotherTestObject)).AssertContainsInAnyOrder(typeof(IAnotherInterface), typeof(ITestObject), typeof(IComparable)); // Generics WithMappings.FromAllInterfaces(typeof(GenericTestObject <,>)).AssertContainsInAnyOrder(typeof(IGenericTestObject <,>)); WithMappings.FromAllInterfaces(typeof(GenericTestObjectAlt <,>)).AssertHasNoItems(); WithMappings.FromAllInterfaces(typeof(GenericTestObject <>)).AssertContainsInAnyOrder(typeof(IComparable <>)); WithMappings.FromAllInterfaces(typeof(GenericTestObject)).AssertContainsInAnyOrder(typeof(IGenericTestObject <string, int>), typeof(IComparable <int>), typeof(IEnumerable <IList <string> >), typeof(IEnumerable)); }
public static void RegisterValidators(this IUnityContainer container) { var types = AllClasses.FromLoadedAssemblies().GetValidators(); foreach (var type in types) { var interfaces = WithMappings.FromAllInterfaces(type); foreach (var @interface in interfaces) { container.RegisterType(@interface, type); } } }
public void WhenMappedToTypeHasNoInterface() { IUnityContainer container = new UnityContainer(); container.RegisterTypes( AllClasses.FromAssemblies( Assembly.Load(new AssemblyName(RegistrationByConventionAssembly1Name)), Assembly.Load(new AssemblyName(RegistrationByConventionAssembly2Name))), (t) => WithMappings.FromAllInterfaces(typeof(TypeWithNoInterface)), WithName.TypeName, null); Assert.AreEqual <int>(1, container.Registrations.Count()); }
public static IUnityContainer RegisterAllValidators(this IUnityContainer container, IEnumerable <Type> validatorTypes, LifetimeManager lifetimeManager = null, bool mapInterfaces = true) { container.RegisterTypes( validatorTypes, t => new[] { t }.Concat( mapInterfaces ? WithMappings.FromAllInterfaces(t) .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IValidator <>)) : Enumerable.Empty <Type>()), WithName.Default, _ => lifetimeManager ?? new SingletonLifetimeManager()); return(container); }
private static IEnumerable <Type> SelectInterfacesToRegister(Type type) { var allInterfaces = WithMappings.FromAllInterfaces(type); return(from intf in allInterfaces where IsInNamespace(intf, "Aspen.DailyUpdates") select intf); }
private IEnumerable <Type> UserDefinedInterfaces(Type implementingType) { return(WithMappings.FromAllInterfaces(implementingType) .Where(iface => iface.Assembly.FullName.StartsWith(MatchingAssemblyPrefix))); }