From 7a6ee7e98dd8e2aa92d057a420bcfdb204c0a5ee Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 17:16:50 +0100 Subject: [PATCH] Plugged in MediatR --- PlexRequests.UI.Tests/app.config | 8 +++ .../Helpers/ContravariantBindingResolver.cs | 65 +++++++++++++++++++ .../NinjectModules/MediatRModule.cs | 52 +++++++++++++++ PlexRequests.UI/PlexRequests.UI.csproj | 14 ++-- PlexRequests.UI/Startup.cs | 20 ++---- PlexRequests.UI/app.config | 8 +++ PlexRequests.UI/packages.config | 5 +- 7 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 PlexRequests.UI/Helpers/ContravariantBindingResolver.cs create mode 100644 PlexRequests.UI/NinjectModules/MediatRModule.cs diff --git a/PlexRequests.UI.Tests/app.config b/PlexRequests.UI.Tests/app.config index 3799f3a1a..be6958e15 100644 --- a/PlexRequests.UI.Tests/app.config +++ b/PlexRequests.UI.Tests/app.config @@ -15,6 +15,14 @@ + + + + + + + + diff --git a/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs b/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs new file mode 100644 index 000000000..f7551d280 --- /dev/null +++ b/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs @@ -0,0 +1,65 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ContravariantBindingResolver.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +using Ninject.Components; +using Ninject.Infrastructure; +using Ninject.Planning.Bindings; +using Ninject.Planning.Bindings.Resolvers; + +namespace PlexRequests.UI.Helpers +{ + public class ContravariantBindingResolver : NinjectComponent, IBindingResolver + { + /// + /// Returns any bindings from the specified collection that match the specified service. + /// + public IEnumerable Resolve(Multimap bindings, Type service) + { + if (service.IsGenericType) + { + var genericType = service.GetGenericTypeDefinition(); + var genericArguments = genericType.GetGenericArguments(); + if (genericArguments.Length == 1 && genericArguments.Single().GenericParameterAttributes.HasFlag(GenericParameterAttributes.Contravariant)) + { + var argument = service.GetGenericArguments().Single(); + var matches = + bindings.Where( + kvp => + kvp.Key.IsGenericType && kvp.Key.GetGenericTypeDefinition() == genericType && kvp.Key.GetGenericArguments().Single() != argument + && kvp.Key.GetGenericArguments().Single().IsAssignableFrom(argument)).SelectMany(kvp => kvp.Value); + return matches; + } + } + + return Enumerable.Empty(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/MediatRModule.cs b/PlexRequests.UI/NinjectModules/MediatRModule.cs new file mode 100644 index 000000000..a63fc4a26 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/MediatRModule.cs @@ -0,0 +1,52 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MediatRModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using MediatR; + +using Ninject; +using Ninject.Extensions.Conventions; +using Ninject.Modules; + +namespace PlexRequests.UI.NinjectModules +{ + public class MediatRModule : NinjectModule + { + public override void Load() + { + Kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllClasses().BindDefaultInterface()); + + Bind().ToMethod(ctx => t => ctx.Kernel.Get(t)); + Bind().ToMethod(ctx => t => ctx.Kernel.GetAll(t)); + + Kernel.Bind(x => x.FromThisAssembly() + .SelectAllClasses() + .InheritedFromAny(typeof(IRequestHandler<,>), typeof(IAsyncRequestHandler<,>)) + .BindDefaultInterfaces()); + + //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllInterfaces().BindAllInterfaces()); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index c4592ef65..150f818c7 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -73,12 +73,16 @@ ..\packages\Nancy.Swagger.0.1.0-alpha3\lib\net40\Nancy.Swagger.dll True - - ..\packages\Ninject.3.0.1.10\lib\net45-full\Ninject.dll + + ..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll True - - ..\packages\Ninject.Extensions.ChildKernel.3.0.0.5\lib\net45-full\Ninject.Extensions.ChildKernel.dll + + ..\packages\Ninject.Extensions.ChildKernel.3.2.0.0\lib\net45-full\Ninject.Extensions.ChildKernel.dll + True + + + ..\packages\Ninject.Extensions.Conventions.3.2.0.0\lib\net45-full\Ninject.Extensions.Conventions.dll True @@ -175,6 +179,7 @@ + @@ -221,6 +226,7 @@ + diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index 43e91815e..2d0bcb221 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -26,9 +26,12 @@ #endregion using System; +using MediatR; + using Nancy.TinyIoc; using Ninject; +using Ninject.Extensions.Conventions; using Ninject.Modules; using Ninject.Planning.Bindings.Resolvers; @@ -54,22 +57,9 @@ namespace PlexRequests.UI var modules = resolver.GetModules(); var kernel = new StandardKernel(modules); - //kernel.Bind(x => x.FromThisAssembly() - // .SelectAllClasses() - // .InheritedFromAny( - // new[] - // { - // typeof(IRequestHandler<,>), - // typeof(IAsyncRequestHandler<,>), - // }) - // .BindDefaultInterfaces()); - - //kernel.Components.Add(); - //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllClasses().BindDefaultInterface()); - //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllInterfaces().BindAllInterfaces()); + kernel.Components.Add(); - //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.Get(t)); - //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.GetAll(t)); + app.UseNancy(options => options.Bootstrapper = new Bootstrapper(kernel)); var scheduler = new Scheduler(); diff --git a/PlexRequests.UI/app.config b/PlexRequests.UI/app.config index 1de7c9330..6cc1ad354 100644 --- a/PlexRequests.UI/app.config +++ b/PlexRequests.UI/app.config @@ -39,6 +39,14 @@ + + + + + + + + diff --git a/PlexRequests.UI/packages.config b/PlexRequests.UI/packages.config index 11c8e9abb..365e6cd83 100644 --- a/PlexRequests.UI/packages.config +++ b/PlexRequests.UI/packages.config @@ -29,8 +29,9 @@ - - + + +