ASP.NET MVP注射服务的依赖性 [英] ASP.NET MVP Injecting Service Dependency

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

问题描述

我有一个实现我的观点,并在此页面constuctor的presenter一个ASP.NET页面。 <一href=\"http://haacked.com/archive/2006/08/09/ASP.NETSupervisingControllerModelView$p$psenterFromSchematicToUnitTestsTo$c$c.aspx\"相对=nofollow>菲尔哈克的职位提供作为出发点,我会刚刚从岗位的例子来说明这个问题。

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. Phil Haack's post providing was used as the starting point, and I'll just the examples from the post to illustrate the question.

public partial class _Default : System.Web.UI.Page, IPostEditView {

    PostEditController controller;
    public _Default()
    {
         this.controller = new PostEditController(this, new BlogDataService());
    }
}

什么是注入BlogDataService实例的最佳方法?这些例子我在标有其注入框架解析属性的依赖Page类中使用的属性。

What is the best approach to inject the instance of the BlogDataService? The examples I have found use properties in the Page class for the dependency marked with an attribute which the injection framework resolves.

不过,我使用的构造方法用于测试目的preFER。

However, I prefer using the constructor approach for testing purposes.

有没有人有输入或者链接到上述的良好实现。我想preFER Ninject,但只要StructureMap或温莎将罚款作为它的流畅。

Does anyone have input or perhaps links to good implementations of the above. I would prefer Ninject, but StructureMap or Windsor would be fine as long as its fluent.

感谢您的任何反馈。

推荐答案

如果您使用微软的ServiceLocator ,您可以将服务定位器设计模式并问容器的服务。

If you use the Microsoft ServiceLocator, you can apply the service locator design pattern and ask the container for the service.

在你的情况下,它会是这个样子:

In your case it would look something like this:

public partial class _Default : System.Web.UI.Page, IPostEditView {

    PostEditController controller;
    public _Default()
    {
         var service = ServiceLocator.Current.GetInstance<IBlogDataService>();
         this.controller = new PostEditController(this, service);
    }
}

的ServiceLocator有温莎城堡和StructureMap实现。不知道Ninject,但它是微不足道的一个新的国际奥委会创建的ServiceLocator适配器。

ServiceLocator has implementations for Castle Windsor and StructureMap. Not sure about Ninject, but it's trivial to create a ServiceLocator adapter for a new IoC.

这篇关于ASP.NET MVP注射服务的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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