moved NinjectDependencyResolver, NinjectDependencyScope and NinjectHelper to their own files

This commit is contained in:
chris.watts90@outlook.com 2017-01-31 22:00:40 +00:00
parent bc1947a31f
commit 8585f0fe77
4 changed files with 103 additions and 96 deletions

View File

@ -0,0 +1,20 @@
using System.Web.Http.Dependencies;
using Ninject;
namespace WindowsDataCenter
{
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
readonly IKernel _kernel;
public NinjectDependencyResolver(IKernel kernel) : base(kernel)
{
_kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(_kernel.BeginBlock());
}
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
using Ninject;
using Ninject.Syntax;
namespace WindowsDataCenter
{
// Provides a Ninject implementation of IDependencyScope
// which resolves services using the Ninject container.
public class NinjectDependencyScope : IDependencyScope
{
IResolutionRoot _resolver;
public NinjectDependencyScope(IResolutionRoot resolver)
{
_resolver = resolver;
}
public object GetService(Type serviceType)
{
if (_resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return _resolver.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (_resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return _resolver.GetAll(serviceType);
}
public void Dispose()
{
IDisposable disposable = _resolver as IDisposable;
disposable?.Dispose();
_resolver = null;
}
}
}

View File

@ -0,0 +1,39 @@
using System.Collections.Generic;
using Ninject;
namespace WindowsDataCenter
{
public class NinjectHelper
{
private static NinjectHelper _instance;
private static readonly object LockObject = new object();
public StandardKernel Kernel { get; private set; }
public static NinjectHelper GetInstance()
{
if (_instance != null)
return _instance;
lock (LockObject)
{
_instance = new NinjectHelper();
return _instance;
}
}
private NinjectHelper()
{
Kernel = Configuration.ConfigureNinject();
}
public T Get<T>()
{
return Kernel.Get<T>();
}
public IEnumerable<T> GetAll<T>()
{
return Kernel.GetAll<T>();
}
}
}

View File

@ -152,100 +152,4 @@ namespace WindowsDataCenter
public List<UserObject> Users { get; set; }
}
// Provides a Ninject implementation of IDependencyScope
// which resolves services using the Ninject container.
public class NinjectDependencyScope : IDependencyScope
{
IResolutionRoot resolver;
public NinjectDependencyScope(IResolutionRoot resolver)
{
this.resolver = resolver;
}
public object GetService(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return resolver.TryGet(serviceType);
}
public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return resolver.GetAll(serviceType);
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
}
// This class is the resolver, but it is also the global scope
// so we derive from NinjectScope.
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
IKernel kernel;
public NinjectDependencyResolver(IKernel kernel) : base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}
public class NinjectHelper
{
private static NinjectHelper _instance;
private static object _lockObject = new object();
private StandardKernel _kernel;
public static NinjectHelper GetInstance()
{
if (_instance == null)
{
throw new ArgumentNullException();
}
return _instance;
}
public static NinjectHelper GetInstance(SQLiteConnection dbconn)
{
if (_instance != null)
return _instance;
lock (_lockObject)
{
_instance = new NinjectHelper(dbconn);
return _instance;
}
}
private NinjectHelper(SQLiteConnection conn)
{
_kernel = Configuration.ConfigureNinject(conn);
}
public StandardKernel Kernel { get { return _kernel; } private set { _kernel = value; } }
public T Get<T>()
{
return _kernel.Get<T>();
}
public IEnumerable<T> GetAll<T>()
{
return _kernel.GetAll<T>();
}
}
}