简单注入器和 Web 窗体 - 注入页面、用户控件和母版页的正确方法?
Simple Injector and Web Forms - the correct approach for injecting into Pages, UserControls and MasterPages?
根据关于 WebForms 集成的 Simple Injector 文档,它通过代码示例说,我们应该通过使用 [Import] 属性 属性 注入到我们的页面中。我们通过根据他们的代码示例连接 Global.asax 文件来启用此行为。它确实适用于 Pages。但是,UserControls 或 MasterPages 的文档中没有任何内容。
在搜索 Whosebug 以获得可靠答案时,普遍存在的响应略微过时,并引用了创建 HttpModule 的内容,为在其 git 存储库中找到的示例项目提供了 link(SimpleInjector.Integration.Web.Forms).不过,从 Simple Injector v4.0 开始,该示例项目已失效并从 repo 中剔除。而且它根本不使用 [Import] 属性。绝对令人困惑。
因此,在不清楚如何前进的情况下,我尝试将两者合并以使其正常工作。
我正在使用 Global.asax 引导程序方法,如最新文档中所述,而不是注册新的 HttpModule。我采用了旧 WebForms 集成项目中定义的 container extension methods,并在我的 Bootstrap 中调用它们而不是旧方法。
private static void Bootstrap()
{
var container = new Container();
//container.Options.PropertySelectionBehavior = new ImportAttributePropertySelectionBehavior(); //approach from latest documentation
container.Options.PropertySelectionBehavior = new SimpleInjector.Integration.Web.Forms.WebFormsPropertySelectionBehavior(container.Options.PropertySelectionBehavior); //changed to using WebForms integration way
...
//RegisterWebPages(ref container); //approach from latest documentation
container.RegisterPages(); //changed to using WebForms integration extension methods
}
当我第一次 运行 它时,container.Verify() 抱怨每个页面都实现了 IDisposable 并且它们被注册为 T运行sient(这让我感到困惑,因为原始引导程序似乎也将 Pages 注册为 T运行sient,但 Verify 没有抛出任何错误)。
因此,为了解决这个问题,我将 RegisterPages 扩展方法修改为默认 Lifestyle.Scoped,这消除了验证错误。
private static void RegisterBatchAsConcrete(this Container container, IEnumerable<Type> types)
{
foreach (Type concreteType in types)
{
//container.Register(concreteType); //originally registering Transient
container.Register(concreteType, concreteType, Lifestyle.Scoped);
}
}
它现在似乎可以工作了,至少对于 Pages 是这样。在继续为 UserControls 和 MasterPages 工作之前,我想知道以下问题的答案:
问题
这是正确的方法吗?我是否会因为将 Page、MasterPage 和 UserControl 注册从 T运行sient 更改为 Scoped 生活方式而 运行 陷入问题(性能或其他)?还有其他我没有想到的问题吗?
为什么 Verify 使用 RegisterPages 扩展方法调用与新的 RegisterWebPages 方法调用 T运行isent Lifestyle 有问题?
我真的应该使用 Import 属性吗?新方法使用 ImportAttributePropertySelectionBehavior,而旧方法使用 WebFormsPropertySelectionBehavior
That example project is dead and culled from the repo as of Simple Injector v4.0, though.
SimpleInjector.Integration.Web.Forms)
是很久以前构建的,旨在成为将 Web Forms 与 Simple Injector 集成的解决方案。由于社区缺乏兴趣(即对将 Simple Injector 与 Web Forms 集成感兴趣的开发人员太少),我们决定不投资将其作为 NuGet 包发布、维护它、创建文档、修复错误、支持它等等
由于 Web Forms 是一项遗留技术,我们最终决定从存储库中提取项目,因为我们知道我们始终可以引用较旧的分支。
And it does not utilize the [Import] attribute stuff at all. Definitely confusing.
事实并非如此。 [导入] 也可以工作,但作为文档 describes,您将必须插入您的自定义 ImportAttributePropertySelectionBehavior
.
Why does Verify have a problem with Transient Lifestyle with the RegisterPages extension method call vs. the new RegisterWebPages method?
integration guide for Web Forms 抑制了 DisposableTransientComponent
警告,所以这也是您必须做的。在集成项目中没有发生这种情况的原因是因为该项目是在 v2.x 时间段内创建的,而当时的验证并不那么严格。从那以后该项目从未更新过。
Is this the right approach? Am I going to run into issues (performance, or otherwise) because I changed the Page, MasterPage, and UserControl registration from Transient to Scoped lifestyle? Are there any other gotcha's I'm not thinking of?
您绝对 不 将您的 类 和用户控制器注册为作用域,因为您很快就会 运行 陷入严重的麻烦。特别是对于用户控件,在页面中拥有同一个控件的多个实例是很常见的。将它们注册为 Scoped
会导致同一实例被放置在页面中的多个位置,这显然会造成麻烦(如果它能工作的话)。
因此,您应该明确保留用户控件(可能还有页面)注册为 Transient
,并禁止显示 DisposableTransientComponent
警告。 DisposableTransientComponent
可以被抑制,因为 ASP.NET 将在请求结束时为您处理所有内容。
Should I actually be using the Import attribute, or no? The new approach uses the ImportAttributePropertySelectionBehavior, while the old approach utilizes WebFormsPropertySelectionBehavior
ImportAttributePropertySelectionBehavior
实现 Explicit Property Injection using the [Import]
attribute, while WebFormsPropertySelectionBehavior
implements Implicit Property Injection。
决定使用什么由您决定,但通常应该首选显式 属性 注入,因为隐式 属性 注入(文档描述)有缺点。
根据关于 WebForms 集成的 Simple Injector 文档,它通过代码示例说,我们应该通过使用 [Import] 属性 属性 注入到我们的页面中。我们通过根据他们的代码示例连接 Global.asax 文件来启用此行为。它确实适用于 Pages。但是,UserControls 或 MasterPages 的文档中没有任何内容。
在搜索 Whosebug 以获得可靠答案时,普遍存在的响应略微过时,并引用了创建 HttpModule 的内容,为在其 git 存储库中找到的示例项目提供了 link(SimpleInjector.Integration.Web.Forms).不过,从 Simple Injector v4.0 开始,该示例项目已失效并从 repo 中剔除。而且它根本不使用 [Import] 属性。绝对令人困惑。
因此,在不清楚如何前进的情况下,我尝试将两者合并以使其正常工作。
我正在使用 Global.asax 引导程序方法,如最新文档中所述,而不是注册新的 HttpModule。我采用了旧 WebForms 集成项目中定义的 container extension methods,并在我的 Bootstrap 中调用它们而不是旧方法。
private static void Bootstrap()
{
var container = new Container();
//container.Options.PropertySelectionBehavior = new ImportAttributePropertySelectionBehavior(); //approach from latest documentation
container.Options.PropertySelectionBehavior = new SimpleInjector.Integration.Web.Forms.WebFormsPropertySelectionBehavior(container.Options.PropertySelectionBehavior); //changed to using WebForms integration way
...
//RegisterWebPages(ref container); //approach from latest documentation
container.RegisterPages(); //changed to using WebForms integration extension methods
}
当我第一次 运行 它时,container.Verify() 抱怨每个页面都实现了 IDisposable 并且它们被注册为 T运行sient(这让我感到困惑,因为原始引导程序似乎也将 Pages 注册为 T运行sient,但 Verify 没有抛出任何错误)。
因此,为了解决这个问题,我将 RegisterPages 扩展方法修改为默认 Lifestyle.Scoped,这消除了验证错误。
private static void RegisterBatchAsConcrete(this Container container, IEnumerable<Type> types)
{
foreach (Type concreteType in types)
{
//container.Register(concreteType); //originally registering Transient
container.Register(concreteType, concreteType, Lifestyle.Scoped);
}
}
它现在似乎可以工作了,至少对于 Pages 是这样。在继续为 UserControls 和 MasterPages 工作之前,我想知道以下问题的答案:
问题
这是正确的方法吗?我是否会因为将 Page、MasterPage 和 UserControl 注册从 T运行sient 更改为 Scoped 生活方式而 运行 陷入问题(性能或其他)?还有其他我没有想到的问题吗?
为什么 Verify 使用 RegisterPages 扩展方法调用与新的 RegisterWebPages 方法调用 T运行isent Lifestyle 有问题?
我真的应该使用 Import 属性吗?新方法使用 ImportAttributePropertySelectionBehavior,而旧方法使用 WebFormsPropertySelectionBehavior
That example project is dead and culled from the repo as of Simple Injector v4.0, though.
SimpleInjector.Integration.Web.Forms)
是很久以前构建的,旨在成为将 Web Forms 与 Simple Injector 集成的解决方案。由于社区缺乏兴趣(即对将 Simple Injector 与 Web Forms 集成感兴趣的开发人员太少),我们决定不投资将其作为 NuGet 包发布、维护它、创建文档、修复错误、支持它等等
由于 Web Forms 是一项遗留技术,我们最终决定从存储库中提取项目,因为我们知道我们始终可以引用较旧的分支。
And it does not utilize the [Import] attribute stuff at all. Definitely confusing.
事实并非如此。 [导入] 也可以工作,但作为文档 describes,您将必须插入您的自定义 ImportAttributePropertySelectionBehavior
.
Why does Verify have a problem with Transient Lifestyle with the RegisterPages extension method call vs. the new RegisterWebPages method?
integration guide for Web Forms 抑制了 DisposableTransientComponent
警告,所以这也是您必须做的。在集成项目中没有发生这种情况的原因是因为该项目是在 v2.x 时间段内创建的,而当时的验证并不那么严格。从那以后该项目从未更新过。
Is this the right approach? Am I going to run into issues (performance, or otherwise) because I changed the Page, MasterPage, and UserControl registration from Transient to Scoped lifestyle? Are there any other gotcha's I'm not thinking of?
您绝对 不 将您的 类 和用户控制器注册为作用域,因为您很快就会 运行 陷入严重的麻烦。特别是对于用户控件,在页面中拥有同一个控件的多个实例是很常见的。将它们注册为 Scoped
会导致同一实例被放置在页面中的多个位置,这显然会造成麻烦(如果它能工作的话)。
因此,您应该明确保留用户控件(可能还有页面)注册为 Transient
,并禁止显示 DisposableTransientComponent
警告。 DisposableTransientComponent
可以被抑制,因为 ASP.NET 将在请求结束时为您处理所有内容。
Should I actually be using the Import attribute, or no? The new approach uses the ImportAttributePropertySelectionBehavior, while the old approach utilizes WebFormsPropertySelectionBehavior
ImportAttributePropertySelectionBehavior
实现 Explicit Property Injection using the [Import]
attribute, while WebFormsPropertySelectionBehavior
implements Implicit Property Injection。
决定使用什么由您决定,但通常应该首选显式 属性 注入,因为隐式 属性 注入(文档描述)有缺点。