在使用MVP模式构建的ASP.NET Web表单应用程序上应用依赖注入 [英] Applying Dependency Injection on an ASP.NET web forms application built using MVP Pattern

查看:91
本文介绍了在使用MVP模式构建的ASP.NET Web表单应用程序上应用依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建具有MVP模式的ASP.NET Web窗体应用程序.我的视图结构是这样的:

I am creating an ASP.NET Web forms application with MVP pattern. The structure of my view is something like this:

public partial class ShipperView : System.Web.UI.Page, IShipperView
{
       ShipperPresenter presenter;
       public ShipperOperationsView()
        {
            IShipperOperations operations = new ShipperOperations();
            INavigator navigator = new Navigator();
            presenter = new ShipperPresenter(this,operations,navigator);  //Instantiating presenter
        }
        ...
}

我的演示者的基本结构如下:

The basic structure of my presenter is something like this:

public class ShipperPresenter
{
        IShipper shipperView;
        IShipperOperations operations;
        INavigator navigator;
        public ShipperPresenter(IShipperView view,IShipperOperations operations,INavigator navigator)
        {
            shipperView = view;
            this.operations = operations;
            this.navigator = navigator;
        }
        ...
}

我不想使用new关键字实例化演示者,我想将其替换为解决依赖性.在依赖关系解析期间,我想将当前视图的实例传递给依赖关系解析程序.我试图对此进行大量搜索,但没有得到满意的答复.

I don't want to instantiate my presenter using the new keyword, I would like to replace it with resolving dependency. During the dependency resolution, I want to pass the instance of the present view to the dependency resolver. I tried searching a lot on this but did not get any satisfactory answer.

可以使用任何IoC容器(如StructureMap,Ninject,Unity或MEF)解决此问题吗?任何指针都会有很大帮助.

Can this issue be resolved using any of the IoC containers like StructureMap, Ninject, Unity or MEF? Any pointer would be of great help.

推荐答案

要解决此问题,可以使用属性注入.

To solve this problem you could use property injection.

首先,在DI容器中注册 ShipperOperations 导航器 ShipperPresenter .

First, register ShipperOperations, Navigator and ShipperPresenter in the DI container.

然后,在视图的Page_Load方法中,调用您选择的DI容器的resolve方法.

Then, in the Page_Load method of your view, invoke the resolve method of the DI container of your choice.

public class ShipperPresenter
{
        IShipper shipperView;
        IShipperOperations operations;
        INavigator navigator;
        public ShipperPresenter(IShipperOperations operations,INavigator navigator)
        {
            this.operations = operations;
            this.navigator = navigator;
        }

        public IShipper ShipperView
        {
            get { return shipperView; }
            set { shipperView = value; }
        }
        ...
}

视图将如下所示:

public partial class ShipperView : System.Web.UI.Page, IShipperView
{
       ShipperPresenter presenter;

       protected void Page_Load(object sender, EventArgs e)
       {
           presenter = YourIOC.Resolve<ShipperPresenter>();
           presenter.ShipperView = this;
       }
       ...
}

您还可以使用工厂在运行时创建演示者,同时向其传递您选择的参数.实际上,在DI世界中,当您要实例化具有仅在运行时才知道的依赖关系的对象时,这是继续进行的方法.温莎城堡有一个很好的机制,称为

You could also use a factory to create the presenter at runtime, while passing it the parameters of your choice. In fact, in a DI world, this is THE way to proceed when you want to instantiate objects with dependencies that are only known at runtime. There is a nice mechanism for this in Castle Windsor, it's called typed factories.

使用工厂,无需修改presenter类.而是为工厂创建一个接口:

Using a factory, no need to modify the presenter class. Instead, create an interface for the factory:

public interface IShipperPresenterFactory
{
    ShipperPresenter Create(IShipper shipperView);
}

如果使用Windsor,唯一要做的就是将该接口注册为类型化工厂.对于其他DI容器,您将必须实现一个在内部使用DI容器来解析演示者的类.

If using Windsor, the only thing that you have to do is to register this interface as a typed factory. With other DI containers, you will have to implement a class that uses the DI container internally to resolve the presenter.

该视图的Page_Load方法将使用如下所示的工厂:

The Page_Load method of the view would then use the factory like this:

protected void Page_Load(object sender, EventArgs e)
{
    var factory = YourIOC.Resolve<IShipperPresenterFactory>();
    presenter = factory.Create(this);
}

这篇关于在使用MVP模式构建的ASP.NET Web表单应用程序上应用依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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