Donate

Using Microsoft.Extensions.DependencyInjection In .NET 5 or .NET Core Console Application

Team,

Here's how to incorporate the Microsoft.Extensions.DependencyInjection namespace and it's ServiceProvider class in a .NET 5 or .NET Core Console Application project. If you're an ASP.NET Core MVC developer, you might recognize how it is applied in Startup.cs for Dependency Injection specifically ConfigureServices() method wherein the AddTransient() method is utilized to map an interface class and it's implementation type. To begin with, lets by creating a .NET 5 Console application project and follow the steps below.

Project Setup

1. Add the Microsoft.Extensions.DependencyInjection package to your project via NuGet.
Using Microsoft.Extensions.DependencyInjection In .NET 5 or .NET Core Console Application
2. Create three folders, new classes and interfaces based from the image below.
Using Microsoft.Extensions.DependencyInjection In .NET 5 or .NET Core Console Application

Models

Add two model classes specifically Employee and Product with three properties for each class.
Product.cs
public class Product
   {
      public int ProductCode { get; set; }
      public string ProductName { get; set; }
      public double ProductPrice { get; set; }

      public Product()
      {
         ProductCode = 0;
         ProductName = string.Empty;
         ProductPrice = 0;
      }
   }
Employee.cs
public class Employee
   {
      public int EmployeeID { get; set; }
      public string EmployeeName { get; set; }
      public double EmployeeSalary { get; set; }

      public Employee()
      {
         EmployeeID = 0;
         EmployeeName = string.Empty;
         EmployeeSalary = 0;
      }
   }

Repository

In your Repository folder, add the interface that defines the contract for Product and Employee and the classes that implements those contracts. This tutorial does not include methods that perform database operations. The emphasis is how Dependency Injection works using Microsoft's Microsoft.Extensions.DependencyInjection namespace.
IProductDataAccess.cs
public interface IProductDataAccess
   {
      public void SaveProduct(Product product);
      public string ShowProduct();
   }
ProductDataAccess.cs
public class ProductDataAccess : IProductDataAccess
   {
      public Product Product { get; set; }

      public ProductDataAccess()
      {
         Product = new Product();
      }

      public void SaveProduct(Product product)
      {
         /* In real-world, this can be a method to save the object to database */
         this.Product = product;
      }

      public string ShowProduct()
      {
         /* In real-world, this can be a method to get an employee object from the database */
         return $"Code - {Product.ProductCode}, Name - {Product.ProductName}, Price - {Product.ProductPrice}";
      }
   }
IEmployeeDataAccess.cs
 public interface IEmployeeDataAccess
   {
      public void SaveEmployee(Employee employee);
      public string ShowEmployee();
   }
EmployeeDataAccess.cs
public class EmployeeDataAccess : IEmployeeDataAccess
   {
      public Employee Employee { get; set; }

      public EmployeeDataAccess()
      {
         Employee = new Employee();
      }

      public void SaveEmployee(Employee employee)
      {
         /* In real-world, this can be a method to save the object to database */
         this.Employee = employee;
      }

      public string ShowEmployee()
      {
         /* In real-world, this can be a method to get an employee object from the database */
         return $"ID - {Employee.EmployeeID}, Name - {Employee.EmployeeName}, Salary - {Employee.EmployeeSalary}";
      }
   }

DependencyInjectionResolver.cs Class

The DependencyInjectionResolver.cs class handles the dependency injection between the service type and it's implementation. In the constructor, you can see that the AddTransient() method maps the specified types say IEmployeeDataAccess and it's implementation type which is the EmployeeDataAccess class. You have the option to add more transient service to the ServiceCollection class as long as the implementation type which is the class must implement the interface. In this class, there are two methods that retrieves the service from the ServiceProvider that returns an interface type. This will be called in your main application or another class.
public class DependencyInjectionResolver
   {
      private ServiceProvider serviceProvider;

      public DependencyInjectionResolver()
      {
         serviceProvider = new ServiceCollection()
            .AddTransient<IEmployeeDataAccess, EmployeeDataAccess>()
            .AddTransient<IProductDataAccess, ProductDataAccess>()
            .BuildServiceProvider();
      }
      
      public IEmployeeDataAccess GetEmployeeRepository()
      {         
         return serviceProvider.GetService<IEmployeeDataAccess>();
      }

      public IProductDataAccess GetProductRepository()
      {
         return serviceProvider.GetService<IProductDataAccess>();
      }
   }

ManageInformationService.cs Class

The ManageInformationService class acts like a middle-tier that performs the database operation and calls the methods of the interface through the DependencyInjectionResolver class. This class has two methods which separates the processing of Employee and Product information.
public class ManageInformationService
   {
      private static DependencyInjectionResolver resolver;

      public ManageInformationService()
      {
         resolver = new DependencyInjectionResolver();
      }

      public void ManageEmployee()
      {
         Employee employee;
         IEmployeeDataAccess empAccess;

         employee = new Employee();
         empAccess = resolver.GetEmployeeRepository();

         employee.EmployeeID = 10001;
         employee.EmployeeName = "Greg Esguerra";
         employee.EmployeeSalary = 500.00;

         empAccess.SaveEmployee(employee);
         Console.WriteLine("Employee Information");
         Console.WriteLine(empAccess.ShowEmployee());
      }

      public void ManageProduct()
      {
         Product product;
         IProductDataAccess prodAccess;

         product = new Product();
         prodAccess = resolver.GetProductRepository();

         product.ProductCode = 2550;
         product.ProductName = "Pringles Potato Chips";
         product.ProductPrice = 500.00;

         prodAccess.SaveProduct(product);
         Console.WriteLine("\n\nProduct Information");
         Console.WriteLine(prodAccess.ShowProduct());
      }
   }

Program.cs Class

The main program creates an instance of the ManageInformationService class and then calls the ManageEmployee and ManageProduct functions. When you run the application, it should show the output below.
class Program
   {     
      static void Main(string[] args)
      {
         ManageInformationService manageInformationService;

         manageInformationService = new ManageInformationService();

         manageInformationService.ManageEmployee();
         manageInformationService.ManageProduct();

         Console.ReadLine();
      }
   }
Output
Using Microsoft.Extensions.DependencyInjection In .NET 5 or .NET Core Console Application


Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.