Skip to content
Browse files

Split auto binders into events and commands

Added fixed filesystem tests
  • Loading branch information...
1 parent 223851d commit 579cd63eb5764899d15f0a03a637e3b4226e1ec0 Thomas Coats committed Mar 3, 2012
View
4 Example.CLI/Program.cs
@@ -23,7 +23,8 @@ public static void Main(string[] args)
kernel.Load(new NormalExampleInfrastructure());
kernel.Load(new AutoBindRepositories());
kernel.Load(new AutoBindFetchingStrategies());
- kernel.Load(new AutoBindMessages());
+ kernel.Load(new AutoBindEventListeners());
+ kernel.Load(new AutoBindCommandHandlers());
kernel.Load(new AutoBindValidators());
var _commandSender = kernel.Get<ISendCommands>();
@@ -79,7 +80,6 @@ public static void Main(string[] args)
Console.WriteLine("Inventory Item is {0}", validator.IsValid((IInventoryItem)inventoryItem) ? "valid" : "invalid");
// TODO: Snapshots (+snapshot versioning)
- // TODO: Event versioning through interfaces
Console.Write("Press any key to quit...");
Console.In.Read();
View
57 Voodoo.Plumbing/AutoBindCommandHandlers.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Ninject;
+using Ninject.Modules;
+using Voodoo.Reflection;
+using Voodoo.Infrastructure;
+using System.Linq.Expressions;
+
+namespace Voodoo.Plumbing
+{
+ public class AutoBindCommandHandlers : NinjectModule
+ {
+ private Func<Action<ICommand>> BuildCommandLookup(Type commandType, Type commandHandlerType)
+ {
+ return () =>
+ {
+ var commandHandler = Kernel.Get(commandHandlerType);
+ var commandParameter = Expression.Parameter(typeof(ICommand), "source");
+ var commandHandlerHandleMethod = typeof(IHandleCommand<>).MakeGenericType(commandType).GetMethod("Handle");
+ var result = Expression.Lambda<Action<ICommand>>(
+ Expression.Call(
+ Expression.Constant(commandHandler, commandHandlerType),
+ commandHandlerHandleMethod,
+ Expression.Convert(commandParameter, commandType)),
+ commandParameter);
+ return result.Compile();
+ };
+ }
+
+ public override void Load()
+ {
+ var defaultCommandTransport = Kernel.Get<DefaultCommandTransport>();
+
+ foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
+ {
+ foreach (var pair in MarkerInterfaces.GetTyped(assembly, typeof(IHandleCommand<>)))
+ {
+ if (pair.Value.Count > 1)
+ throw new Exception();
+
+ var commandHandlerType = pair.Value.First();
+
+ if (!Kernel.GetBindings(commandHandlerType).Any())
+ Bind(commandHandlerType).ToSelf().InSingletonScope();
+
+ Bind(pair.Key).ToMethod(c => c.Kernel.Get(commandHandlerType));
+
+ var commandType = pair.Key.GetGenericArguments().First();
+
+ defaultCommandTransport.RegisterCommandHandlerLookup(commandType, BuildCommandLookup(commandType, commandHandlerType));
+ }
+ }
+ }
+ }
+}
View
37 Voodoo.Plumbing/AutoBindMessages.cs → Voodoo.Plumbing/AutoBindEventListeners.cs
@@ -10,25 +10,8 @@
namespace Voodoo.Plumbing
{
- public class AutoBindMessages : NinjectModule
+ public class AutoBindEventListeners : NinjectModule
{
- private Func<Action<ICommand>> BuildCommandLookup(Type commandType, Type commandHandlerType)
- {
- return () =>
- {
- var commandHandler = Kernel.Get(commandHandlerType);
- var commandParameter = Expression.Parameter(typeof(ICommand), "source");
- var commandHandlerHandleMethod = typeof(IHandleCommand<>).MakeGenericType(commandType).GetMethod("Handle");
- var result = Expression.Lambda<Action<ICommand>>(
- Expression.Call(
- Expression.Constant(commandHandler, commandHandlerType),
- commandHandlerHandleMethod,
- Expression.Convert(commandParameter, commandType)),
- commandParameter);
- return result.Compile();
- };
- }
-
private Func<List<Action<IEvent>>> BuildEventLookup(Type eventType, IEnumerable<Type> eventListenerTypes)
{
return () =>
@@ -53,28 +36,10 @@ private Func<List<Action<IEvent>>> BuildEventLookup(Type eventType, IEnumerable<
public override void Load()
{
- var defaultCommandTransport = Kernel.Get<DefaultCommandTransport>();
var defaultEventTransport = Kernel.Get<DefaultEventTransport>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
- foreach (var pair in MarkerInterfaces.GetTyped(assembly, typeof(IHandleCommand<>)))
- {
- if (pair.Value.Count > 1)
- throw new Exception();
-
- var commandHandlerType = pair.Value.First();
-
- if (!Kernel.GetBindings(commandHandlerType).Any())
- Bind(commandHandlerType).ToSelf().InSingletonScope();
-
- Bind(pair.Key).ToMethod(c => c.Kernel.Get(commandHandlerType));
-
- var commandType = pair.Key.GetGenericArguments().First();
-
- defaultCommandTransport.RegisterCommandHandlerLookup(commandType, BuildCommandLookup(commandType, commandHandlerType));
- }
-
foreach (var pair in MarkerInterfaces.GetTyped(assembly, typeof(IListenToEvent<>)))
{
foreach (var type in pair.Value)
View
52 Voodoo.Plumbing/NinjectModuleExtensions.cs
@@ -1,52 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Ninject.Modules;
-using System.Reflection;
-
-namespace Voodoo.Plumbing
-{
- public static class NinjectModuleExtensions
- {
- public static void BindAllImplementations(this NinjectModule module, Type type, List<string> assemblyNames = null)
- {
- Assembly[] assemblies;
-
- if (assemblyNames == null || !assemblyNames.Any())
- assemblies = AppDomain.CurrentDomain.GetAssemblies();
- else
- assemblies = assemblyNames.Select(a => Assembly.Load(a)).ToArray();
-
- foreach (var assembly in assemblies)
- {
- Type[] types = null;
- try
- {
- types = assembly.GetTypes();
- }
- catch (Exception)
- {
-
- }
-
- if (types == null)
- continue;
-
- foreach (var typeToConsider in types)
- {
- var implementers = typeToConsider.GetInterfaces()
- .Where(t =>
- t.IsGenericType &&
- t.GetGenericTypeDefinition() == type).ToList();
-
- if (!implementers.Any())
- continue;
-
- foreach (var implementer in implementers)
- module.Bind(implementer).To(typeToConsider);
- }
- }
- }
- }
-}
View
4 Voodoo.Plumbing/Properties/AssemblyInfo.cs
@@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.1.0.0")]
-[assembly: AssemblyFileVersion("1.1.0.0")]
+[assembly: AssemblyVersion("1.2.0.0")]
+[assembly: AssemblyFileVersion("1.2.0.0")]
View
2 Voodoo.Plumbing/TestingInfrastructure.cs
@@ -22,8 +22,6 @@ public override void Load()
Bind<IFileSystem>().ToMethod(context => context.Kernel.Get<FixedFileSystem>()).InSingletonScope();
Bind<EventStore>().To<EventStore>().InSingletonScope();
Bind<IStoreEvents>().ToMethod(context => context.Kernel.Get<EventStore>()).InSingletonScope();
- //Bind<IStrategyForFetching<IEnumerable<IEvent>, FetchById>>()
- // .ToMethod(context => context.Kernel.Get<EventStore>()).InSingletonScope();
Bind<AnythingValidator>().ToSelf().InSingletonScope();
Bind<IValidateAnything>().ToMethod(c => c.Kernel.Get<AnythingValidator>()).InSingletonScope();
}
View
BIN Voodoo.Plumbing/Voodoo.Plumbing.1.2.0.0.nupkg
Binary file not shown.
View
4 Voodoo.Plumbing/Voodoo.Plumbing.csproj
@@ -47,12 +47,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="AutoBindMessages.cs" />
+ <Compile Include="AutoBindEventListeners.cs" />
<Compile Include="AutoBindValidators.cs" />
<Compile Include="AutoBindFetchingStrategies.cs" />
<Compile Include="AutoBindRepositories.cs" />
+ <Compile Include="AutoBindCommandHandlers.cs" />
<Compile Include="InternalMessaging.cs" />
- <Compile Include="NinjectModuleExtensions.cs" />
<Compile Include="NLogLogger.cs" />
<Compile Include="NormalInfrastructure.cs" />
<Compile Include="PreLoader.cs" />
View
52 Voodoo.Tests/IO/FixedFileSystemTests.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Voodoo.IO;
+
+namespace Voodoo.Tests.IO
+{
+ [TestClass]
+ public class FixedFileSystemTests
+ {
+ [TestMethod]
+ public void AddToFilesystem_AddFiveFoldersToRoot_FiveFoldersInRoot()
+ {
+ var fs = new FixedFileSystem();
+
+ fs.AddPaths(
+ @"\one\",
+ @"\two\",
+ @"\three\",
+ @"\four\",
+ @"\five\");
+
+ Assert.AreEqual(0, fs.Root.Files.Count);
+ Assert.AreEqual(5, fs.Root.Directories.Count);
+ }
+
+ [TestMethod]
+ public void AddToFilesystem_AddFileToRoot_OnlyOneFileInRoot()
+ {
+ var fs = new FixedFileSystem();
+
+ fs.AddPaths(@"\bad movie.avi");
+
+ Assert.AreEqual(1, fs.Root.Files.Count);
+ Assert.AreEqual(0, fs.Root.Directories.Count);
+ }
+
+ [TestMethod]
+ public void AddToFilesystem_AddFileToFolder_OnlyOneFileInFolder()
+ {
+ var fs = new FixedFileSystem();
+
+ fs.AddPaths(@"\movies\bad movie.avi");
+
+ Assert.AreEqual(0, fs.Root.Files.Count);
+ Assert.AreEqual(1, fs.Root.Directories.Count);
+ Assert.AreEqual(1, fs.Root.Directories.Single(x => x.Name == "movies").Files.Count);
+ }
+ }
+}
View
1 Voodoo.Tests/Voodoo.Tests.csproj
@@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Graph\GraphTests.cs" />
+ <Compile Include="IO\FixedFileSystemTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reflection\ReflectUtilTests.cs" />
<Compile Include="Extensions\ToTitleCaseTests.cs" />
View
4 Voodoo/Properties/AssemblyInfo.cs
@@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.1.0.0")]
-[assembly: AssemblyFileVersion("1.1.0.0")]
+[assembly: AssemblyVersion("1.2.0.0")]
+[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: CLSCompliant(true)]
View
37 Voodoo/Reflection/MarkerInterfaces.cs
@@ -9,46 +9,55 @@ namespace Voodoo.Reflection
public static class MarkerInterfaces
{
/// <summary>
- /// public class Something : IMarkClasses(T)
+ /// Given an interface (generic or not) find all implementing classes
+ /// then find the non generic version of the interface used
+ ///
+ /// eg IListenToEvent() =>
+ /// IListenToEvent(InventoryItemCreated) -> InventoryItemDetailView, InventoryListView
/// </summary>
- public static IDictionary<Type, IList<Type>> GetTyped(Assembly assembly, Type markerInterface)
+ public static IDictionary<Type, List<Type>> GetTyped(Assembly assembly, Type markerInterface)
{
- var results = new Dictionary<Type, IList<Type>>();
+ var results = new Dictionary<Type, List<Type>>();
// look through the assembly public types looking for types that have the marker interface
foreach (var markedClass in Get(assembly, markerInterface))
{
- var typedClasses = markedClass.GetInterfaces()
- .Where(x => x.IsGenericType
- && x.GetGenericTypeDefinition() == markerInterface);
+ var typedInterfaces = markedClass.GetInterfaces()
+ .Where(t => t.IsInterface
+ && !t.IsGenericTypeDefinition
+ && t.IsGenericType
+ && t.GetGenericTypeDefinition() == markerInterface);
// For each of the marker interfaces on the type register a route
- foreach (var typedClass in typedClasses)
+ foreach (var typedInterface in typedInterfaces)
{
- if (!results.ContainsKey(typedClass))
- results.Add(typedClass, new List<Type>());
+ if (!results.ContainsKey(typedInterface))
+ results.Add(typedInterface, new List<Type>());
- results[typedClass].Add(markedClass);
+ results[typedInterface].Add(markedClass);
}
}
return results;
}
/// <summary>
- /// public class Something : IMarkClasses
+ /// Given an interface (generic or not) find all implementing classes
+ ///
+ /// eg IListenToEvent() => InventoryItemDetailView, InventoryListView
/// </summary>
- public static IList<Type> Get(Assembly assembly, Type markerInterface)
+ public static List<Type> Get(Assembly assembly, Type markerInterface)
{
if (assembly.IsDynamic)
return new List<Type>();
var searchTypes = assembly.GetExportedTypes()
- .Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericTypeDefinition);
+ .Where(t => !t.IsInterface && !t.IsAbstract && !t.IsGenericTypeDefinition);
// If the marker interface is generic look for generic interfaces
if (markerInterface.IsGenericType)
return searchTypes
- .Where(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == markerInterface))
+ .Where(t => t.GetInterfaces().Any(i => i.IsGenericType
+ && i.GetGenericTypeDefinition() == markerInterface))
.ToList();
else
return searchTypes
View
BIN Voodoo/Voodoo.1.2.0.0.nupkg
Binary file not shown.

0 comments on commit 579cd63

Please sign in to comment.
Something went wrong with that request. Please try again.