我怎样才能在asp.net Web窗体实现Ninject或DI? [英] How can I implement Ninject or DI on asp.net Web Forms?

查看:118
本文介绍了我怎样才能在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?

推荐答案

下面是使用Ninject与WebForms的步骤。

Here are the steps to use Ninject with WebForms.

第一步 - 下载

有需要两个下载 - Ninject-2.0.0.0释放净3.5 和WebForm的扩展<一个HREF =htt​​ps://github.com/ninject/ninject.web/downloads> 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中

全局类需要从 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 构造函数采用模块

第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 - [注入] - 告诉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步 - 主网页

不可避免地,你就会有母版页,并允许一个母版访问注入你需要从 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; }     

第六步 - 静态Web服务方法

接下来的问题是不能够注入静态方法。我们有几个阿贾克斯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(用户的onSuccess),而不是 PageMethods.GetProductivity(用户的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能力你自己的基本用户控件,我发现它更快地将属性添加到用户控制所需的对象,并在容器页面设置属性。我认为,用户控件支持开箱是在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语法 - 绑定&LT; ILogger&GT;()为&lt; WebLogger&GT;()InSingletonScope()。有没有必要改变WebLogger为实际辛格尔顿implmentation,我喜欢这个。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆