Added Ninject and ninject resolvers to enable dependency inject on the ApiController Classes.
This commit is contained in:
parent
a892e1d2c8
commit
a7d23d9e7c
@ -11,6 +11,9 @@ using System.Threading.Tasks;
|
|||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Http.Dependencies;
|
using System.Web.Http.Dependencies;
|
||||||
using Microsoft.Owin.Hosting;
|
using Microsoft.Owin.Hosting;
|
||||||
|
using Ninject;
|
||||||
|
using Ninject.Syntax;
|
||||||
|
using Ninject.Web.Common;
|
||||||
using SQLite.Net;
|
using SQLite.Net;
|
||||||
using SQLite.Net.Attributes;
|
using SQLite.Net.Attributes;
|
||||||
using SQLite.Net.Platform.Win32;
|
using SQLite.Net.Platform.Win32;
|
||||||
@ -84,8 +87,17 @@ namespace WindowsDataCenter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Configuration
|
||||||
{
|
{
|
||||||
|
public static StandardKernel ConfigureNinject(SQLiteConnection conn)
|
||||||
{
|
{
|
||||||
|
var kernel = new StandardKernel();
|
||||||
|
kernel.Bind<SQLiteConnection>().ToConstant(conn);
|
||||||
|
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
|
||||||
|
|
||||||
|
return kernel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class CardUniqueId
|
public sealed class CardUniqueId
|
||||||
{
|
{
|
||||||
@ -140,15 +152,100 @@ namespace WindowsDataCenter
|
|||||||
public List<UserObject> Users { get; set; }
|
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>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ namespace WindowsDataCenter
|
|||||||
//);
|
//);
|
||||||
config.MapHttpAttributeRoutes();
|
config.MapHttpAttributeRoutes();
|
||||||
|
|
||||||
|
config.DependencyResolver = new NinjectDependencyResolver(NinjectHelper.GetInstance().Kernel);
|
||||||
config.Formatters.Insert(0, new JsonpFormatter());
|
config.Formatters.Insert(0, new JsonpFormatter());
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -59,6 +59,16 @@
|
|||||||
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Ninject.Web.Common, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Ninject.Web.Common.3.2.0.0\lib\net45-full\Ninject.Web.Common.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Ninject.Web.WebApi, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\Ninject.Web.WebApi.3.2.4.0\lib\net45-full\Ninject.Web.WebApi.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
|
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
|
||||||
|
|||||||
@ -9,6 +9,9 @@
|
|||||||
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net452" />
|
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net452" />
|
||||||
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net452" />
|
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net452" />
|
||||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
|
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
|
||||||
|
<package id="Ninject" version="3.2.0.0" targetFramework="net452" />
|
||||||
|
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net452" />
|
||||||
|
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net452" />
|
||||||
<package id="Owin" version="1.0" targetFramework="net452" />
|
<package id="Owin" version="1.0" targetFramework="net452" />
|
||||||
<package id="SQLite.Net.Core-PCL" version="3.1.1" targetFramework="net452" />
|
<package id="SQLite.Net.Core-PCL" version="3.1.1" targetFramework="net452" />
|
||||||
<package id="SQLite.Net-PCL" version="3.1.1" targetFramework="net452" />
|
<package id="SQLite.Net-PCL" version="3.1.1" targetFramework="net452" />
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user