How to use Autofac for Dependency Injection in ASP.NET MVC

This example will show you how to use autofac in C#.NET

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

You can create a new ASP.NET MVC project, then open the Manage Nuget Packages and install Autofac library

I will create a simple sample allows you access to your database

public class Invoice
{
    public Guid Id { get; set; }
    public string InvoiceId { get; set; }
    public string BuyerId { get; set; }
    public string BuyerName { get; set; }
    public string BuyerAddress { get; set; }
    public string BuyerTaxId { get; set; }
    public decimal Total { get; set; }
    public string Note { get; set; }
    public string PaymentMethod { get; set; }
    public DateTime InvoiceDate { get; set; }
    public List<InvoiceDetails> InvoiceDetails { get; set; }
}
public class InvoiceDetails
{
    public Guid InvoiceId { get; set; }
    public string ItemId { get; set; }
    public string ItemName { get; set; }
    public string Unit { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal Total => (Quantity * Price + Quantity * Price * Vat);
}

Next, We will create a simple interface to interact with your database

public interface IInvoiceService
{
    Guid Create(Invoice obj);
    Invoice GetInvoiceById(Guid id);
}

Let's implement the interface

public class InvoiceRepository : IInvoiceService
{
    public Guid Create(Invoice obj)
    {
        //Add code to store invoice to your database
        return new Guid();
    }

    public Invoice GetInvoiceById(Guid id)
    {
        //Add code to get invoice from your database
        return new Invoice();
    }
}

Create a DataModule class, then override Load method to register your class what you want to inject.

As you see we will use IInvoiceService interface to access our data from database, when we access a method it will be called the method from InvoiceRepository. You can create more repositories then implement IInvoiceService

public class DataModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<InvoiceRepository>().As<IInvoiceService>().InstancePerDependency();
        base.Load(builder);
    }
}

Create a WebModule class, then override Load method to register all controllers

public class WebModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        if (builder == null)
            throw new ArgumentNullException(nameof(builder), "Argument builder can not be null.");
        //The line below tells autofac, when a controller is initialized, pass into its constructor, the implementations of the required interfaces
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        base.Load(builder);
    }
}

Create an AppConfig class to initialize your modules

public class AppConfig
{
    public static void Register()
    {
        var builder = new ContainerBuilder();
        builder.RegisterModule(new DataModule());
        builder.RegisterModule(new WebModule());
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    }
}

Open your Global.aspx, then modify your code as shown below

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AppConfig.Register();
}

This time we will play the demo. You need to create a ApiController or Controller, then add your code as shown below

public class InvoiceController : ApiController
{
    private readonly IInvoiceService _repository;

    public InvoiceController(IInvoiceService repository)
    {
        _repository = repository;
    }

    [HttpPost]
    public IHttpActionResult Create([FromBody]Invoice model)
    {
        try
        {
            return Json(new { result = _repository.Create(model) });
        }
        catch (Exception ex)
        {
            return Json(new { result = ex.Message });
        }
    }

    [HttpGet]
    public IHttpActionResult GetInvoiceById(Guid id)
    {
        try
        {
            return Json(new { result = _repository.GetInvoiceById(id) });
        }
        catch (Exception ex)
        {
            return Json(new { result = ex.Message });
        }
    }
}

or

public class HomeController : Controller
{
    private readonly IInvoiceService _repository;

    public HomeController(IInvoiceService repository)
    {
        _repository = repository;
    }

    public ActionResult Index(Guid id)
    {
        return View(_repository.GetInvoiceById(id));
    }
}

I hope the sample above can help you solve the problem