Service layer method inject using Autofac in a .NET Console Application
in this article, I’m going to show how to inject the entity framework’s backend service layer method using Autofac in a .NET console application.
Below diagram showing this application structure overview.
This is the dataset using for the demonstrations.
Class diagram for a sample table
Sample Table Class File
here I’m going to use a service class file that is extending its interface, service files connection diagram.
Sample Service Interface
Sample Service Implementation
in order to work with the entity framework, we would have DBContext.
so IContext file is like the following.
TestDBContext will be generated like the following, once follow the above DB structure I mentioned.
from here on, we focus on the path to inject the service layer method.
So we can place the following ServiceModule class file in Console Application end.
we can build up EFModule class which is extending Module class
as usual, in inject class, we can invoke the service method as following
then in our Program class Main method we can refer to the above initialize class as following.
Now everything is set up, Once run the project we should be able to see the following output in a console app.
Please note that this approach works Entity Framework Version 6.4 upwards and Autofac version 5.2 upwords only.