我想解决多个类的IEnumerable集合的依赖关系,继承控制器上的接口。
我希望在应用程序启动期间解决以下依赖关系:
var notificationStrategy = new NotificationStrategy(
new INotificationService[]
{
new TextNotificationService(), // <-- inject any dependencies here
new EmailNotificationService() // <-- inject any dependencies here
});NotificationStragey
public class NotificationStrategy : INotificatonStrategy
{
private readonly IEnumerable<INotificationService> notificationServices;
public NotificationStrategy(IEnumerable<INotificationService> notificationServices)
{
this.notificationServices = notificationServices ?? throw new ArgumentNullException(nameof(notificationServices));
}
}在不使用任何外部依赖项或库的情况下,在IEnumerable核心中注入asp.net类型的对象的最佳方法是什么?
发布于 2021-06-11 16:58:16
将所有类型注册到复合根目录的服务集合中。
//...
services.AddScoped<INotificationService, TextNotificationService>();
services.AddScoped<INotificationService, EmailNotificationService>();
services.AddScoped<INotificatonStrategy, NotificationStrategy>();
//...在解析所需类型时,应该注入所有依赖项,因为构造函数已经将IEnumerable<INotificationService>作为构造函数参数。
https://stackoverflow.com/questions/67940785
复制相似问题