Skip to content

Commit

Permalink
Updated CommanderExtension.RegisterCommands() to use TypeUtils.GetAss…
Browse files Browse the repository at this point in the history
…ignableTypesInNamespace() to get commands from the current assembly(ies).
  • Loading branch information
intentor committed Apr 2, 2015
1 parent 741149d commit 9cfa890
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/Assets/Adic/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Version 2.5 (2015-04-02)
Framework
- Added creation and injection of factories by the container.

Commander extension
- Updated CommanderExtension.RegisterCommands() to use TypeUtils.GetAssignableTypesInNamespace() to get commands from the current assembly(ies).

Version 2.4 (2015-04-01)

Framework
Expand Down
22 changes: 7 additions & 15 deletions src/Assets/Adic/Scripts/Extensions/Commander/CommanderExtension.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using Adic.Commander.Exceptions;
using Adic.Container;
using Adic.Exceptions;
using Adic.Util;

namespace Adic {
/// <summary>
Expand Down Expand Up @@ -66,20 +65,13 @@ public static void RegisterCommands(this IInjectionContainer container, string n
/// <param name="container">The container in which the command will be registered.</param>
/// <param name="includeChildren">Indicates whether child namespaces should be included.</param>
/// <param name="namespaceName">Namespace name.</param>
public static void RegisterCommands(this IInjectionContainer container, string namespaceName, bool includeChildren) {
var commandType = typeof(ICommand);
var commands =
from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass &&
(
(includeChildren && !string.IsNullOrEmpty(t.Namespace) && t.Namespace.StartsWith(namespaceName)) ||
(!includeChildren && t.Namespace == namespaceName)
) &&
commandType.IsAssignableFrom(t)
select t;
public static void RegisterCommands(this IInjectionContainer container,
string namespaceName,
bool includeChildren) {
var commands = TypeUtils.GetAssignableTypesInNamespace(typeof(ICommand), namespaceName, includeChildren);

foreach (var type in commands) {
container.Bind<ICommand>().To(type);
for (var cmdIndex = 0; cmdIndex < commands.Length; cmdIndex++) {
container.Bind<ICommand>().To(commands[cmdIndex]);
}

PoolCommands(container);
Expand Down
20 changes: 18 additions & 2 deletions src/Assets/Adic/Scripts/Framework/Util/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class TypeUtils {
public static bool IsAssignable(Type potentialBase, Type potentialDescendant) {
return potentialBase.IsAssignableFrom(potentialDescendant);
}

/// <summary>
/// Gets all types assignable from a given <paramref name="baseType"/>
/// in a given <paramref name="namespaceName"/>.
Expand All @@ -26,6 +26,18 @@ public static bool IsAssignable(Type potentialBase, Type potentialDescendant) {
/// <param name="namespaceName">Namespace name.</param>
/// <returns>The assignable types in the namespace.</returns>
public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespaceName) {
return GetAssignableTypesInNamespace(baseType, namespaceName, false);
}

/// <summary>
/// Gets all types assignable from a given <paramref name="baseType"/>
/// in a given <paramref name="namespaceName"/>.
/// </summary>
/// <param name="baseType">Base type from which the types in the namespace must be assignable.</param>
/// <param name="namespaceName">Namespace name.</param>
/// <param name="includeChildren">Indicates whether child namespaces should be included.</param>
/// <returns>The assignable types in the namespace.</returns>
public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespaceName, bool includeChildren) {
var typesToBind = new List<Type>();

//Fills the assembly list.
Expand All @@ -50,7 +62,11 @@ public static Type[] GetAssignableTypesInNamespace(Type baseType, string namespa
for (int typeIndex = 0; typeIndex < allTypes.Length; typeIndex++) {
var type = allTypes[typeIndex];

if (type.Namespace == namespaceName &&
var isTypeInNamespace =
(includeChildren && !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith(namespaceName)) ||
(!includeChildren && type.Namespace == namespaceName);

if (isTypeInNamespace &&
type.IsClass &&
TypeUtils.IsAssignable(baseType, type)) {
typesToBind.Add(type);
Expand Down

0 comments on commit 9cfa890

Please sign in to comment.