Dependency Injection using Autofac
Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity(GitHub-Autofac 2019).
Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more(autofac.org 2019).
닷넷 프레임워크를 사용하다 보면 의존성 주입이란 단어가 자주 등장한다. ASP.NET Core MVC, WPF MVVM 그리고 Caliburn.Micro에서 Ioc.Get<T>() 에서 DI를 사용하는 예를 볼 수 있는 데 이번 글에서는 일반적인 개발환경에서 DI를 자동으로 그리고 쉽게 사용할 수 있는 Autofac 패키지의 사용법을 간단하게 작성해보았다. 참고 : 일반적인 DI, Interface 사용법 보기
메인 프로그램
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Program.cs
using Autofac;
using System;
namespace ConsoleUI;
class Program
{
static void Main()
{
var container = ContainerConfig.Configure();
using(var scope = container.BeginLifetimeScope())
{
var app = scope.Resolve<IApplication>();
app.Execute();
}
Console.ReadLine();
}
}