dependency injection
dependency injection
Real-Time Applications.
dependencies are injected into the class from outside, typically through
constructor parameters.
Let us understand this need for Dependency Injection Design Patterns in ASP.NET
Core Applications with an example.
namespace FirstCoreMVCWebApplication.Models
{
public class Student
{
public int StudentId { get; set; }
public string? Name { get; set; }
public string? Branch { get; set; }
public string? Section { get; set; }
public string? Gender { get; set; }
}
}
--This interface will declare the methods or operations we can perform on the
student data.
--So, open IStudentRepository.cs and copy and paste the following code.
--Here, you can see we have created the interface with two methods.
using System.Collections.Generic;
namespace FirstCoreMVCWebApplication.Models
{
public interface IStudentRepository
{
Student GetStudentById(int StudentId);
List<Student> GetAllStudent();
}
}
Creating Service Implementation:
--Next, create a class file named StudentRepository.cs within the same Models
folder.
--Then open the StudentRepository.cs class file and copy-paste the following code.
--This class implements the IStudentRepository interface by implementing the two
methods declared in the IStudentRepository interface.
--Here, we have hard-coded the student data, but you will get the student data from
a database in real-time applications.
using System.Collections.Generic;
using System.Linq;
namespace FirstCoreMVCWebApplication.Models
{
public class StudentRepository : IStudentRepository
{
public List<Student> DataSource()
{
return new List<Student>()
{
new Student() { StudentId = 101, Name = "James", Branch = "CSE",
Section = "A", Gender = "Male" },
new Student() { StudentId = 102, Name = "Smith", Branch = "ETC",
Section = "B", Gender = "Male" },
new Student() { StudentId = 103, Name = "David", Branch = "CSE",
Section = "A", Gender = "Male" },
new Student() { StudentId = 104, Name = "Sara", Branch = "CSE",
Section = "A", Gender = "Female" },
new Student() { StudentId = 105, Name = "Pam", Branch = "ETC",
Section = "B", Gender = "Female" }
};
}
--Program.cs:
In the Program class, we need to do two things.
--First, we need to configure the required MVC service to the IoC Container,
--and then we need to add the MVC Middleware to the request processing pipeline.
So, modify the Program class as shown below.
namespace FirstCoreMVCWebApplication
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMvc();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
app.Run();
}
}
}
using FirstCoreMVCWebApplication.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace FirstCoreMVCWebApplication.Controllers
{
public class HomeController : Controller
{
public JsonResult Index()
{
StudentRepository repository = new StudentRepository();
List<Student> allStudentDetails = repository.GetAllStudent();
return Json(allStudentDetails);
}
It is the most commonly used design pattern nowadays to remove the dependencies
between objects, allowing us to develop loosely coupled software components.
--
The ASP.NET Core Framework is designed from scratch to provide built-in support for
dependency injection design patterns. It injects dependency objects into a class
through a constructor or method using the built-in IoC (Inversion of Control)
container. The built-in IoC (Inversion of Control) container is represented by
IServiceProvider implementation, which supports default constructor injection. The
types (i.e., classes) managed by built-in IoC containers are called services.
Advertisements
Framework Services: Services that are a part of the ASP.NET Core Framework, such as
IApplicationBuilder, IHostingEnvironment, ILoggerFactory, etc.
Application Services: The services (custom types or classes) you create as a
programmer for your application.