依赖注入:如何在运行时解析多个实例?
Dependency injection : how do i resolve multiple instances at runtime?
我正在使用 Simple Injector
来享受注射乐趣。
据我所知,我只在我的引导程序项目中使用我的容器进行注册和解析。
所以我的控制台应用程序中有这段代码:
static void Main(string[] args)
{
Container container = ComposeRoot();
var controller = container.GetInstance<IBussinesLogicController>();
controller.Execute();
Console.WriteLine("Started");
Console.ReadLine();
}
private static Container ComposeRoot()
{
var container = new Container();
//Bussines Logic controllers.
container.Register<IBussinesLogicController, BussinesLogicController>(Lifestyle.Singleton);
container.Register<IStationController, StationController>(Lifestyle.Transient);
//Data Access controllers.
container.Register<IDataAccessController, DataAccessController>(Lifestyle.Singleton);
container.Register<IDirectoryStructureController, DirectoryStructureController>(Lifestyle.Singleton);
//Shared controllers.
container.Register<ILogger, LoggerController>(Lifestyle.Singleton);
container.Verify();
return container;
}
这将解析我的业务逻辑控制器并调用执行。
现在在执行中,我想解析 IStationController
的 10 个新实例并将它们添加到列表中。
var stations = new List<IStationController>();
for (int i = 0; i < 10; i++)
{
//Resolve IStationController here...
//IStationController station = diContainer.GetInstance<IStationController>();
//station.StationName = $"Station {i}";
//stations.Add(station);
}
我该如何正确执行此操作,因为我的业务逻辑项目没有也不允许引用依赖项容器?
我可以通过将依赖项添加到容器中轻松解决此问题,但恐怕我没有遵循正确的模式。
我的解决方案布局如下:
- Class 图书馆 (DAL)
- Class 库 (BLL) <- IBussinesLogicController
- Class 库(模型)
- Class 库(记录器)
- 控制台应用程序 (ConsoleApp6) <- ComposeRoot()
请告诉我解决这个问题的正确方法是什么。
谢谢!
查看 Simple Injector, have a look at the following link 的文档,其中详细介绍了 ASP.Net MVC 的依赖注入的无缝实现,其中 DI 框架负责解决 运行-时间。我假设您正在使用 MVC 控制器
粘贴相关代码并自定义:
应用程序启动事件
using System.Web.Mvc;
using SimpleInjector;
using SimpleInjector.Integration.Web.Mvc;
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
// 1. Create a new Simple Injector container
var container = new Container();
// 2. Configure the container (register)
// See below for more configuration examples
container.Register<IStationController, StationController>(Lifestyle.Transient);
// 3. Optionally verify the container's configuration.
container.Verify();
// 4. Store the container for use by the application
DependencyResolver.SetResolver(
new SimpleInjectorDependencyResolver(container));
}
}
基本控制器
using System;
using System.Web.Mvc;
public class BusinessLogicController : Controller {
private readonly IStationController stationController;
public BusinessLogicController(IStationController stationController) {
this.stationController = stationController;
}
// Use the IStationController object in various calls
}
重要(来自文档):
在MVC的情况下,第五步(解析实例)是MVC框架的职责。对于每个收到的 Web 请求,MVC 框架将该请求映射到控制器类型,并要求应用程序的 IDependencyResolver 创建该控制器类型的实例。 SimpleInjectorDependencyResolver 的注册(SimpleInjector.Integration.Web.Mvc.dll 的一部分)将确保创建实例的请求被转发到 Simple Injector。 Simple Injector 将创建具有所有嵌套依赖项的控制器。
您可以注册一个 factory delegate 并在您的 class 中使用它。
注册委托如下:
container.Register<IStationController, StationController>(Lifestyle.Transient);
container.RegisterSingleton<Func<IStationController>>(
() => container.GetInstance<IStationController>());
并将其注入到您的 BussinesLogicController
构造函数中:
public BussinesLogicController (Func<IStationController> stationControllerFacrtory)
然后你在循环中使用它:
List<IStationController> stations = new List<IStationController>();
for (int i = 0; i < 10; i++)
{
//Resolve IStationController here...
IStationController station = this.stationControllerFactory.Invoke();
station.StationName = $"Station {i}";
stations.Add(station);
}
我正在使用 Simple Injector
来享受注射乐趣。
据我所知,我只在我的引导程序项目中使用我的容器进行注册和解析。
所以我的控制台应用程序中有这段代码:
static void Main(string[] args)
{
Container container = ComposeRoot();
var controller = container.GetInstance<IBussinesLogicController>();
controller.Execute();
Console.WriteLine("Started");
Console.ReadLine();
}
private static Container ComposeRoot()
{
var container = new Container();
//Bussines Logic controllers.
container.Register<IBussinesLogicController, BussinesLogicController>(Lifestyle.Singleton);
container.Register<IStationController, StationController>(Lifestyle.Transient);
//Data Access controllers.
container.Register<IDataAccessController, DataAccessController>(Lifestyle.Singleton);
container.Register<IDirectoryStructureController, DirectoryStructureController>(Lifestyle.Singleton);
//Shared controllers.
container.Register<ILogger, LoggerController>(Lifestyle.Singleton);
container.Verify();
return container;
}
这将解析我的业务逻辑控制器并调用执行。
现在在执行中,我想解析 IStationController
的 10 个新实例并将它们添加到列表中。
var stations = new List<IStationController>();
for (int i = 0; i < 10; i++)
{
//Resolve IStationController here...
//IStationController station = diContainer.GetInstance<IStationController>();
//station.StationName = $"Station {i}";
//stations.Add(station);
}
我该如何正确执行此操作,因为我的业务逻辑项目没有也不允许引用依赖项容器?
我可以通过将依赖项添加到容器中轻松解决此问题,但恐怕我没有遵循正确的模式。
我的解决方案布局如下:
- Class 图书馆 (DAL)
- Class 库 (BLL) <- IBussinesLogicController
- Class 库(模型)
- Class 库(记录器)
- 控制台应用程序 (ConsoleApp6) <- ComposeRoot()
请告诉我解决这个问题的正确方法是什么。
谢谢!
查看 Simple Injector, have a look at the following link 的文档,其中详细介绍了 ASP.Net MVC 的依赖注入的无缝实现,其中 DI 框架负责解决 运行-时间。我假设您正在使用 MVC 控制器
粘贴相关代码并自定义:
应用程序启动事件
using System.Web.Mvc;
using SimpleInjector;
using SimpleInjector.Integration.Web.Mvc;
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) {
// 1. Create a new Simple Injector container
var container = new Container();
// 2. Configure the container (register)
// See below for more configuration examples
container.Register<IStationController, StationController>(Lifestyle.Transient);
// 3. Optionally verify the container's configuration.
container.Verify();
// 4. Store the container for use by the application
DependencyResolver.SetResolver(
new SimpleInjectorDependencyResolver(container));
}
}
基本控制器
using System;
using System.Web.Mvc;
public class BusinessLogicController : Controller {
private readonly IStationController stationController;
public BusinessLogicController(IStationController stationController) {
this.stationController = stationController;
}
// Use the IStationController object in various calls
}
重要(来自文档):
在MVC的情况下,第五步(解析实例)是MVC框架的职责。对于每个收到的 Web 请求,MVC 框架将该请求映射到控制器类型,并要求应用程序的 IDependencyResolver 创建该控制器类型的实例。 SimpleInjectorDependencyResolver 的注册(SimpleInjector.Integration.Web.Mvc.dll 的一部分)将确保创建实例的请求被转发到 Simple Injector。 Simple Injector 将创建具有所有嵌套依赖项的控制器。
您可以注册一个 factory delegate 并在您的 class 中使用它。
注册委托如下:
container.Register<IStationController, StationController>(Lifestyle.Transient);
container.RegisterSingleton<Func<IStationController>>(
() => container.GetInstance<IStationController>());
并将其注入到您的 BussinesLogicController
构造函数中:
public BussinesLogicController (Func<IStationController> stationControllerFacrtory)
然后你在循环中使用它:
List<IStationController> stations = new List<IStationController>();
for (int i = 0; i < 10; i++)
{
//Resolve IStationController here...
IStationController station = this.stationControllerFactory.Invoke();
station.StationName = $"Station {i}";
stations.Add(station);
}