diff --git a/README.md b/README.md index d6e81e7..07b8f8f 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,14 @@ ShortBus is an in-process mediator with low-friction API ### Command + public class DoSomething : ICommand { } - public class DoesSomething : ICommandHandler { - public void Handle(DoSomething command) { - // does something - } - } + public class DoesSomething : ICommandHandler { + public void Handle(DoSomething command) { + // does something + } + } _mediator.Send(new DoSomething()); @@ -17,14 +18,14 @@ ShortBus is an in-process mediator with low-friction API ### Query public class AskAQuestion : IQuery { } - public class Answerer : IQueryHandler { - public Answer Handle(AskAQuestion query) { - return answer; - } - } + public class Answerer : IQueryHandler { + public Answer Handle(AskAQuestion query) { + return answer; + } + } - var answer = _mediator.Request(new AskAQuestion()); - + var answer = _mediator.Request(new AskAQuestion()); + ### StructureMap ShortBus depends on StructureMap and it requires that you register handlers: diff --git a/ShortBus.Autofac.nuspec b/ShortBus.Autofac.nuspec index c696b6f..12b263a 100644 --- a/ShortBus.Autofac.nuspec +++ b/ShortBus.Autofac.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,13 +13,13 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - + + - - - + + + \ No newline at end of file diff --git a/ShortBus.Autofac/AutofacDependencyResolver.cs b/ShortBus.Autofac/AutofacDependencyResolver.cs index 3895c22..93f6e74 100644 --- a/ShortBus.Autofac/AutofacDependencyResolver.cs +++ b/ShortBus.Autofac/AutofacDependencyResolver.cs @@ -1,11 +1,12 @@ namespace ShortBus.Autofac { using System; + using System.Collections.Generic; using global::Autofac; public class AutofacDependencyResolver : IDependencyResolver { - readonly ILifetimeScope _container; + private readonly ILifetimeScope _container; public AutofacDependencyResolver(ILifetimeScope container) { @@ -16,5 +17,10 @@ public object GetInstance(Type type) { return _container.Resolve(type); } + + public IEnumerable GetInstances() + { + return _container.Resolve>(); + } } } \ No newline at end of file diff --git a/ShortBus.Autofac/Properties/AssemblyInfo.cs b/ShortBus.Autofac/Properties/AssemblyInfo.cs index 185cc0e..f79c03d 100644 --- a/ShortBus.Autofac/Properties/AssemblyInfo.cs +++ b/ShortBus.Autofac/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Autofac")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("d043983c-3a10-4b36-8739-980f32c93401")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Autofac/packages.config b/ShortBus.Autofac/packages.config index defc345..cc1f6f4 100644 --- a/ShortBus.Autofac/packages.config +++ b/ShortBus.Autofac/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/ShortBus.Markers.nuspec b/ShortBus.Markers.nuspec index ed4e4bf..b788e16 100644 --- a/ShortBus.Markers.nuspec +++ b/ShortBus.Markers.nuspec @@ -1,4 +1,5 @@ + 1.0.0 @@ -12,9 +13,9 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - - + + + \ No newline at end of file diff --git a/ShortBus.Markers/IAsyncCommand.cs b/ShortBus.Markers/IAsyncCommand.cs deleted file mode 100644 index 3a0a23f..0000000 --- a/ShortBus.Markers/IAsyncCommand.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ShortBus -{ - public interface IAsyncCommand : IAsyncCommand { } - - public interface IAsyncCommand { } -} \ No newline at end of file diff --git a/ShortBus.Markers/IAsyncQuery.cs b/ShortBus.Markers/IAsyncQuery.cs deleted file mode 100644 index 3844f14..0000000 --- a/ShortBus.Markers/IAsyncQuery.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ShortBus -{ - public interface IAsyncQuery {} -} \ No newline at end of file diff --git a/ShortBus.Markers/IAsyncRequest.cs b/ShortBus.Markers/IAsyncRequest.cs new file mode 100644 index 0000000..217fef8 --- /dev/null +++ b/ShortBus.Markers/IAsyncRequest.cs @@ -0,0 +1,4 @@ +namespace ShortBus +{ + public interface IAsyncRequest {} +} \ No newline at end of file diff --git a/ShortBus.Markers/ICommand.cs b/ShortBus.Markers/ICommand.cs deleted file mode 100644 index 64391a5..0000000 --- a/ShortBus.Markers/ICommand.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ShortBus -{ - public interface ICommand : ICommand { } - - public interface ICommand { } -} \ No newline at end of file diff --git a/ShortBus.Markers/IQuery.cs b/ShortBus.Markers/IQuery.cs deleted file mode 100644 index 67151db..0000000 --- a/ShortBus.Markers/IQuery.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace ShortBus -{ - public interface IQuery {} -} \ No newline at end of file diff --git a/ShortBus.Markers/IRequest.cs b/ShortBus.Markers/IRequest.cs new file mode 100644 index 0000000..6e486bb --- /dev/null +++ b/ShortBus.Markers/IRequest.cs @@ -0,0 +1,4 @@ +namespace ShortBus +{ + public interface IRequest {} +} \ No newline at end of file diff --git a/ShortBus.Markers/Properties/AssemblyInfo.cs b/ShortBus.Markers/Properties/AssemblyInfo.cs index 51fa59e..adaa398 100644 --- a/ShortBus.Markers/Properties/AssemblyInfo.cs +++ b/ShortBus.Markers/Properties/AssemblyInfo.cs @@ -1,25 +1,27 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Markers")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("ShortBus.Markers")] -[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyCopyright("Copyright © 2014")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("a6579898-beb7-480d-89d9-a3515bee169f")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Markers/ShortBus.Markers.csproj b/ShortBus.Markers/ShortBus.Markers.csproj index 8706368..bebca86 100644 --- a/ShortBus.Markers/ShortBus.Markers.csproj +++ b/ShortBus.Markers/ShortBus.Markers.csproj @@ -43,10 +43,8 @@ - - - - + + diff --git a/ShortBus.Markers/UnitType.cs b/ShortBus.Markers/UnitType.cs index 3604648..a65900d 100644 --- a/ShortBus.Markers/UnitType.cs +++ b/ShortBus.Markers/UnitType.cs @@ -4,6 +4,6 @@ public sealed class UnitType { public static readonly UnitType Default = new UnitType(); - UnitType() { } + private UnitType() {} } } \ No newline at end of file diff --git a/ShortBus.Ninject.nuspec b/ShortBus.Ninject.nuspec index 73688c9..2f655a1 100644 --- a/ShortBus.Ninject.nuspec +++ b/ShortBus.Ninject.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,13 +13,13 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - + + - - - + + + \ No newline at end of file diff --git a/ShortBus.Ninject/NinjectDependencyResolver.cs b/ShortBus.Ninject/NinjectDependencyResolver.cs index 94ba2d3..fe3b798 100644 --- a/ShortBus.Ninject/NinjectDependencyResolver.cs +++ b/ShortBus.Ninject/NinjectDependencyResolver.cs @@ -1,11 +1,12 @@ namespace ShortBus.Ninject { using System; + using System.Collections.Generic; using global::Ninject; public class NinjectDependencyResolver : IDependencyResolver { - readonly IKernel _container; + private readonly IKernel _container; public NinjectDependencyResolver(IKernel container) { @@ -16,5 +17,10 @@ public object GetInstance(Type type) { return _container.Get(type); } + + public IEnumerable GetInstances() + { + return _container.GetAll(); + } } } \ No newline at end of file diff --git a/ShortBus.Ninject/Properties/AssemblyInfo.cs b/ShortBus.Ninject/Properties/AssemblyInfo.cs index e1983ed..df158d0 100644 --- a/ShortBus.Ninject/Properties/AssemblyInfo.cs +++ b/ShortBus.Ninject/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Ninject")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("44900dbc-c446-453c-966a-d985b47cecb7")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Ninject/packages.config b/ShortBus.Ninject/packages.config index 55823db..4b47fd0 100644 --- a/ShortBus.Ninject/packages.config +++ b/ShortBus.Ninject/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/ShortBus.StructureMap.nuspec b/ShortBus.StructureMap.nuspec index 3c7ab3b..97d6226 100644 --- a/ShortBus.StructureMap.nuspec +++ b/ShortBus.StructureMap.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,13 +13,13 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - + + - - - + + + \ No newline at end of file diff --git a/ShortBus.StructureMap/Properties/AssemblyInfo.cs b/ShortBus.StructureMap/Properties/AssemblyInfo.cs index 0f5a4e0..186eccc 100644 --- a/ShortBus.StructureMap/Properties/AssemblyInfo.cs +++ b/ShortBus.StructureMap/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.StructureMap")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("862899e5-67df-49d9-9102-a6c0ac5ad715")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.StructureMap/StructureMapDependencyResolver.cs b/ShortBus.StructureMap/StructureMapDependencyResolver.cs index e6e95eb..972d5c5 100644 --- a/ShortBus.StructureMap/StructureMapDependencyResolver.cs +++ b/ShortBus.StructureMap/StructureMapDependencyResolver.cs @@ -1,11 +1,12 @@ namespace ShortBus.StructureMap { using System; + using System.Collections.Generic; using global::StructureMap; public class StructureMapDependencyResolver : IDependencyResolver { - readonly IContainer _container; + private readonly IContainer _container; public StructureMapDependencyResolver(IContainer container) { @@ -16,5 +17,10 @@ public object GetInstance(Type type) { return _container.GetInstance(type); } + + public IEnumerable GetInstances() + { + return _container.GetAllInstances(); + } } } \ No newline at end of file diff --git a/ShortBus.StructureMap/packages.config b/ShortBus.StructureMap/packages.config index 60a21cd..1dbe4da 100644 --- a/ShortBus.StructureMap/packages.config +++ b/ShortBus.StructureMap/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/ShortBus.Tests/Containers/ContainerTests.cs b/ShortBus.Tests/Containers/ContainerTests.cs index 4fc0e6c..dcd2c79 100644 --- a/ShortBus.Tests/Containers/ContainerTests.cs +++ b/ShortBus.Tests/Containers/ContainerTests.cs @@ -1,93 +1,129 @@ namespace ShortBus.Tests.Containers { - using Autofac; + using System.Linq; using Castle.MicroKernel.Registration; using Castle.Windsor; using global::Autofac; using global::Ninject; using global::StructureMap; using Microsoft.Practices.Unity; - using Ninject; using NUnit.Framework; - using StructureMap; - using Unity; - using Windsor; + using ShortBus.Autofac; + using ShortBus.Ninject; + using ShortBus.StructureMap; + using ShortBus.Unity; + using ShortBus.Windsor; [TestFixture] public class ContainerTests { - public ContainerTests() { } - - class Registered { } - [Test] - public void AutofacResolveSingleInstance() + public void AutofacResolve() { var builder = new ContainerBuilder(); - var registered = new Registered(); - builder.RegisterInstance(registered); + var registerSingle = new RegisterSingle(); + var multipleRegistrations = new[] {new RegisterMultiple(), new RegisterMultiple()}; + builder.RegisterInstance(registerSingle); + builder.RegisterInstance(multipleRegistrations[0]); + builder.RegisterInstance(multipleRegistrations[1]); var resolver = new AutofacDependencyResolver(builder.Build()); - var resolved = (Registered) resolver.GetInstance(typeof (Registered)); - - Assert.That(resolved, Is.EqualTo(registered)); + var resolvedSingle = (RegisterSingle) resolver.GetInstance(typeof (RegisterSingle)); + var resolvedMultiple = resolver.GetInstances().ToArray(); + Assert.That(resolvedSingle, Is.EqualTo(registerSingle)); + Assert.That(resolvedMultiple.Count(), Is.EqualTo(2)); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[0])); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[1])); } [Test] - public void NinjectResolveSingleInstance() + public void NinjectResolve() { var kernel = new StandardKernel(); - var registered = new Registered(); - kernel.Bind().ToConstant(registered); + var registerSingle = new RegisterSingle(); + var multipleRegistrations = new[] {new RegisterMultiple(), new RegisterMultiple()}; + kernel.Bind().ToConstant(registerSingle); + kernel.Bind().ToConstant(multipleRegistrations[0]); + kernel.Bind().ToConstant(multipleRegistrations[1]); var resolver = new NinjectDependencyResolver(kernel); - var resolved = (Registered) resolver.GetInstance(typeof (Registered)); - - Assert.That(resolved, Is.EqualTo(registered)); + var resolvedSingle = (RegisterSingle) resolver.GetInstance(typeof (RegisterSingle)); + var resolvedMultiple = resolver.GetInstances().ToArray(); + Assert.That(resolvedSingle, Is.EqualTo(registerSingle)); + Assert.That(resolvedMultiple.Count(), Is.EqualTo(2)); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[0])); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[1])); } [Test] - public void StructureMapResolveSingleInstance() + public void StructureMapResolve() { - var registered = new Registered(); + var registerSingle = new RegisterSingle(); + var multipleRegistrations = new[] {new RegisterMultiple(), new RegisterMultiple()}; - ObjectFactory.Initialize(i => i.Register(registered)); + ObjectFactory.Initialize(i => + { + i.Register(registerSingle); + i.Register(multipleRegistrations[0]); + i.Register(multipleRegistrations[1]); + }); var resolver = new StructureMapDependencyResolver(ObjectFactory.Container); - var resolved = (Registered) resolver.GetInstance(typeof (Registered)); - - Assert.That(resolved, Is.EqualTo(registered)); + var resolvedSingle = (RegisterSingle) resolver.GetInstance(typeof (RegisterSingle)); + var resolvedMultiple = resolver.GetInstances().ToArray(); + Assert.That(resolvedSingle, Is.EqualTo(registerSingle)); + Assert.That(resolvedMultiple.Count(), Is.EqualTo(2)); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[0])); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[1])); } [Test] - public void UnityResolveSingleInstance() + public void UnityResolve() { var container = new UnityContainer(); - var registered = new Registered(); - container.RegisterInstance(registered); + var registerSingle = new RegisterSingle(); + var multipleRegistrations = new[] {new RegisterMultiple(), new RegisterMultiple()}; + container.RegisterInstance(registerSingle); + //unity requires a name when registering multiple instances of the same type or nothing will be resolved. + container.RegisterInstance("instance1", multipleRegistrations[0]); + container.RegisterInstance("instance2", multipleRegistrations[1]); var resolver = new UnityDependencyResolver(container); - var resolved = (Registered) resolver.GetInstance(typeof (Registered)); - - Assert.That(resolved, Is.EqualTo(registered)); + var resolvedSingle = (RegisterSingle) resolver.GetInstance(typeof (RegisterSingle)); + var resolvedMultiple = resolver.GetInstances().ToArray(); + Assert.That(resolvedSingle, Is.EqualTo(registerSingle)); + Assert.That(resolvedMultiple.Count(), Is.EqualTo(2)); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[0])); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[1])); } [Test] - public void WindsorResolveSingleInstance() + public void WindsorResolve() { var container = new WindsorContainer(); - var registered = new Registered(); - container.Register(Component.For().Instance(registered)); + var registerSingle = new RegisterSingle(); + var multipleRegistrations = new[] {new RegisterMultiple(), new RegisterMultiple()}; + //windsor requires a name when registering multiple instances of the same type or nothing will be resolved. + container.Register(Component.For().Instance(registerSingle)); + container.Register(Component.For().Instance(multipleRegistrations[0]).Named("instance1")); + container.Register(Component.For().Instance(multipleRegistrations[1]).Named("instance2")); var resolver = new WindsorDependencyResolver(container); - var resolved = (Registered) resolver.GetInstance(typeof (Registered)); - - Assert.That(resolved, Is.EqualTo(registered)); + var resolvedSingle = (RegisterSingle) resolver.GetInstance(typeof (RegisterSingle)); + var resolvedMultiple = resolver.GetInstances().ToArray(); + Assert.That(resolvedSingle, Is.EqualTo(registerSingle)); + Assert.That(resolvedMultiple.Count(), Is.EqualTo(2)); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[0])); + Assert.That(resolvedMultiple.Contains(multipleRegistrations[1])); } + + private class RegisterSingle {} + + private class RegisterMultiple {} } } \ No newline at end of file diff --git a/ShortBus.Tests/Example/AsyncExample.cs b/ShortBus.Tests/Example/AsyncExample.cs index 1e0e1b3..8bc0d0d 100644 --- a/ShortBus.Tests/Example/AsyncExample.cs +++ b/ShortBus.Tests/Example/AsyncExample.cs @@ -1,59 +1,66 @@ namespace ShortBus.Tests.Example { - using System; + using System.Collections; + using System.Collections.Generic; using System.Threading.Tasks; using global::StructureMap; using NUnit.Framework; - using StructureMap; + using ShortBus.StructureMap; [TestFixture] public class AsyncExample { [Test] - public void RequestResponse() + public void Notification() { - ObjectFactory.Initialize(i => i.Scan(s => + var handled = new List(); + + ObjectFactory.Initialize(i => { - s.TheCallingAssembly(); - s.AddAllTypesOf((typeof(IAsyncQueryHandler<,>))); - })); + i.Scan(s => + { + s.TheCallingAssembly(); + s.AddAllTypesOf((typeof (INotificationHandler<>))); + }); + i.For().Use(handled); + }); var resolver = new StructureMapDependencyResolver(ObjectFactory.Container); - var query = new ExternalResourceQuery(); + var notification = new Notification(); var mediator = new Mediator(resolver); - var task = mediator.RequestAsync(query); + mediator.Notify(notification); - Assert.That(task.Result.Data, Is.EqualTo("success")); - Assert.That(task.Result.HasException(), Is.False); + CollectionAssert.AreEquivalent(handled, new[] {1, 2}); } [Test] - public void SendResult() + public void RequestResponse() { ObjectFactory.Initialize(i => i.Scan(s => { s.TheCallingAssembly(); - s.AddAllTypesOf((typeof(IAsyncCommandHandler<,>))); + s.AddAllTypesOf((typeof (IAsyncRequestHandler<,>))); })); var resolver = new StructureMapDependencyResolver(ObjectFactory.Container); - var query = new AddResource(); + var query = new ExternalResourceQuery(); var mediator = new Mediator(resolver); - var result = mediator.SendAsync(query).Result; + var task = mediator.RequestAsync(query); - Assert.That(result.Data, Is.EqualTo(AddResourceHandler.Result)); + Assert.That(task.Result.Data, Is.EqualTo("success")); + Assert.That(task.Result.HasException(), Is.False); } } - public class ExternalResourceQuery : IAsyncQuery { } + public class ExternalResourceQuery : IAsyncRequest {} - public class ExternalResourceHandler : IAsyncQueryHandler + public class ExternalResourceHandler : IAsyncRequestHandler { public Task HandleAsync(ExternalResourceQuery request) { @@ -61,15 +68,35 @@ public Task HandleAsync(ExternalResourceQuery request) } } - public class AddResource : IAsyncCommand { } + public class Notification {} - public class AddResourceHandler : IAsyncCommandHandler + public class NotificationHandler1 : INotificationHandler { - public static Guid Result = new Guid("D5361D4E-26F2-4E16-932B-930243CBC830"); + private readonly IList _list; + + public NotificationHandler1(IList list) + { + _list = list; + } + + public void Handle(Notification notification) + { + _list.Add(1); + } + } + + public class NotificationHandler2 : INotificationHandler + { + private readonly IList _list; + + public NotificationHandler2(IList list) + { + _list = list; + } - public Task HandleAsync(AddResource message) + public void Handle(Notification notification) { - return Task.FromResult(Result); + _list.Add(2); } } } \ No newline at end of file diff --git a/ShortBus.Tests/Example/AutofacBasicExample.cs b/ShortBus.Tests/Example/AutofacBasicExample.cs index 95b00db..8d8adae 100644 --- a/ShortBus.Tests/Example/AutofacBasicExample.cs +++ b/ShortBus.Tests/Example/AutofacBasicExample.cs @@ -1,13 +1,13 @@ -using System; -using System.Diagnostics; -using System.Linq; -using Autofac; -using Autofac.Features.Variance; -using NUnit.Framework; -using ShortBus.Autofac; - namespace ShortBus.Tests.Example { + using System; + using System.Diagnostics; + using System.Linq; + using global::Autofac; + using global::Autofac.Features.Variance; + using NUnit.Framework; + using ShortBus.Autofac; + [TestFixture] public class AutofacBasicExample { @@ -23,11 +23,7 @@ public void SetUpRootScope() builder.RegisterSource(new ContravariantRegistrationSource()); builder.RegisterAssemblyTypes(typeof (IMediator).Assembly, GetType().Assembly) - .AsClosedTypesOf(typeof (IQueryHandler<,>)) - .AsImplementedInterfaces(); - - builder.RegisterAssemblyTypes(typeof (IMediator).Assembly, GetType().Assembly) - .AsClosedTypesOf(typeof (ICommandHandler<,>)) + .AsClosedTypesOf(typeof (IRequestHandler<,>)) .AsImplementedInterfaces(); builder.RegisterType().AsImplementedInterfaces().InstancePerLifetimeScope(); @@ -136,7 +132,7 @@ public void Send_void() var mediator = TestScope.Resolve(); - var response = mediator.Send(command); + var response = mediator.Request(command); Assert.That(response.HasException(), Is.False, response.Exception == null ? string.Empty : response.Exception.ToString()); @@ -153,7 +149,7 @@ public void Send_void_variant() var mediator = TestScope.Resolve(); - var response = mediator.Send(command); + var response = mediator.Request(command); Assert.That(response.HasException(), Is.False, response.Exception == null ? string.Empty : response.Exception.ToString()); @@ -166,7 +162,7 @@ public void Send_result() var mediator = TestScope.Resolve(); - var response = mediator.Send(command); + var response = mediator.Request(command); Assert.That(response.Data, Is.EqualTo("foo"), response.Exception == null ? string.Empty : response.Exception.ToString()); diff --git a/ShortBus.Tests/Example/BasicExample.cs b/ShortBus.Tests/Example/BasicExample.cs index 249cfa4..ba2f0cd 100644 --- a/ShortBus.Tests/Example/BasicExample.cs +++ b/ShortBus.Tests/Example/BasicExample.cs @@ -1,11 +1,11 @@ -using System; -using System.Diagnostics; -using NUnit.Framework; -using ShortBus.StructureMap; -using StructureMap; - -namespace ShortBus.Tests.Example +namespace ShortBus.Tests.Example { + using System; + using System.Diagnostics; + using global::StructureMap; + using NUnit.Framework; + using ShortBus.StructureMap; + [TestFixture] public class BasicExample { @@ -18,8 +18,8 @@ public BasicExample() s.AssemblyContainingType(); s.TheCallingAssembly(); s.WithDefaultConventions(); - s.AddAllTypesOf((typeof(IQueryHandler<,>))); - s.AddAllTypesOf(typeof(ICommandHandler<,>)); + s.AddAllTypesOf((typeof (IRequestHandler<,>))); + s.AddAllTypesOf(typeof (INotificationHandler<>)); }); i.For().Use(() => DependencyResolver.Current); @@ -86,14 +86,15 @@ public void Send_void() var command = new PrintText { Format = "This is a {0} message", - Args = new object[] { "text" } + Args = new object[] {"text"} }; var mediator = ObjectFactory.GetInstance(); - var response = mediator.Send(command); + var response = mediator.Request(command); - Assert.That(response.HasException(), Is.False, response.Exception == null ? string.Empty : response.Exception.ToString()); + Assert.That(response.HasException(), Is.False, + response.Exception == null ? string.Empty : response.Exception.ToString()); } [Test] @@ -102,12 +103,12 @@ public void Send_void_variant() var command = new PrintTextSpecial { Format = "This is a {0} message", - Args = new object[] { "text" } + Args = new object[] {"text"} }; var mediator = ObjectFactory.GetInstance(); - var response = mediator.Send(command); + var response = mediator.Request(command); Assert.That(response.HasException(), Is.False); } @@ -119,7 +120,7 @@ public void Send_result() var mediator = new Mediator(DependencyResolver.Current); - var response = mediator.Send(command); + var response = mediator.Request(command); Assert.That(response.Data, Is.EqualTo("foo")); } diff --git a/ShortBus.Tests/Example/ConsoleWriter.cs b/ShortBus.Tests/Example/ConsoleWriter.cs index 374917e..cf0a1af 100644 --- a/ShortBus.Tests/Example/ConsoleWriter.cs +++ b/ShortBus.Tests/Example/ConsoleWriter.cs @@ -2,15 +2,17 @@ { using System; - public class ConsoleWriter : CommandHandler + public class ConsoleWriter : IRequestHandler { - protected override void HandleCore(PrintText command) + public UnitType Handle(PrintText request) { - Console.WriteLine(command.Format, command.Args); + Console.WriteLine(request.Format, request.Args); + + return UnitType.Default; } } - public class CommandWithResultHandler : ICommandHandler + public class CommandWithResultHandler : IRequestHandler { public string Handle(CommandWithResult command) { diff --git a/ShortBus.Tests/Example/MultiPing.cs b/ShortBus.Tests/Example/MultiPing.cs index c28b625..000e9fc 100644 --- a/ShortBus.Tests/Example/MultiPing.cs +++ b/ShortBus.Tests/Example/MultiPing.cs @@ -1,7 +1,10 @@ namespace ShortBus.Tests.Example { - public class DoublePing : IQuery {} + public class DoublePing : IRequest {} + public class DoublePingALing : DoublePing {} - public class TriplePing : IQuery {} + + public class TriplePing : IRequest {} + public class TriplePingALing : TriplePing {} } \ No newline at end of file diff --git a/ShortBus.Tests/Example/MultiPong.cs b/ShortBus.Tests/Example/MultiPong.cs index d894dff..9c1c914 100644 --- a/ShortBus.Tests/Example/MultiPong.cs +++ b/ShortBus.Tests/Example/MultiPong.cs @@ -1,8 +1,8 @@ namespace ShortBus.Tests.Example { - public class MultiPong - : IQueryHandler, - IQueryHandler + public class MultiPong + : IRequestHandler, + IRequestHandler { public string Handle(DoublePing request) { diff --git a/ShortBus.Tests/Example/Ping.cs b/ShortBus.Tests/Example/Ping.cs index 3e28c7a..9860314 100644 --- a/ShortBus.Tests/Example/Ping.cs +++ b/ShortBus.Tests/Example/Ping.cs @@ -1,6 +1,6 @@ namespace ShortBus.Tests.Example { - public class Ping : IQuery { } + public class Ping : IRequest {} - public class PingALing : Ping { } + public class PingALing : Ping {} } \ No newline at end of file diff --git a/ShortBus.Tests/Example/Pong.cs b/ShortBus.Tests/Example/Pong.cs index 3ed320d..76368bc 100644 --- a/ShortBus.Tests/Example/Pong.cs +++ b/ShortBus.Tests/Example/Pong.cs @@ -1,6 +1,6 @@ namespace ShortBus.Tests.Example { - public class Pong : IQueryHandler + public class Pong : IRequestHandler { public string Handle(Ping request) { diff --git a/ShortBus.Tests/Example/PrintText.cs b/ShortBus.Tests/Example/PrintText.cs index 9665b5a..b55fa15 100644 --- a/ShortBus.Tests/Example/PrintText.cs +++ b/ShortBus.Tests/Example/PrintText.cs @@ -1,11 +1,11 @@ -namespace ShortBus.Tests.Example -{ - public class PrintText : ICommand - { - public virtual string Format { get; set; } - public virtual object[] Args { get; set; } - } - +namespace ShortBus.Tests.Example +{ + public class PrintText : IRequest + { + public virtual string Format { get; set; } + public virtual object[] Args { get; set; } + } + public class PrintTextSpecial : PrintText { private string _format; @@ -17,8 +17,5 @@ public override string Format } } - public class CommandWithResult : ICommand - { - - } + public class CommandWithResult : IRequest {} } \ No newline at end of file diff --git a/ShortBus.Tests/Properties/AssemblyInfo.cs b/ShortBus.Tests/Properties/AssemblyInfo.cs index 90b77ec..f415989 100644 --- a/ShortBus.Tests/Properties/AssemblyInfo.cs +++ b/ShortBus.Tests/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Tests")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("d9de61ab-11c1-4867-9625-fc536c7864ed")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Tests/packages.config b/ShortBus.Tests/packages.config index 7f14ac2..3e8d21e 100644 --- a/ShortBus.Tests/packages.config +++ b/ShortBus.Tests/packages.config @@ -1,9 +1,10 @@  - + + - + diff --git a/ShortBus.Unity.nuspec b/ShortBus.Unity.nuspec index f4d5841..a89d59a 100644 --- a/ShortBus.Unity.nuspec +++ b/ShortBus.Unity.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,13 +13,13 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - + + - - - + + + \ No newline at end of file diff --git a/ShortBus.Unity/Properties/AssemblyInfo.cs b/ShortBus.Unity/Properties/AssemblyInfo.cs index ec6d698..fa92b9e 100644 --- a/ShortBus.Unity/Properties/AssemblyInfo.cs +++ b/ShortBus.Unity/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Unity")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("da5dd16f-602e-4840-9d7d-78ed44a87c5a")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Unity/UnityDependencyResolver.cs b/ShortBus.Unity/UnityDependencyResolver.cs index a992964..87538ee 100644 --- a/ShortBus.Unity/UnityDependencyResolver.cs +++ b/ShortBus.Unity/UnityDependencyResolver.cs @@ -1,11 +1,12 @@ namespace ShortBus.Unity { using System; + using System.Collections.Generic; using Microsoft.Practices.Unity; public class UnityDependencyResolver : IDependencyResolver { - readonly IUnityContainer _container; + private readonly IUnityContainer _container; public UnityDependencyResolver(IUnityContainer container) { @@ -16,5 +17,10 @@ public object GetInstance(Type type) { return _container.Resolve(type); } + + public IEnumerable GetInstances() + { + return _container.ResolveAll(); + } } } \ No newline at end of file diff --git a/ShortBus.Unity/packages.config b/ShortBus.Unity/packages.config index d272543..9bdd58c 100644 --- a/ShortBus.Unity/packages.config +++ b/ShortBus.Unity/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/ShortBus.Windsor.nuspec b/ShortBus.Windsor.nuspec index ac9e461..d42166e 100644 --- a/ShortBus.Windsor.nuspec +++ b/ShortBus.Windsor.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,13 +13,13 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - - + + - - - + + + \ No newline at end of file diff --git a/ShortBus.Windsor/Properties/AssemblyInfo.cs b/ShortBus.Windsor/Properties/AssemblyInfo.cs index d3bfeb5..9dba08b 100644 --- a/ShortBus.Windsor/Properties/AssemblyInfo.cs +++ b/ShortBus.Windsor/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus.Windsor")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] @@ -17,9 +17,11 @@ // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("ce241a26-dc71-4fe6-b8a6-ce73f27d4c95")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus.Windsor/WindsorDependencyResolver.cs b/ShortBus.Windsor/WindsorDependencyResolver.cs index 15478ea..a462e5d 100644 --- a/ShortBus.Windsor/WindsorDependencyResolver.cs +++ b/ShortBus.Windsor/WindsorDependencyResolver.cs @@ -1,11 +1,12 @@ namespace ShortBus.Windsor { using System; + using System.Collections.Generic; using Castle.Windsor; public class WindsorDependencyResolver : IDependencyResolver { - readonly IWindsorContainer _container; + private readonly IWindsorContainer _container; public WindsorDependencyResolver(IWindsorContainer container) { @@ -16,5 +17,10 @@ public object GetInstance(Type type) { return _container.Resolve(type); } + + public IEnumerable GetInstances() + { + return _container.ResolveAll(); + } } } \ No newline at end of file diff --git a/ShortBus.Windsor/packages.config b/ShortBus.Windsor/packages.config index 9188b4e..55c455e 100644 --- a/ShortBus.Windsor/packages.config +++ b/ShortBus.Windsor/packages.config @@ -1,4 +1,5 @@  + diff --git a/ShortBus.nuspec b/ShortBus.nuspec index 5d1a0a3..e030149 100644 --- a/ShortBus.nuspec +++ b/ShortBus.nuspec @@ -1,8 +1,9 @@ + 1.0.0 - ShortBus community + ShortBus community mhinze https://github.com/mhinze/ShortBus/blob/master/LICENSE.md https://github.com/mhinze/ShortBus @@ -12,12 +13,12 @@ false ShortBus is an in-process mediator with low-friction API ShortBus is an in-process mediator with low-friction API - Copyright Matt Hinze 2013 + Copyright Matt Hinze 2014 - + - - - + + + \ No newline at end of file diff --git a/ShortBus.sln b/ShortBus.sln index 71fd84f..ef050d9 100644 --- a/ShortBus.sln +++ b/ShortBus.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShortBus", "ShortBus\ShortBus.csproj", "{50078B66-DBAC-4273-8DD0-970176B2FE2B}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShortBus.Tests", "ShortBus.Tests\ShortBus.Tests.csproj", "{9EA21775-A0CF-4F6D-82A6-CDFB1817DB3A}" @@ -31,6 +33,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShortBus.Ninject", "ShortBu EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShortBus.Unity", "ShortBus.Unity\ShortBus.Unity.csproj", "{E86D92BC-A5E4-4704-99DB-BBFA42A3C975}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Nuspecs", "Nuspecs", "{52E74E88-C37B-4DA6-940B-D0E22AAA7FD3}" + ProjectSection(SolutionItems) = preProject + ShortBus.Autofac.nuspec = ShortBus.Autofac.nuspec + ShortBus.Markers.nuspec = ShortBus.Markers.nuspec + ShortBus.Ninject.nuspec = ShortBus.Ninject.nuspec + ShortBus.nuspec = ShortBus.nuspec + ShortBus.StructureMap.nuspec = ShortBus.StructureMap.nuspec + ShortBus.Unity.nuspec = ShortBus.Unity.nuspec + ShortBus.Windsor.nuspec = ShortBus.Windsor.nuspec + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/ShortBus.sln.DotSettings b/ShortBus.sln.DotSettings index 771471d..5d131a3 100644 --- a/ShortBus.sln.DotSettings +++ b/ShortBus.sln.DotSettings @@ -1,2 +1,5 @@  - SOLUTION \ No newline at end of file + SOLUTION + False + True + True \ No newline at end of file diff --git a/ShortBus/DependencyResolver.cs b/ShortBus/DependencyResolver.cs index bcdb0a5..4f65314 100644 --- a/ShortBus/DependencyResolver.cs +++ b/ShortBus/DependencyResolver.cs @@ -1,10 +1,13 @@ namespace ShortBus { using System; + using System.Collections.Generic; + using System.Linq; public interface IDependencyResolver { object GetInstance(Type type); + IEnumerable GetInstances(); } public static class DependencyResolver @@ -16,24 +19,32 @@ public static void SetResolver(IDependencyResolver resolver) Current = resolver; } - public static void SetResolver(Func getInstance) + public static void SetResolver(Func getInstance, Func> getInstances) { - Current = new DelegateDependencyResolver(getInstance); + Current = new DelegateDependencyResolver(getInstance, getInstances); } - class DelegateDependencyResolver : IDependencyResolver + private class DelegateDependencyResolver : IDependencyResolver { - readonly Func _getInstance; + private readonly Func _getInstance; + private readonly Func> _getInstances; - public DelegateDependencyResolver(Func getInstance) + public DelegateDependencyResolver(Func getInstance, + Func> getInstances) { _getInstance = getInstance; + _getInstances = getInstances; } public object GetInstance(Type type) { return _getInstance(type); } + + public IEnumerable GetInstances() + { + return _getInstances(typeof (T)).OfType(); + } } } } \ No newline at end of file diff --git a/ShortBus/Handlers.cs b/ShortBus/Handlers.cs new file mode 100644 index 0000000..235d44f --- /dev/null +++ b/ShortBus/Handlers.cs @@ -0,0 +1,24 @@ +namespace ShortBus +{ + using System.Threading.Tasks; + + public interface IRequestHandler where TRequest : IRequest + { + TResponse Handle(TRequest request); + } + + public interface IAsyncRequestHandler where TRequest : IAsyncRequest + { + Task HandleAsync(TRequest request); + } + + public interface INotificationHandler + { + void Handle(TNotification notification); + } + + public interface IAsyncNotificationHandler + { + Task HandleAsync(TNotification notification); + } +} \ No newline at end of file diff --git a/ShortBus/IAsyncCommandHandler.cs b/ShortBus/IAsyncCommandHandler.cs deleted file mode 100644 index f99823a..0000000 --- a/ShortBus/IAsyncCommandHandler.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace ShortBus -{ - using System.Threading.Tasks; - - public interface IAsyncCommandHandler where TCommand : IAsyncCommand - { - Task HandleAsync(TCommand message); - } - - public abstract class AsyncCommandHandler : IAsyncCommandHandler - where TCommand : IAsyncCommand - { - public async Task HandleAsync(TCommand message) - { - await HandleAsyncCore(message); - - return UnitType.Default; - } - - protected abstract Task HandleAsyncCore(TCommand message); - } -} \ No newline at end of file diff --git a/ShortBus/IAsyncQueryHandler.cs b/ShortBus/IAsyncQueryHandler.cs deleted file mode 100644 index 1b95320..0000000 --- a/ShortBus/IAsyncQueryHandler.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace ShortBus -{ - using System.Threading.Tasks; - - public interface IAsyncQueryHandler where TRequest : IAsyncQuery - { - Task HandleAsync(TRequest request); - } -} \ No newline at end of file diff --git a/ShortBus/ICommandHandler.cs b/ShortBus/ICommandHandler.cs deleted file mode 100644 index 812ddc2..0000000 --- a/ShortBus/ICommandHandler.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace ShortBus -{ - public interface ICommandHandler - { - TResult Handle(TCommand command); - } - - public interface ICommandHandler : ICommandHandler { } - - public abstract class CommandHandler : ICommandHandler - { - public UnitType Handle(TCommand command) - { - HandleCore(command); - return UnitType.Default; - } - - protected abstract void HandleCore(TCommand command); - } -} \ No newline at end of file diff --git a/ShortBus/IMediator.cs b/ShortBus/IMediator.cs deleted file mode 100644 index 48fd7e6..0000000 --- a/ShortBus/IMediator.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace ShortBus -{ - using System.Threading.Tasks; - - public interface IMediator - { - Response Request(IQuery query); - Task> RequestAsync(IAsyncQuery query); - - Response Send(ICommand command); - Task> SendAsync(IAsyncCommand command); - } -} \ No newline at end of file diff --git a/ShortBus/IQueryHandler.cs b/ShortBus/IQueryHandler.cs deleted file mode 100644 index 91d7f45..0000000 --- a/ShortBus/IQueryHandler.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ShortBus -{ - public interface IQueryHandler where TRequest : IQuery - { - TResponse Handle(TRequest request); - } -} \ No newline at end of file diff --git a/ShortBus/Mediator.cs b/ShortBus/Mediator.cs index 0d14b6d..81fc6b5 100644 --- a/ShortBus/Mediator.cs +++ b/ShortBus/Mediator.cs @@ -1,9 +1,20 @@ namespace ShortBus { using System; + using System.Collections.Generic; + using System.Linq; using System.Reflection; using System.Threading.Tasks; + public interface IMediator + { + Response Request(IRequest request); + Task> RequestAsync(IAsyncRequest query); + + Response Notify(TNotification notification); + Task NotifyAsync(TNotification notification); + } + public class Mediator : IMediator { private readonly IDependencyResolver _dependencyResolver; @@ -13,15 +24,16 @@ public Mediator(IDependencyResolver dependencyResolver) _dependencyResolver = dependencyResolver; } - public virtual Response Request(IQuery query) + public virtual Response Request(IRequest request) { var response = new Response(); try { - var plan = new MediatorPlan(typeof(IQueryHandler<,>), "Handle", query.GetType(), _dependencyResolver); + var plan = new MediatorPlan(typeof (IRequestHandler<,>), "Handle", request.GetType(), + _dependencyResolver); - response.Data = plan.Invoke(query); + response.Data = plan.Invoke(request); } catch (Exception e) { @@ -31,13 +43,14 @@ public virtual Response Request(IQuery> RequestAsync(IAsyncQuery query) + public async Task> RequestAsync(IAsyncRequest query) { var response = new Response(); try { - var plan = new MediatorPlan(typeof(IAsyncQueryHandler<,>), "HandleAsync", query.GetType(), _dependencyResolver); + var plan = new MediatorPlan(typeof (IAsyncRequestHandler<,>), "HandleAsync", + query.GetType(), _dependencyResolver); response.Data = await plan.InvokeAsync(query); } @@ -49,74 +62,95 @@ public async Task> RequestAsync(IAsyncQue return response; } - public Response Send(ICommand command) + public Response Notify(TNotification notification) { - var response = new Response(); - - try - { - var plan = new MediatorPlan(typeof(ICommandHandler<,>), "Handle", command.GetType(), _dependencyResolver); - - response.Data = plan.Invoke(command); - } - catch (Exception e) - { - response.Exception = e; - } - + var handlers = _dependencyResolver.GetInstances>(); + + var response = new Response(); + List exceptions = null; + + foreach (var handler in handlers) + try + { + handler.Handle(notification); + } + catch (Exception e) + { + (exceptions ?? (exceptions = new List())).Add(e); + } + if (exceptions != null) + response.Exception = new AggregateException(exceptions); return response; } - public async Task> SendAsync(IAsyncCommand command) + public async Task NotifyAsync(TNotification notification) { - var response = new Response(); + var handlers = _dependencyResolver.GetInstances>(); + + return await Task + .WhenAll(handlers.Select(x => notifyAsync(x, notification))) + .ContinueWith(task => + { + var exceptions = task.Result.Where(exception => exception != null).ToArray(); + var response = new Response(); + + if (exceptions.Any()) + { + response.Exception = new AggregateException(exceptions); + } + + return response; + }); + } + private static async Task notifyAsync( + IAsyncNotificationHandler asyncCommandHandler, TNotification message) + { try { - var plan = new MediatorPlan(typeof(IAsyncCommandHandler<,>), "HandleAsync", command.GetType(), _dependencyResolver); - - response.Data = await plan.InvokeAsync(command); + await asyncCommandHandler.HandleAsync(message); } catch (Exception e) { - response.Exception = e; + return e; } - return response; + return null; } - class MediatorPlan + private class MediatorPlan { - readonly MethodInfo HandleMethod; - readonly Func HandlerInstanceBuilder; + private readonly MethodInfo HandleMethod; + private readonly Func HandlerInstanceBuilder; - public MediatorPlan(Type handlerTypeTemplate, string handlerMethodName, Type messageType, IDependencyResolver dependencyResolver) + public MediatorPlan(Type handlerTypeTemplate, string handlerMethodName, Type messageType, + IDependencyResolver dependencyResolver) { - var handlerType = handlerTypeTemplate.MakeGenericType(messageType, typeof(TResult)); + var handlerType = handlerTypeTemplate.MakeGenericType(messageType, typeof (TResult)); HandleMethod = GetHandlerMethod(handlerType, handlerMethodName, messageType); HandlerInstanceBuilder = () => dependencyResolver.GetInstance(handlerType); } - MethodInfo GetHandlerMethod(Type handlerType, string handlerMethodName, Type messageType) + private MethodInfo GetHandlerMethod(Type handlerType, string handlerMethodName, Type messageType) { return handlerType .GetMethod(handlerMethodName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, CallingConventions.HasThis, - new[] { messageType }, + new[] {messageType}, null); } public TResult Invoke(object message) { - return (TResult)HandleMethod.Invoke(HandlerInstanceBuilder(), new[] { message }); + return (TResult) HandleMethod.Invoke(HandlerInstanceBuilder(), new[] {message}); } public async Task InvokeAsync(object message) { - return await (Task)HandleMethod.Invoke(HandlerInstanceBuilder(), new[] { message }); + return await (Task) HandleMethod.Invoke(HandlerInstanceBuilder(), new[] {message}); } } } diff --git a/ShortBus/Properties/AssemblyInfo.cs b/ShortBus/Properties/AssemblyInfo.cs index e7e3518..8936f2b 100644 --- a/ShortBus/Properties/AssemblyInfo.cs +++ b/ShortBus/Properties/AssemblyInfo.cs @@ -1,25 +1,27 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. + [assembly: AssemblyTitle("ShortBus")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("ShortBus")] -[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyCopyright("Copyright © 2014")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. + [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM + [assembly: Guid("476645e5-3a3c-458e-9d0b-0f10a0b40cec")] // Version information for an assembly consists of the following four values: @@ -32,5 +34,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.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/ShortBus/Response.cs b/ShortBus/Response.cs index 36fddcf..449b304 100644 --- a/ShortBus/Response.cs +++ b/ShortBus/Response.cs @@ -2,7 +2,14 @@ namespace ShortBus { using System; - public sealed class Response : Response { } + public sealed class Response : Response + { + public override UnitType Data + { + get { return UnitType.Default; } + set { } + } + } public class Response { diff --git a/ShortBus/ShortBus.csproj b/ShortBus/ShortBus.csproj index 6ac72d7..debfd73 100644 --- a/ShortBus/ShortBus.csproj +++ b/ShortBus/ShortBus.csproj @@ -42,11 +42,7 @@ - - - - - + diff --git a/ShortBus/packages.config b/ShortBus/packages.config index 037b768..8b20c4d 100644 --- a/ShortBus/packages.config +++ b/ShortBus/packages.config @@ -1,3 +1,4 @@  + \ No newline at end of file