Skip to content

Commit

Permalink
Updates to CommonConventions static helper class: 1) Adapter suffix i…
Browse files Browse the repository at this point in the history
…s now registered by default, default convention takes precedence 2) Single implementations of interface no longer required to auto-register 3) ExcludeFromAutoRegistrationAttribute can be applied to any class to explicitly exclude it from auto registration 4) Renamed IsMatch private functions to IsExcludedType for clarity.
  • Loading branch information
NightOwl888 committed Jan 29, 2014
1 parent f3d235f commit b3f2e45
Showing 1 changed file with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace DI
{
internal class CommonConventions
{
// Single implementations of interface with matching name (minus the "I").
// Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
// and not decorated with the [ExcludeFromAutoRegistrationAttribute].
public static void RegisterDefaultConventions(
Action<Type, Type> registerMethod,
Assembly[] interfaceAssemblies,
Expand All @@ -23,21 +24,26 @@ public static void RegisterDefaultConventions(

foreach (var interfaceType in interfaces)
{
if (!IsMatch(interfaceType, excludeTypes, excludeRegEx))
if (!IsExcludedType(interfaceType, excludeTypes, excludeRegEx))
{
List<Type> implementations = new List<Type>();

foreach (var assembly in implementationAssemblies)
implementations.AddRange(GetImplementationsOfInterface(assembly, interfaceType));
implementations.AddRange(GetImplementationsOfInterface(assembly, interfaceType).Where(implementation => !IsExcludedType(implementation, excludeTypes, excludeRegEx)).ToArray());

if (implementations.Count == 1)
// Prefer the default name ITypeName = TypeName
Type implementationType = implementations.Where(implementation => IsDefaultType(interfaceType, implementation)).FirstOrDefault();

if (implementationType == null)
{
// Fall back on ITypeName = ITypeNameAdapter
implementationType = implementations.Where(implementation => IsAdapterType(interfaceType, implementation)).FirstOrDefault();
}

if (implementationType != null)
{
var implementationType = implementations[0];
if (!IsMatch(implementationType, excludeTypes, excludeRegEx) && interfaceType.Name.Equals("I" + implementationType.Name))
{
System.Diagnostics.Debug.WriteLine("Auto registration of {1} : {0}", interfaceType.Name, implementationType.Name);
registerMethod(interfaceType, implementationType);
}
System.Diagnostics.Debug.WriteLine("Auto registration of {1} : {0}", interfaceType.Name, implementationType.Name);
registerMethod(interfaceType, implementationType);
}
}
}
Expand All @@ -60,7 +66,7 @@ public static void RegisterAllImplementationsOfInterface(

foreach (var implementationType in implementations)
{
if (!IsMatch(implementationType, excludeTypes, excludeRegEx))
if (!IsExcludedType(implementationType, excludeTypes, excludeRegEx))
{
System.Diagnostics.Debug.WriteLine("Auto registration of {1} : {0}", interfaceType.Name, implementationType.Name);
registerMethod(interfaceType, implementationType);
Expand All @@ -87,7 +93,7 @@ public static void RegisterAllImplementationsOfInterfaceSingle(

foreach (var implementationType in implementations)
{
if (!IsMatch(implementationType, excludeTypes, excludeRegEx))
if (!IsExcludedType(implementationType, excludeTypes, excludeRegEx))
{
matchingImplementations.Add(implementationType);
}
Expand All @@ -99,22 +105,38 @@ public static void RegisterAllImplementationsOfInterfaceSingle(
}


private static bool IsMatch(Type type, Type[] excludeTypes, string excludeRegEx)
private static bool IsExcludedType(Type type, Type[] excludeTypes, string excludeRegEx)
{
return IsMatch(type, excludeTypes) || IsMatch(type, excludeRegEx);
return IsExcludedType(type, excludeTypes) || IsExcludedType(type, excludeRegEx) || IsExcludedType(type);
}

private static bool IsMatch(Type type, Type[] excludeTypes)
private static bool IsExcludedType(Type type, Type[] excludeTypes)
{
return excludeTypes.Contains(type);
}

private static bool IsMatch(Type type, string excludeRegEx)
private static bool IsExcludedType(Type type, string excludeRegEx)
{
if (string.IsNullOrEmpty(excludeRegEx)) return false;
return Regex.Match(type.Name, excludeRegEx, RegexOptions.Compiled).Success;
}

private static bool IsExcludedType(Type type)
{
return type.GetCustomAttributes(typeof(MvcSiteMapProvider.DI.ExcludeFromAutoRegistrationAttribute), false).Length > 0;
}

private static bool IsDefaultType(Type interfaceType, Type implementationType)
{
return interfaceType.Name.Equals("I" + implementationType.Name);
}

private static bool IsAdapterType(Type interfaceType, Type implementationType)
{
return implementationType.Name.EndsWith("Adapter") &&
interfaceType.Name.Equals("I" + implementationType.Name.Substring(0, implementationType.Name.Length - 7));
}

private static IEnumerable<Type> GetInterfaces(Assembly assembly)
{
return assembly.GetTypes().Where(t => t.IsInterface);
Expand Down

0 comments on commit b3f2e45

Please sign in to comment.