gpt4 book ai didi

c# - 如何使用 Simple Injector 注册 Windows 窗体

转载 作者:太空狗 更新时间:2023-10-30 00:18:20 27 4
gpt4 key购买 nike

背景

我正在构建一个 winforms 应用程序,我在其中使用 IoC 容器 (SimpleInjector) 来注册我的类型。在我的应用程序中,大多数屏幕(即表单)在任何给定时间都只有一个实例。

问题

对于在任何给定时间只需要一个实例的表单,我可以将它们注册为单例:

container.Register<IHomeView, HomeView>(Lifestyle.Singleton);

这让我可以使用容器来跟踪所有表单。然而,在这种情况下,当表单关闭时,它将被释放(表单实现 IDisposable)。如果应用程序尝试使用容器再次打开该表单,则容器的表单实例将被丢弃,并引发异常。

问题

处理这个问题的正确方法是什么?我目前看到两种解决方案:

  1. 对于每个表单,重写关闭表单而不是隐藏表单,而不是实际关闭它。我真的不喜欢这个主意。我觉得我宁愿每次都关闭表格并从一个新的/新鲜的表格开始。
  2. 注册具有短暂生活方式的表单,而不是作为单例。在这种情况下,容器实际上更像是一个工厂。我遇到了两个问题:a) 我失去了通过容器跟踪表单的能力,并且 b) 容器在验证期间抛出异常,说一次性类型不应该注册为 transient (我不明白为什么会这样).这些问题都适用于我同时需要多个实例的表单。

我可以通过在验证期间抑制诊断警告来解决问题 b)。

registration = container.GetRegistration(typeof(ILoginView)).Registration;
registration.SuppressDiagnosticWarning(
DiagnosticType.DisposableTransientComponent,
"Winforms registration supression.");

在这里采取的正确方法是什么?我错过了什么吗?

最佳答案

理想情况下,您希望将表单注册为 Singleton。然而,根据我的经验,这将导致难以调试错误,尤其是当您使用 BindingSource 时。将您的数据绑定(bind)到任何东西。

使用 Singleton 作为生活方式的第二个问题是,如果您的应用程序使用无模式窗口,则该窗口在第二次打开时将抛出 ObjectDisposedException,因为 Windows Forms 应用程序框架将在第一次关闭时处理 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'。并显示一条消息:“创建窗口句柄时出错”。这实际上意味着您耗尽了计算机的所有资源。

因此,处理表格很重要。而且,如果您使用 Scope,Simple Injector 也可以完成这项工作。 ,这对于 Windows Forms 来说不太容易实现。因此,您必须自己处理使用 ShowDialog() 显示的关闭窗体。

根据您的具体用例,有多种方法可以实现 FormOpenerNavigationService。一种方法:

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>();
}
}

这个类必须注册为Singleton:

container.RegisterSingleton<IFormOpener, FormOpener>();

并且可以通过将此服务注入(inject)到例如您的应用程序的根表单中来使用:

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
}
}

关于c# - 如何使用 Simple Injector 注册 Windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com