简体   繁体   English

ASP.NET MVP注入服务依赖性

[英]ASP.NET MVP Injecting Service Dependency

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. 我有一个ASP.NET页面,它实现了我的视图,并在页面构造器中创建了演示者。 Phil Haack's post providing was used as the starting point , and I'll just the examples from the post to illustrate the question. Phil Haack的帖子提供被用作起点 ,我只是从帖子中的例子来说明问题。

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

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

What is the best approach to inject the instance of the BlogDataService? 注入BlogDataService实例的最佳方法是什么? The examples I have found use properties in the Page class for the dependency marked with an attribute which the injection framework resolves. 我发现的示例在Page类中使用了属性,该属性标记有注入框架解析的属性。

However, I prefer using the constructor approach for testing purposes. 但是,我更喜欢使用构造函数方法进行测试。

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. 我更喜欢Ninject,但只要它流利,StructureMap或Windsor会很好。

Thanks for any feedback. 感谢您的任何反馈。

In our homegrown MVP-framework we had a typed base-class that all Pages inherited from. 在我们自己开发的MVP框架中,我们有一个所有Pages继承自的类型化基类。 The type needed to be of type Presenter (our base presenter class) 类型必须是Presenter类型(我们的基本演示者类)

In the base-class we then initialized the controller using the IoC-container. 在基类中,我们然后使用IoC容器初始化控制器。

Sample code: 示例代码:

public class EditPage : BasePage<EditController> {
}

public class EditController : Presenter {
 public EditController(IService service) { }
}

public class BasePage<T> : Page where T: Presenter
{
 T Presenter { get; set; }
 public BasePage() { 
  Presenter = ObjectFactory.GetInstance<T>(); //StructureMap
 }
}

Hope this helps! 希望这可以帮助!

If you use the Microsoft ServiceLocator , you can apply the service locator design pattern and ask the container for the service. 如果使用Microsoft ServiceLocator ,则可以应用服务定位器设计模式并向容器请求该服务。

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 has implementations for Castle Windsor and StructureMap. ServiceLocator具有Castle Windsor和StructureMap的实现。 Not sure about Ninject, but it's trivial to create a ServiceLocator adapter for a new IoC. 不确定Ninject,但为新的IoC创建ServiceLocator适配器是微不足道的。

I haven't seen a general purpose method for doing constructor injection on webforms. 我还没有看到在webforms上进行构造函数注入的通用方法。 I assume it may be possible via a PageFactory implementation, but since most on the edge right now are moving to MVC rather than webforms, that may not happen. 我假设它可能通过PageFactory实现,但由于现在大多数人正在转向MVC而不是webforms,这可能不会发生。

However, autofac (a DI container I like a lot) has an integration module for ASP.NET WebForms that does property injection without attributes - your code would look like this : 但是, autofac (我喜欢的DI容器)有一个用于ASP.NET WebForms集成模块,它可以在没有属性的情况下进行属性注入 - 您的代码看起来像这样:

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

    public IBlogDataService DataService{get;set;}
    public _Default()
    {
    }
}

I know this doesn't specifically solve your desire to use constructor injection, but this is the closest I know of. 我知道这并没有特别解决你使用构造函数注入的愿望,但这是我所知道的最接近的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM