在注射presenter低层依赖在ASP.NET应用程序MVP [英] Injecting Lower Layer Dependency in Presenter in an ASP.NET MVP Application

查看:113
本文介绍了在注射presenter低层依赖在ASP.NET应用程序MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我读<一个href=\"http://haacked.com/archive/2006/08/09/ASP.NETSupervisingControllerModelView$p$psenterFromSchematicToUnitTestsTo$c$c.aspx\"相对=nofollow>菲尔哈克的帖子在那里,他给实施模型 - 视图 - presenter为ASP.NET的一个例子。其中code代码片段显示了code的视图类。

I recently read Phil Haack's post where he gives an example of implementing Model View Presenter for ASP.NET. One of the code snippets shows how the code for the view class.

public partial class _Default : System.Web.UI.Page, IPostEditView
{    
    PostEditController controller;
    public _Default()
    {
         this.controller = new PostEditController(this, new BlogDataService());
    }
}

然而,这里的景色构建BlogDataService的实例,并将它传递给presenter。理想情况下,而不应该知道BlogDataService或任何presenter的下层依赖。但我也preFER保持BlogDataService作为构造注入的presenter的依赖,因为它使presenter的依赖关系明确。

However, here the view constructs the instance of the BlogDataService and passes it along to the presenter. Ideally the view should not know about BlogDataService or any of the presenter's lower layer dependencies. But i also prefer to keep the BlogDataService as a constructor injected dependency of the presenter as it makes the dependencies of the presenter explicit.

这同样的问题已经被这里问及计算器

This same question has been asked on stackoverflow here.

答案之一提出使用服务定位器,以获得BlogDataService的实例,并传递给它沿至presenter的constructor.This溶液然而不能解决知道关于BlogDataService视图的问题和需要明确得到它的参考。

One of the answers suggests using a service locator to get the instance of the BlogDataService and passing it along to the presenter's constructor.This solution however does not solve the problem of the view knowing about the BlogDataService and needing to explicitly get a reference to it.

有没有一种方法使用一个IoC或者DI容器工具自动构建presenter对象,这样的观点并没有对付明确创建BlogDataService对象,也注入了看法和服务实例到presenter的构造。我preFER以尽可能使用构造注入模式。

Is there a way to automatically construct the presenter object using an IoC or DI container tool such that the view does not have to deal with explicitly creating the BlogDataService object and also injecting the view and service instances into the presenter's constructor. I prefer to use the constructor injection pattern as far as possible.

还是有更好的设计可用来解决这个问题?能有实现这个更好的方式,如果我建立一个WinForms应用程序,而不是一个ASP.NET应用程序的WebForms?

Or is there a better design available to solve the problem?. Can there be a better way to implement this If i am building a WinForms application instead of a ASP.NET WebForms application?

感谢您的任何反馈。

推荐答案

是的,有。例如,在Web窗体构造函数使用StructureMap:

Yes there is. For example using StructureMap in a webform constructor:

 public partial class AttributeDetails : EntityDetailView<AttributeDetailPresenter>, IAttributeDetailView
    {
 public AttributeDetails()
        {
            _presenter = ObjectFactory.With<IAttributeDetailView>(this).GetInstance<AttributeDetailPresenter>();
        }

....
}

和,你可以在这里看到presenter需要视图和服务注入

and as you can see here presenter needs view and service injected

 public AttributeDetailPresenter(IAttributeDetailView view, IAttributeService attributeService)
        {
            MyForm = view;
            AppService = attributeService;
        }

您也可以使用StructureMap的积聚功能对于web表单,这样就可以避免直接在视图中使用的ObjectFactory。

You can also use StructureMap BuildUp feature for webforms so that you can avoid using ObjectFactory directly in your view.

这篇关于在注射presenter低层依赖在ASP.NET应用程序MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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