如何使用 Simple Injector 注册 Windows 表单
How to register Windows Forms with Simple Injector
背景
我正在构建一个 winforms 应用程序,我在其中使用 IoC 容器 (SimpleInjector) 来注册我的类型。在我的应用程序中,大多数屏幕(即表单)在任何给定时间都只有一个实例。
问题
对于在任何给定时间只需要一个实例的表单,我可以将它们注册为单例:
container.Register<IHomeView, HomeView>(Lifestyle.Singleton);
这让我可以使用容器来跟踪所有表单。然而,在这种情况下,当表单关闭时,它将被释放(表单实现 IDisposable)。如果应用程序尝试使用容器再次打开该表单,则容器的表单实例将被释放,并引发异常。
问题
处理这个问题的正确方法是什么?我目前看到两种解决方案:
- 对于每个表单,重写关闭表单而不是隐藏表单,而不是实际关闭它。我真的不喜欢这个主意。我觉得我宁愿每次都关闭表格并从 new/fresh 表格开始。
- 以短暂的生活方式而不是作为单身人士注册表格。在这种情况下,容器实际上更像是一个工厂。我 运行 遇到两个问题:a) 我失去了通过容器跟踪表单的能力,并且 b) 容器在验证期间抛出异常,说一次性类型不应该注册为瞬态(我没有明白这是为什么)。这些问题都适用于我同时需要多个实例的表单。
我可以通过在验证过程中抑制诊断警告来解决问题 b)。
registration = container.GetRegistration(typeof(ILoginView)).Registration;
registration.SuppressDiagnosticWarning(
DiagnosticType.DisposableTransientComponent,
"Winforms registration supression.");
这里正确的方法是什么?我错过了什么吗?
理想情况下,您希望将表单注册为 Singleton
。然而,根据我的经验,这将导致难以调试错误,尤其是当您使用 BindingSource
将数据绑定到任何内容时。
使用 Singleton
作为生活方式的第二个问题是,如果您的应用程序使用无模式 windows,则此 windows 将在第二次打开时抛出 ObjectDisposedException
,因为 Windows Forms Application 框架将在第一次关闭时处理 Form,而 Simple Injector 应该负责。因此,如果注册为 Singleton,Simple Injector 将创建一个且恰好是一个实例。如果其他人(例如您的应用程序,windows 表单框架)将处置该对象,则不会重新创建它。
最简单的解决方案,也很容易理解,就是将您的表单注册为 Transient
。是的,您需要抑制诊断警告。根据 documentation 出现此诊断警告的原因:
A component that implements IDisposable
would usually need deterministic clean-up but Simple Injector does not implicitly track and dispose components registered with the transient lifestyle.
Simple Injector 无法释放临时组件,因为它无法确定何时释放对象。但是,这意味着通过调用 .ShowDialog()
以模态方式打开的表单将永远不会被释放!由于 windows 表单应用程序通常会运行很长时间,甚至可能一周或一个月,这最终会导致 'Win32Exception' 显示一条消息:"Error Creating Window Handle"。这实质上意味着您耗尽了计算机的所有资源。
因此,处理表格很重要。虽然如果您使用 Scope,Simple Injector 能够完成这项工作,但使用 Windows 表单并不容易实现。因此,您必须自己处理使用 ShowDialog()
.
显示的已关闭表单
根据您的具体用例,有多种方法可以实现 FormOpener
或 NavigationService
。一种方法:
public interface IFormOpener
{
void ShowModelessForm<TForm>() where TForm : Form;
DialogResult ShowModalForm<TForm>() where TForm : Form;
}
public class FormOpener : IFormOpener
{
private readonly Container container;
private readonly Dictionary<Type, Form> openedForms;
public FormOpener(Container container)
{
this.container = container;
this.openedForms = new Dictionary<Type, Form>();
}
public void ShowModelessForm<TForm>() where TForm : Form
{
Form form;
if (this.openedForms.ContainsKey(typeof(TForm)))
{
// a form can be held open in the background, somewhat like
// singleton behavior, and reopened/reshown this way
// when a form is 'closed' using form.Hide()
form = this.openedForms[typeof(TForm)];
}
else
{
form = this.GetForm<TForm>();
this.openedForms.Add(form.GetType(), form);
// the form will be closed and disposed when form.Closed is called
// Remove it from the cached instances so it can be recreated
form.Closed += (s, e) => this.openedForms.Remove(form.GetType());
}
form.Show();
}
public DialogResult ShowModalForm<TForm>() where TForm : Form
{
using (var form = this.GetForm<TForm>())
{
return form.ShowDialog();
}
}
private Form GetForm<TForm>() where TForm : Form
{
return this.container.GetInstance<TForm>();
}
}
这个class必须注册为Singleton
:
container.RegisterSingleton<IFormOpener, FormOpener>();
并且可以通过在例如应用程序的根表单中注入此服务来使用:
public partial class RootForm : Form
{
private readonly IFormOpener formOpener;
public RootForm(IFormOpener formOpener)
{
this.formOpener = formOpener;
this.InitializeComponent();
}
private void ShowCustomers_Click(object sender, EventArgs e)
{
this.formOpener.ShowModelessForm<AllCustomersForm>();
}
private void EditCustomer_Click(object sender, EventArgs e)
{
var result = this.formOpener.ShowModalForm<EditCustomerForm>();
// do something with result
}
}
背景
我正在构建一个 winforms 应用程序,我在其中使用 IoC 容器 (SimpleInjector) 来注册我的类型。在我的应用程序中,大多数屏幕(即表单)在任何给定时间都只有一个实例。
问题
对于在任何给定时间只需要一个实例的表单,我可以将它们注册为单例:
container.Register<IHomeView, HomeView>(Lifestyle.Singleton);
这让我可以使用容器来跟踪所有表单。然而,在这种情况下,当表单关闭时,它将被释放(表单实现 IDisposable)。如果应用程序尝试使用容器再次打开该表单,则容器的表单实例将被释放,并引发异常。
问题
处理这个问题的正确方法是什么?我目前看到两种解决方案:
- 对于每个表单,重写关闭表单而不是隐藏表单,而不是实际关闭它。我真的不喜欢这个主意。我觉得我宁愿每次都关闭表格并从 new/fresh 表格开始。
- 以短暂的生活方式而不是作为单身人士注册表格。在这种情况下,容器实际上更像是一个工厂。我 运行 遇到两个问题:a) 我失去了通过容器跟踪表单的能力,并且 b) 容器在验证期间抛出异常,说一次性类型不应该注册为瞬态(我没有明白这是为什么)。这些问题都适用于我同时需要多个实例的表单。
我可以通过在验证过程中抑制诊断警告来解决问题 b)。
registration = container.GetRegistration(typeof(ILoginView)).Registration;
registration.SuppressDiagnosticWarning(
DiagnosticType.DisposableTransientComponent,
"Winforms registration supression.");
这里正确的方法是什么?我错过了什么吗?
理想情况下,您希望将表单注册为 Singleton
。然而,根据我的经验,这将导致难以调试错误,尤其是当您使用 BindingSource
将数据绑定到任何内容时。
使用 Singleton
作为生活方式的第二个问题是,如果您的应用程序使用无模式 windows,则此 windows 将在第二次打开时抛出 ObjectDisposedException
,因为 Windows Forms Application 框架将在第一次关闭时处理 Form,而 Simple Injector 应该负责。因此,如果注册为 Singleton,Simple Injector 将创建一个且恰好是一个实例。如果其他人(例如您的应用程序,windows 表单框架)将处置该对象,则不会重新创建它。
最简单的解决方案,也很容易理解,就是将您的表单注册为 Transient
。是的,您需要抑制诊断警告。根据 documentation 出现此诊断警告的原因:
A component that implements
IDisposable
would usually need deterministic clean-up but Simple Injector does not implicitly track and dispose components registered with the transient lifestyle.
Simple Injector 无法释放临时组件,因为它无法确定何时释放对象。但是,这意味着通过调用 .ShowDialog()
以模态方式打开的表单将永远不会被释放!由于 windows 表单应用程序通常会运行很长时间,甚至可能一周或一个月,这最终会导致 'Win32Exception' 显示一条消息:"Error Creating Window Handle"。这实质上意味着您耗尽了计算机的所有资源。
因此,处理表格很重要。虽然如果您使用 Scope,Simple Injector 能够完成这项工作,但使用 Windows 表单并不容易实现。因此,您必须自己处理使用 ShowDialog()
.
根据您的具体用例,有多种方法可以实现 FormOpener
或 NavigationService
。一种方法:
public interface IFormOpener
{
void ShowModelessForm<TForm>() where TForm : Form;
DialogResult ShowModalForm<TForm>() where TForm : Form;
}
public class FormOpener : IFormOpener
{
private readonly Container container;
private readonly Dictionary<Type, Form> openedForms;
public FormOpener(Container container)
{
this.container = container;
this.openedForms = new Dictionary<Type, Form>();
}
public void ShowModelessForm<TForm>() where TForm : Form
{
Form form;
if (this.openedForms.ContainsKey(typeof(TForm)))
{
// a form can be held open in the background, somewhat like
// singleton behavior, and reopened/reshown this way
// when a form is 'closed' using form.Hide()
form = this.openedForms[typeof(TForm)];
}
else
{
form = this.GetForm<TForm>();
this.openedForms.Add(form.GetType(), form);
// the form will be closed and disposed when form.Closed is called
// Remove it from the cached instances so it can be recreated
form.Closed += (s, e) => this.openedForms.Remove(form.GetType());
}
form.Show();
}
public DialogResult ShowModalForm<TForm>() where TForm : Form
{
using (var form = this.GetForm<TForm>())
{
return form.ShowDialog();
}
}
private Form GetForm<TForm>() where TForm : Form
{
return this.container.GetInstance<TForm>();
}
}
这个class必须注册为Singleton
:
container.RegisterSingleton<IFormOpener, FormOpener>();
并且可以通过在例如应用程序的根表单中注入此服务来使用:
public partial class RootForm : Form
{
private readonly IFormOpener formOpener;
public RootForm(IFormOpener formOpener)
{
this.formOpener = formOpener;
this.InitializeComponent();
}
private void ShowCustomers_Click(object sender, EventArgs e)
{
this.formOpener.ShowModelessForm<AllCustomersForm>();
}
private void EditCustomer_Click(object sender, EventArgs e)
{
var result = this.formOpener.ShowModalForm<EditCustomerForm>();
// do something with result
}
}