Dependency Injection (DI) is a pattern that allows objects to be loosely coupled by removing the responsibility of creating and managing dependencies from a class and delegating it to an external entity. In .NET, this pattern is commonly implemented using frameworks like ASP.NET Core or third-party libraries like Autofac or Ninject.
Here's an example implementation of DI in C# using the ASP.NET Core framework:
// Service interface
public interface IMyService
{
void DoSomething();
}
// Service implementation
public class MyService : IMyService
{
public void DoSomething()
{
Console.WriteLine("Doing something...");
}
}
// Controller class with dependency injection
public class MyController : ControllerBase
{
private readonly IMyService _service;
public MyController(IMyService service)
{
_service = service;
}
public IActionResult Index()
{
_service.DoSomething();
return View();
}
}
// Startup class with DI configuration
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IMyService, MyService>();
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
In this implementation, we have an interface IMyService that defines the methods of a service, a MyService class that implements IMyService, and a MyController class that depends on IMyService.
In the MyController class constructor, we use dependency injection to inject the IMyService instance into the controller, so that we can use it in the Index action method.
In the Startup class, we use the ConfigureServices method to configure the dependency injection container, adding the MyService implementation as a singleton service and adding the MyController to the services collection.
Finally, in the Configure method, we configure the ASP.NET Core middleware to use routing and map the default controller route.
To use the controller, you can simply create an instance of MyController in the Index action method of an ASP.NET Core controller:
public class HomeController : Controller
{
private readonly MyController _myController;
public HomeController(MyController myController)
{
_myController = myController;
}
public IActionResult Index()
{
_myController.Index();
return View();
}
}
In this example, we create an instance of MyController using dependency injection in the constructor of the HomeController class, and call the Index action method of MyController from the Index action method of the HomeController.
This example demonstrates how the Dependency Injection Pattern can be used to loosely couple objects and delegate the responsibility of creating and managing dependencies to an external entity, allowing for easier testing, maintenance, and scalability of the code.
Here's an example implementation of DI in C# using the ASP.NET Core framework:
// Service interface
public interface IMyService
{
void DoSomething();
}
// Service implementation
public class MyService : IMyService
{
public void DoSomething()
{
Console.WriteLine("Doing something...");
}
}
// Controller class with dependency injection
public class MyController : ControllerBase
{
private readonly IMyService _service;
public MyController(IMyService service)
{
_service = service;
}
public IActionResult Index()
{
_service.DoSomething();
return View();
}
}
// Startup class with DI configuration
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IMyService, MyService>();
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
In this implementation, we have an interface IMyService that defines the methods of a service, a MyService class that implements IMyService, and a MyController class that depends on IMyService.
In the MyController class constructor, we use dependency injection to inject the IMyService instance into the controller, so that we can use it in the Index action method.
In the Startup class, we use the ConfigureServices method to configure the dependency injection container, adding the MyService implementation as a singleton service and adding the MyController to the services collection.
Finally, in the Configure method, we configure the ASP.NET Core middleware to use routing and map the default controller route.
To use the controller, you can simply create an instance of MyController in the Index action method of an ASP.NET Core controller:
public class HomeController : Controller
{
private readonly MyController _myController;
public HomeController(MyController myController)
{
_myController = myController;
}
public IActionResult Index()
{
_myController.Index();
return View();
}
}
In this example, we create an instance of MyController using dependency injection in the constructor of the HomeController class, and call the Index action method of MyController from the Index action method of the HomeController.
This example demonstrates how the Dependency Injection Pattern can be used to loosely couple objects and delegate the responsibility of creating and managing dependencies to an external entity, allowing for easier testing, maintenance, and scalability of the code.
0 comments:
Post a Comment
Please do not enter any spam link in the message box.