changed Configuration to bind ninject assemblies from xml file using Ninject.Extentions.Xml

ninject will load DefaultLogger if no logger is found. if no IRepository is found, will exception.
created DefaultLogger which will just log to Debug.WriteLine
#5
This commit is contained in:
chris.watts90@outlook.com 2017-02-10 22:40:18 +00:00
parent f73fb9d345
commit 24ba9480e9
5 changed files with 151 additions and 3 deletions

View File

@ -1,4 +1,8 @@
using System.Web;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web;
using Interfaces;
using Ninject;
using Ninject.Web.Common;
@ -11,8 +15,26 @@ namespace WindowsDataCenter
{
var kernel = new StandardKernel();
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
//TODO: move to app.config. or ninject.xml or similar.
kernel.Bind<IRepository>().To<SQLiteRepository.SQLiteRepository>();
if (File.Exists("NinjectConfig.xml"))
{
kernel.Load("NinjectConfig.xml");
}
var logger = kernel.TryGet<ILogger>();
if (logger == null)
{
kernel.Bind<ILogger>().To<DefaultComponents.DefaultLogger>();
logger = kernel.Get<ILogger>();
}
logger.Fatal("test message - ninject stuff loaded.");
var repo = kernel.TryGet<IRepository>();
if (repo == null)
{
logger.Fatal("A type of IRepository must be installed. Exiting.");
throw new ArgumentNullException("IRepository");
}
return kernel;
}

View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Interfaces;
namespace WindowsDataCenter.DefaultComponents
{
public class DefaultLogger : ILogger
{
public bool IsDebugEnabled { get { return true; } }
public bool IsErrorEnabled { get { return true; } }
public bool IsFatalEnabled { get { return true; } }
public bool IsInfoEnabled { get { return true; } }
public bool IsTraceEnabled { get { return true; } }
public bool IsWarnEnabled { get { return true; } }
public void Debug(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Debug(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Debug(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
public void Error(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Error(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Error(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
public void Fatal(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Fatal(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Fatal(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
public void Info(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Info(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Info(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
public void Trace(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Trace(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Trace(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
public void Warn(Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception);
}
public void Warn(string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(format, args));
}
public void Warn(Exception exception, string format, params object[] args)
{
System.Diagnostics.Debug.WriteLine(string.Format(exception + ", " + format, args));
}
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<module name="NinjectAssemblies">
<bind service="Interfaces.ILogger, Interfaces"
to="NLogLogger.NLogger, NLogLogger" scope="singleton" />
<bind service="Interfaces.IRepository, Interfaces"
to="SQLiteRepository.SQLiteRepository, SQLiteRepository" />
</module>

View File

@ -68,6 +68,10 @@
<HintPath>..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ninject.Extensions.Xml, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<HintPath>..\packages\Ninject.Extensions.Xml.3.2.0.0\lib\net45-full\Ninject.Extensions.Xml.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>
@ -113,6 +117,7 @@
<Compile Include="Controllers\CardsController.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="Controllers\TimelogController.cs" />
<Compile Include="DefaultComponents\DefaultLogger.cs" />
<Compile Include="Helpers\CacheControlAttribute.cs" />
<Compile Include="JsonpFormatter.cs" />
<Compile Include="NinjectDependencyResolver.cs" />
@ -134,6 +139,9 @@
<Compile Include="Controllers\ValuesController.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="NinjectConfig.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="www\css\bootstrap-grid.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View File

@ -13,6 +13,7 @@
<package id="Microsoft.Owin.StaticFiles" version="3.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.Extensions.Xml" 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" />