How can I implement Ninject or DI on asp.net Web Forms?(如何在 asp.net Web 窗体上实现 Ninject 或 DI?)
问题描述
有很多例子可以让它在 MVC 应用程序上运行.它是如何在 Web 表单上完成的?
There are plenty of examples for having it worked on an MVC application. How is it done on Web Forms?
推荐答案
以下是在 WebForms 中使用 Ninject 的步骤.
Here are the steps to use Ninject with WebForms.
第 1 步 - 下载
需要两个下载 - Ninject-2.0.0.0-release-net-3.5 和 WebForm 扩展 Ninject.Web_1.0.0.0_With.log4net(有一个 NLog 替代方案).
There are two downloads required - Ninject-2.0.0.0-release-net-3.5 and the WebForm extensions Ninject.Web_1.0.0.0_With.log4net (there is an NLog alternative).
Web 应用程序中需要引用以下文件:Ninject.dll、Ninject.Web.dll、Ninject.Extensions.Logging.dll 和 Ninject.Extensions.Logging.Log4net.dll.
The following files need to be referenced in the web application: Ninject.dll, Ninject.Web.dll, Ninject.Extensions.Logging.dll and Ninject.Extensions.Logging.Log4net.dll.
第 2 步 - Global.asax
Global 类需要从 Ninject.Web.NinjectHttpApplication
派生并实现 CreateKernel()
,从而创建容器:
The Global class needs to derive from Ninject.Web.NinjectHttpApplication
and implement CreateKernel()
, which creates the container:
using Ninject; using Ninject.Web;
namespace Company.Web {
public class Global : NinjectHttpApplication
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new YourWebModule());
return kernel;
}
StandardKernel
构造函数采用 Module
.
第 3 步 - 模块
模块,在本例中为 YourWebModule
,定义了 Web 应用程序需要的所有绑定:
The Module, in this case YourWebModule
, defines all the bindings the web application will need:
using Ninject;
using Ninject.Web;
namespace Company.Web
{
public class YourWebModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<ICustomerRepository>().To<CustomerRepository>();
}
在此示例中,无论在何处引用 ICustomerRepository
接口,都会使用具体的 CustomerRepository
.
In this example, wherever the ICustomerRepository
interface is referenced the concrete CustomerRepository
will be used.
第 4 步 - 页面
完成后,每个页面都需要从 Ninject.Web.PageBase
继承:
Once that's done each page needs to inherit from Ninject.Web.PageBase
:
using Ninject;
using Ninject.Web;
namespace Company.Web
{
public partial class Default : PageBase
{
[Inject]
public ICustomerRepository CustomerRepo { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Customer customer = CustomerRepo.GetCustomerFor(int customerID);
}
InjectAttribute -[Inject]
- 告诉 Ninject 将 ICustomerRepository
注入 CustomerRepo 属性.
The InjectAttribute -[Inject]
- tells Ninject to inject ICustomerRepository
into the CustomerRepo Property.
如果你已经有一个基本页面,你只需要让你的基本页面派生自 Ninject.Web.PageBase.
If you already have a base page you just need to get your base page to derive from the Ninject.Web.PageBase.
第 5 步 - 母版页
不可避免地,您将拥有母版页,并且要允许 MasterPage 访问注入的对象,您需要从 Ninject.Web.MasterPageBase
派生母版页:
Inevitably, you'll have master pages, and to allow a MasterPage to access injected objects you'll need to derive your master page from Ninject.Web.MasterPageBase
:
using Ninject;
using Ninject.Web;
namespace Company.Web
{
public partial class Site : MasterPageBase
{
#region Properties
[Inject]
public IInventoryRepository InventoryRepo { get; set; }
第 6 步 - 静态 Web 服务方法
下一个问题是无法注入静态方法.我们有一些 Ajax PageMethods,它们显然是静态的,所以我不得不将这些方法转移到一个标准的 Web 服务中.同样,Web 服务需要派生自 Ninject 类 - Ninject.Web.WebServiceBase
:
The next problem was not being able to inject into static methods. We had a few Ajax PageMethods, which are obviously static, so I had to move the methods into a standard web service. Again, the web service needs to derive from a Ninject class - Ninject.Web.WebServiceBase
:
using Ninject;
using Ninject.Web;
namespace Company.Web.Services
{
[WebService(Namespace = "//tempuri.org/">http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class YourWebService : WebServiceBase
{
#region Properties
[Inject]
public ICountbackRepository CountbackRepo { get; set; }
#endregion
[WebMethod]
public Productivity GetProductivity(int userID)
{
CountbackService _countbackService =
new CountbackService(CountbackRepo, ListRepo, LoggerRepo);
在您的 JavaScript 中,您需要引用标准服务 - Company.Web.Services.YourWebService.GetProductivity(user, onSuccess)
,而不是 PageMethods.GetProductivity(user, onSuccess)
.
In your JavaScript you'll need to reference the standard service - Company.Web.Services.YourWebService.GetProductivity(user, onSuccess)
, rather than PageMethods.GetProductivity(user, onSuccess)
.
我发现的唯一其他问题是将对象注入用户控件.虽然可以使用 Ninject 功能创建您自己的基本 UserControl,但我发现将属性添加到所需对象的用户控件并在容器页面中设置属性会更快.我认为支持开箱即用的 UserControls 在 Ninject待办事项"列表中.
The only other problem I found was injecting objects into User Controls. While it's possible to create your own base UserControl with Ninject capabilities, I found it quicker to add a Property to the user control for the required object and setting the Property in the container page. I think supporting UserControls out of the box is on the Ninject "to-do" list.
添加 Ninject 非常简单,它是一个雄辩的 IoC 解决方案.很多人喜欢它是因为没有 XML 配置.它还有其他有用的技巧",例如仅使用 Ninject 语法将对象转换为单例 - Bind
.无需将 WebLogger 更改为实际的 Singleton 实现,我喜欢这样.
Adding Ninject is quite simple and it is an eloquent IoC solution. Many people like it because there is no Xml configuration. It has other useful "tricks" such as turning objects into Singletons with just the Ninject syntax - Bind<ILogger>().To<WebLogger>().InSingletonScope()
. There is no need to change WebLogger into an actual Singleton implmentation, I like this.
这篇关于如何在 asp.net Web 窗体上实现 Ninject 或 DI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 asp.net Web 窗体上实现 Ninject 或 DI?
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01