January 2, 2020

Using Autofac for Azure Table Storage with C# ASP.Net Core MVC 3.0

Azure Table Storage DI

Using Azure Table Storage is a very cost effective way to run a cloud hosted database in Microsoft Azure. The question is, how to we manage the DI.

Enter “Autofac”

We’ve of course all heard of Autofac, so I won’t explain the benefits of their framework.

Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

Implementation Steps

  • Create our data model.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Create our CRUD repository and inject an Azure CloudTable into the constructor.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Create our CRUD service layer and inject the repository into the constructor.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Implement a module to configure Azure Table Storage using Autofac’s DI layer, in four steps -
  1. Create an Azure Storage Account:
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  1. Configure our service layer and our Azure Cloud Table:
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  1. Configure our repository to use our Azure Cloud Table:
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Configure Startup.cs file to use the Azure Table Storage Autofac module we’ve built.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Configure Program.cs file to use Autofac.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • Utilise our CRUD service at the controller level in ASP.Net MVC Core 3.0.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

  • View our Azure Table Storage NoSQL database to verify our implementation.
Autofac Azure Storage C# ASP.Net MVC Core 3.0

Autofac Azure Storage C# ASP.Net MVC Core 3.0

Enjoy!