2

I would like to use Autofac in console application. It is my really first usage. Before that I was using it in ASP.NET MVC only. In MVC project I can setup Autofac in Global.asax, inject IService to controller and we can say more and less it works. In console application I do it like below:

internal class Program
{
    private static IContainer Container { get; set;}

    private static void Main(string[] args)
    {
         Container = Container.Configure(); // here I have all necessary objects set
         // now I can use it in Main method as:
         using (var scope = Container.BeginLifetimeScope())
         {
             scope.Resolve<ISomething>();
         }
    }
}

As you can see usage of it is simple in just Main method. How about using it in external class? Let say I would like to create class Cat, and inside that use Autofac. should I pass to contructor object Container from class Program? E.g.:

Cat cat = new Cat(Program.Container, "Molly");

Or maybe I should create IContainer inside Cat class?

What is the best solution?

mason
  • 31,774
  • 10
  • 77
  • 121
cube
  • 65
  • 5

2 Answers2

4

Only your console application really needs to know about AutoFac, otherwise you're falling onto the service locator pattern, which is often considered an anti-pattern. Instead, your application should follow this pattern:

//in your console application
using (var scope = Container.BeginLifetimeScope())
{
    IServiceservice = scope.Resolve<IService>();
    service.Execute();
}

class SomeService : IService
{
    readonly ISomeDependency _dependency;

    public SomeService(ISomeDependency dependency)
    {
        _dependency = dependency;
    }

    public void Execute()
    {
        _dependency.DoSomething();
    }
}

interface IService
{
    void Execute();
}

Notice I never actually call a constructor. I make it a habit to never "new up" an object unless that object is just a POCO (contains only data, no logic).

Note that ISomeDependency can itself depends on 0 or more other classes, which it takes via constructor injection. Since AutoFac created the IService, and all of its dependencies, including ISomeDependency, all of ISomeDependency's dependencies will also be initialized, and so forth all the way down. A good video demonstrating this concept is Miguel Castro's Deep Dive into Dependency Injection and Writing Decoupled Quality Code and Testable Software.

mason
  • 31,774
  • 10
  • 77
  • 121
  • mason, thank you for your valuable answer. I would like to extend my problem from first post. As you wrote we can create any object INSIDE main method. What in case I would like to use Autofac in other class? Let's say I have class `public class EmployeeService { public Employee GetEmployee(decimal id) { // here I would like to use some other service: inject using AutoFac. // should I create here again Container object and then: // using (var scope = Container.BeginLifetimeScope()) { ... } } }` – cube Sep 15 '17 at 11:00
  • @cube I don't understand what you're asking now. Yes, you can create an instance of an object via AutoFac and inject things via constructor injection. – mason Sep 15 '17 at 12:13
  • I get confused with my understaning. However you answered on my doubts. Thank you again. – cube Sep 15 '17 at 12:49
0

I created this dotnet core console app template with autofac dependency injection configure, this would save your precious time for some repetitive job everytime setting up a new project. Install via nuget, hope it works well for you.

https://medium.com/@mrkevin.wang/create-a-dotnet-core-cosnole-app-template-with-autofac-dependency-injections-60e09f27df17?source=friends_link&sk=d831e88acc36319e2a43e8c7dac70238

superwalnut
  • 319
  • 5
  • 11