【发布时间】:2018-08-23 18:48:39
【问题描述】:
我目前有一个案例,我有多个客户,他们对他们想要的通知形式(即电子邮件、传真等)有不同的看法。他们也可能想要一个或多个。所以我创建了一个带有一些基本反射的工厂,它将根据通过客户配置文件发送的一些参数动态创建具体类。我很好奇使用 ASP.Net Core Dependency Injection 是否有更好的方法来做到这一点?我在这里输入了工厂代码,以帮助人们理解我正在尝试做的事情。
客户资料将把他们订阅的服务作为参数发送给 CreateInstances 的字符串数组,这样只会动态创建特定的服务。
public Dictionary<string, Type> Notifications;
public NotificationFactory()
{
LoadTypes();
}
public IEnumerable<INotificationService> CreateInstances(params string[] namesOfServices)
{
var servicesToInstantiate = namesOfServices.ToList();
List<INotificationService> result = new List<INotificationService>();
foreach (var service in servicesToInstantiate)
{
Type serviceName = GetServiceNameToCreate(service.ToLower());
if (serviceName != null)
{
result.Add(Activator.CreateInstance(serviceName) as INotificationService);
}
}
return result;
}
private Type GetServiceNameToCreate(string NotificationClassName)
{
return Notifications.FirstOrDefault(a => a.Key.Contains(NotificationClassName)).Value;
}
protected virtual void LoadTypes()
{
Notifications = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => typeof(INotificationService).IsAssignableFrom(t) && !t.IsInterface)
.ToDictionary(t => t.Name.ToLower(), t => t);
}
【问题讨论】:
-
这可能更适合Code Review。
-
这对我来说还不错。我会考虑将所有服务名称放在一个枚举中,以便将服务名称发送到列表中的手指麻烦的可能性较小。
-
谢谢,伯图斯!这是一个很好的建议。但我不确定核心中的 DI 引擎在实现目标方面是否会比我编写代码做得更好?
-
DI 要求能够几乎完全基于构造函数中的类型来解析实例。如果您有一些静态逻辑,您可以使用工厂方法注册您的类型,但是,在这种情况下,这将有点难以实现(尽管可能并非不可能),因为它基于个人用户偏好。最好还是留在你的工厂这里。
标签: c# dependency-injection asp.net-core-2.0 factory