From 24ba9480e9497b1484a4105bd8fb7f226f9e87f0 Mon Sep 17 00:00:00 2001 From: "chris.watts90@outlook.com" Date: Fri, 10 Feb 2017 22:40:18 +0000 Subject: [PATCH] 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 --- .../WindowsDataCenter/Configuration.cs | 28 ++++- .../DefaultComponents/DefaultLogger.cs | 109 ++++++++++++++++++ .../WindowsDataCenter/NinjectConfig.xml | 8 ++ .../WindowsDataCenter.csproj | 8 ++ .../WindowsDataCenter/packages.config | 1 + 5 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DefaultComponents/DefaultLogger.cs create mode 100644 DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/NinjectConfig.xml diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Configuration.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Configuration.cs index a011f4f..308290c 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Configuration.cs +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/Configuration.cs @@ -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().To(); - //TODO: move to app.config. or ninject.xml or similar. - kernel.Bind().To(); + + if (File.Exists("NinjectConfig.xml")) + { + kernel.Load("NinjectConfig.xml"); + } + + var logger = kernel.TryGet(); + if (logger == null) + { + kernel.Bind().To(); + logger = kernel.Get(); + } + logger.Fatal("test message - ninject stuff loaded."); + + var repo = kernel.TryGet(); + if (repo == null) + { + logger.Fatal("A type of IRepository must be installed. Exiting."); + throw new ArgumentNullException("IRepository"); + } return kernel; } diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DefaultComponents/DefaultLogger.cs b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DefaultComponents/DefaultLogger.cs new file mode 100644 index 0000000..60520ee --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/DefaultComponents/DefaultLogger.cs @@ -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)); + } + } +} diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/NinjectConfig.xml b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/NinjectConfig.xml new file mode 100644 index 0000000..26f0342 --- /dev/null +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/NinjectConfig.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj index 51e4123..53f9bca 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/WindowsDataCenter.csproj @@ -68,6 +68,10 @@ ..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll True + + ..\packages\Ninject.Extensions.Xml.3.2.0.0\lib\net45-full\Ninject.Extensions.Xml.dll + True + ..\packages\Ninject.Web.Common.3.2.0.0\lib\net45-full\Ninject.Web.Common.dll True @@ -113,6 +117,7 @@ + @@ -134,6 +139,9 @@ + + Always + PreserveNewest diff --git a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config index 9cb5e47..ac0f599 100644 --- a/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config +++ b/DataCenter_Windows/WindowsDataCenter/WindowsDataCenter/packages.config @@ -13,6 +13,7 @@ +