【发布时间】:2014-02-12 17:50:58
【问题描述】:
我有一些现有的 MDI WinForms 代码,它使用 Windsor 来解析这样的子表单:
private void Show<T>() where T : IMdiChildView
{
var view = _windsorContainer.Resolve<T>();
view.Closed += (sender, args) => _windsorContainer.Release(view);
view.Show(this);
}
视图注册一个 IWindsorInstaller 像这样:
public class ViewInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromAssemblyContaining(typeof(IView)).BasedOn(typeof(IView))
.WithServiceAllInterfaces().LifestylePooled(1));
}
}
LifestylePooled(1) 的原因是当子视图打开时,应该只有一个。这可能是使用LifestylePooled 的错误方式? LifestyleTransient 不会发生这种情况,但是当存在一个表单时,我们不希望创建任何新表单,并且在处理每个表单时,它们只是坐在容器中占用内存。
最简单的视图没有其他依赖项,但是当我们关闭它并随后再次调用Show<T>() 方法时,Windsor 会返回已处理的表单,当 MdiParent 尝试显示它时会导致 ObjectDisposedException。
我开始认为在这个项目中使用 Windsor 是有缺陷的(我已经认识到它被用作 ServiceLocator),但即使不推荐这种特殊用法,这种行为似乎也很难解释。
【问题讨论】:
标签: c# castle-windsor ioc-container