I have a static class which calls a static Logger class,
e.g
static class DoesStuffStatic { public static void DoStuff() { try { //something } catch(Exception e) { //do stuff; Logger.Log(e); } } } static class Logger { public static void Log(Exception e) { //do stuff here } }
How do I inject the Logger into my static class?
Note: I've read Dependency Injection in .NET with examples?, but this seems to use an instance logger.
You can use dependency injection in a static class using method or property injection. However, you cannot use constructor injection in a static class because the constructor of a static class cannot accept any parameters.
Both Constructor Injection and Property Injection are applied inside the startup path of the application (a.k.a. the Composition Root) and require the consumer to store the dependency in a private field for later reuse. This requires the constructor and property to be instance members, i.e. non-static.
Using dependency injection, we can pass an instance of class C to class B, and pass an instance of B to class A, instead of having these classes to construct the instances of B and C. In the example, below, class Runner has a dependency on the class Logger.
A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the . NET Class Library, the static System.
This is not necessarily so. As long as your static logger exposes a method for:
Here's an example. Take the following class for DI:
public class Logger : ILogger { public void Log(string stringToLog) { Console.WriteLine(stringToLog); } } public interface ILogger { void Log(string stringToLog); }
And here's our static class which needs a logger:
public static class SomeStaticClass { private static IKernel _diContainer; private static ILogger _logger; public static void Init(IKernel dIcontainer) { _diContainer = dIcontainer; _logger = _diContainer.Get<ILogger>(); } public static void Log(string stringToLog) { _logger.Log(stringToLog); } }
Now, in a global startup for your app (in this case, in my global.asax.cs), you can instantiate your DI Container, then hand that off to your static class.
public class Global : Ninject.Web.NinjectHttpApplication { protected override IKernel CreateKernel() { return Container; } static IKernel Container { get { var standardKernel = new StandardKernel(); standardKernel.Bind<ILogger>().To<Logger>(); return standardKernel; } } void Application_Start(object sender, EventArgs e) { SomeStaticClass.Init(Container); SomeStaticClass.Log("Dependency Injection with Statics is totally possible"); }
And presto! You are now up and running with DI in your static classes.
Hope that helps someone. I am re-working an application which uses a LOT of static classes, and we've been using this successfully for a while now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With