【发布时间】:2015-05-18 02:13:11
【问题描述】:
关于 IDisposable 对象并在此处使用 Autofac,我已经发布了一个一般准则问题:Dependency Injection and IDisposable。不幸的是,我没有考虑我们项目中的一个特定场景,这确实是一个独立的问题,所以在这里提出:
我有一个 Repository 对象,用于管理其中的会话对象的生命周期。因此,Repository 对象是 IDisposable 并销毁会话(Repository 在构造时注入工厂委托,在第一次使用期间实例化会话,如果会话不为空,则在 IDisposable 中销毁会话)。根据上面对 StackOverflow 问题的引用,我了解任何使用我的 Repository 对象注入的对象都不应该实现 IDisposable,因为 Autofac 将处理我的存储库的处置,如果它正在注入它们。
根据提到的 StackOverflow 线程,我已经开始从我的对象中清理 IDisposable 的使用,直到我偶然发现了如下所示的 NotificationPublisher 类。有几个类似的地方,类被注入了 IComponentContext 的实现,它充当工厂。解析是在函数中手动进行的,因为代码库直到运行时才知道需要注入什么处理程序。
public class NotificationPublisher : INotificationPublisher
{
private readonly IComponentContext _container;
private readonly INotificationManager _notificationManager;
public NotificationPublisher(IComponentContext container,
INotificationManager notificationManager)
{
_container = container;
_notificationManager = notificationManager;
}
public IEnumerable<IAlertSubscription> Publish(Account account,
INotificationInitiator owner, INotificationEntity entity,
Int32 severity, CheckCycleContext monitoringContext)
{
var alertSubscriptions =
_notificationManager.GetAlertSubscriptions(account, owner, severity);
foreach (var alertSubscription in alertSubscriptions)
{
var destination = alertSubscription.GetConsumer();
Type handlerType = typeof (INotificationHandler<,>)
.MakeGenericType(entity.GetType(), destination.GetType());
using (var handler =
(INotificationCustomHandler)_container.ResolveOptional(handlerType))
{
if (handler == null) continue;
try
{
Retry.Execute(() => (handler).Send(entity, destination), 3, 500);
monitoringContext.Record(CheckCycleContext.CycleSeverity.Information,
string.Format("NotificationPublisher.Publish:{0}/{1}",
entity.GetType().Name, destination.GetType().Name), "Success");
}
catch (Exception ex)
{
monitoringContext.Record(CheckCycleContext.CycleSeverity.Error,
string.Format("NotificationPublisher.Publish:{0}/{1}",
entity.GetType().Name, destination.GetType().Name), ex.Message, ex,
new {entity, destination});
}
}
}
return alertSubscriptions;
}
}
我假设由于 INotificationCustomHandler 是手动解析的,因此必须使用 using 语句手动处理它,因为 INotificationCustomHandler 的实现注入了 IManager 的实现,而 IManager 的实现注入了 IRepository 的实现。
因此,在这种情况下,我需要在整个代码库中传播 IDisposable,这与我在之前的 SO 问题中提出的建议背道而驰。
如何在需要时通过工厂手动解析对象,而让 Autofac 处理处置?
【问题讨论】:
标签: c# dependency-injection autofac idisposable