Say I have a simple console application:
public static class Program
{
private static ILog Log { get; set; }
public static void Main()
{
Log.Write("Hello, world!");
}
}
What is the simplest way I can use Autofac to inject the Log instance and ensure that the Log property will not be null at run time? The issue is that I can't pass it in through Main() and I'm trying to avoid service location using the container (just because).
Autofac is an open-source dependency injection (DI) or inversion of control (IoC) container developed on Google Code. Autofac differs from many related technologies in that it sticks as close to bare-metal C# programming as possible.
Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .
AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.
What you should do is extract all logic from your main into a class. This class can have a constructor with dependencies. You resolve this class in the main and call it. This class should then be considered to be the entire application. Everything that happens inside the Program
class can now be considered your Composition Root.
// ** Begin composition root
public static class Program
{
public static void Main(string[] args)
{
var container = ConfigureContainer();
var application = container.Resolve<ApplicationLogic>();
application.Run(args); // Pass runtime data to application here
}
private static IContainer ConfigureContainer()
{
var builder = new ContainerBuilder();
builder.RegisterType<ApplicationLogic>.AsSelf();
builder.RegisterType<Log>().As<ILog>();
// Register all dependencies (and dependencies of those dependencies, etc)
return builder.Build();
}
}
// ** End composition root
public class ApplicationLogic
{
private readonly ILog log;
public ApplicationLogic(ILog log) => this.log = log;
public void Run(string[] args) => this.log.Write("Hello, world!");
}
Note that container.Resolve<ApplicationLogic>()
doesn't just resolve the ApplicationLogic
class, it resolves the entire object graph including all of ApplicationLogic
's dependencies, and dependencies of those dependencies, etc. no matter how deep the graph is. The only thing you are responsible for is registering those dependencies in the ConfigureContainer()
method. Therefore, it is somewhat unusual to have any more than 1 Resolve()
method call a console application, and if there are, they should always be called or wired up inside the Composition Root.
Had to change builder.RegisterType.AsSelf(); to builder.RegisterType().AsSelf(); to get this to work for me
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