Skip to content

Commit

Permalink
Revert "Updates to CommonConventions static helper class:
Browse files Browse the repository at this point in the history
1) Adapter suffix is 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."

This reverts commit b3f2e45.
  • Loading branch information
NightOwl888 committed Feb 8, 2014
1 parent 3772c22 commit b68d787
Showing 1 changed file with 16 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace DI
{
internal class CommonConventions
{
// Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
// and not decorated with the [ExcludeFromAutoRegistrationAttribute].
// Single implementations of interface with matching name (minus the "I").
public static void RegisterDefaultConventions(
Action<Type, Type> registerMethod,
Assembly[] interfaceAssemblies,
Expand All @@ -24,26 +23,21 @@ public static void RegisterDefaultConventions(

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

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

// 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)
if (implementations.Count == 1)
{
System.Diagnostics.Debug.WriteLine("Auto registration of {1} : {0}", interfaceType.Name, implementationType.Name);
registerMethod(interfaceType, implementationType);
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);
}
}
}
}
Expand All @@ -66,7 +60,7 @@ public static void RegisterAllImplementationsOfInterface(

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

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


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

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

private static bool IsExcludedType(Type type, string excludeRegEx)
private static bool IsMatch(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 b68d787

Please sign in to comment.