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?