How to Use Dependency Injection Dotnet Core Easy
- Updated date Jun 08, 2022
- 4.7k
- 3
In this blog, you will learn about dependency Injection and Different ways to inject it without using constructor using .NET Core API.
In this blog, we are going to discuss dependency injection and its usage and benefits. Also, discuss different ways to implement Dependency Injection. Prerequisites The purpose behind the dependency injection design patterns Now we are looking what is the problem we have faced without using Dependency Injection. Suppose we have class Car and that depends on BMW class. Here in this example class Car depends on BMW class and it is tightly coupled because of that each time we need to create an object of BMW class. In the future suppose we want to add some parameter into BMW Class Constructor like ModelName as shown in the below example So, in this case, we need to add the ModelName parameter into the Car class and again we need to change the constructor and pass the parameter. This is not a good practice in software development and because of that there is a hard dependency created and when our application is large then it's hard to maintain. These are the challenges we face while implementing functionality without Dependency Injection. .NET Core provides a mechanism like IOC Container that will respond to take care of the following things. Scope Singleton Transient Now we are going to create .NET Core Product Application using Dependency Injection one by one. Then configure the project Provide additional information like .NET Version and other configuration Then, install the following NuGet Packages which we need for Swagger, SQL Server, Migration, and Database Update. Now, Create the ProductDetail Class inside the Model Folder Later on, Create DBContextClass for database related operation of entity framework Add the Database Connection string of SQL Server in the appsetting.json file Create the ProductService and IProductService Classes inside Service Folder for data manipulation using Dependency Injection After this, Create ProductController and inside that, you can see we inject product service into the constructor easily without being tightly coupled. So it helps to extend the functionality of the Product without modifying existing functionality. Finally, Register the service inside the ConfigureServices method and also add Db Provider and configure the Database connection string which we put in the app settings. Our Project structure looks like as shown in the image Finally, we are going to create a database and migrations using Entity Framework. So for that open the Package Manager Console in Visual Studio and enter the following command for migration one by one. Now, run the Product Application and open the swagger dashboard and use the endpoints. There are also following different ways to inject the DI without Controller Constructor Method 1 Method 2 Method 3 So, this is all about Dependency Injection. I hope you understand. Happy Coding!
Dependency Injection
public class Car { private BMW _bmw; public Car() { _bmw = new BMW(); } } public class BMW { public BMW() {} }
public class Car { private BMW _bmw; public Car() { _bmw = new BMW("BMW X1"); } } public class BMW { public BMW(string ModelName) {} }
Uses of Dependency Injection in .NET Core
Ways to register Lifetime of Services in startup class
using System.ComponentModel.DataAnnotations; namespace Product.Model { public class ProductDetail { [Key] public int Id { get; set; } public string Name { get; set; } public int Cost { get; set; } public int NoOfStock { get; set; } } }
using Microsoft.EntityFrameworkCore; using Product.Model; namespace Product.Data { public class DBContextClass : DbContext { public DBContextClass(DbContextOptions<DBContextClass> options) : base(options) { } public DbSet<ProductDetail> Products { get; set; } } }
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { "DefaultConnection": "Data Source=ServerName;Initial Catalog=ProductData;User Id=Test;Password=database@123;" } }
using Product.Model; using System.Collections.Generic; namespace Product.Service { public interface IProductService { ProductDetail AddProduct(ProductDetail employee); List < ProductDetail > GetProducts(); void UpdateProduct(ProductDetail employee); void DeleteProduct(int Id); ProductDetail GetProduct(int Id); } }
using Product.Data; using Product.Model; using System.Collections.Generic; using System.Linq; namespace Product.Service { public class ProductService: IProductService { private readonly DBContextClass _context; public ProductService(DBContextClass context) { _context = context; } public ProductDetail AddProduct(ProductDetail product) { _context.Products.Add(product); _context.SaveChanges(); return product; } public void DeleteProduct(int Id) { var product = _context.Products.FirstOrDefault(x => x.Id == Id); if (product != null) { _context.Remove(product); _context.SaveChanges(); } } public ProductDetail GetProduct(int Id) { return _context.Products.FirstOrDefault(x => x.Id == Id); } public List < ProductDetail > GetProducts() { return _context.Products.OrderBy(a => a.Name).ToList(); } public void UpdateProduct(ProductDetail product) { _context.Products.Update(product); _context.SaveChanges(); } } }
using Microsoft.AspNetCore.Mvc; using Product.Model; using Product.Service; using System.Collections.Generic; namespace Product.Controllers { [Route("api/[controller]")] [ApiController] public class ProductController : ControllerBase { private readonly IProductService productService; public ProductController(IProductService _productService) { productService = _productService; } [HttpGet] [Route("api/Product/GetProducts")] public IEnumerable<ProductDetail> GetProducts() { return productService.GetProducts(); } [HttpPost] [Route("api/Product/AddProduct")] public IActionResult AddProduct(ProductDetail product) { productService.AddProduct(product); return Ok(); } [HttpPut] [Route("api/Product/UpdateProduct")] public IActionResult UpdateProduct(ProductDetail product) { productService.UpdateProduct(product); return Ok(); } [HttpDelete] [Route("api/Product/DeleteProduct")] public IActionResult DeleteProduct(int id) { var existingProduct = productService.GetProduct(id); if (existingProduct != null) { productService.DeleteProduct(existingProduct.Id); return Ok(); } return NotFound($"Product Not Found with ID : {existingProduct.Id}"); } [HttpGet] [Route("GetProduct")] public ProductDetail GetProduct(int id) { return productService.GetProduct(id); } } }
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Product.Data; using Product.Service; namespace Product { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //DBContext Configuration services.AddDbContext < DBContextClass > (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); //Register the ProductService for DI purpose services.AddScoped < IProductService, ProductService > (); //enable swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Product", Version = "v1" }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Product v1")); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
Add-Migration "FirstMigration" database-update
[HttpGet] [Route("api/Product/GetProducts")] public IEnumerable<ProductDetail> GetProducts() { //method 1 var services = this.HttpContext.RequestServices; var productService = (IProductService)services.GetService(typeof(IProductService)); return productService.GetProducts(); }
[HttpPost] [Route("api/Product/AddProduct")] public IActionResult AddProduct(ProductDetail product) { //Method 2 var productService = (IProductService)this.HttpContext.RequestServices.GetService(typeof(IProductService)); productService.AddProduct(product); return Ok(); }
//Method 3 public IActionResult UpdateProduct([FromServices] IProductService productService, ProductDetail product) { productService.UpdateProduct(product); return Ok(); }
Source: https://www.c-sharpcorner.com/blogs/dependency-injection-using-net-core-api
0 Response to "How to Use Dependency Injection Dotnet Core Easy"
Postar um comentário