ASP.NET MVP 注入服务依赖

发布于 2024-08-10 23:06:45 字数 691 浏览 3 评论 0原文

我有一个 ASP.NET 页面,它实现我的视图并在页面构造函数中创建演示者。 Phil Haack 的帖子提供作为起点,并且我将仅举帖子中的示例来说明问题。

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

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

注入 BlogDataService 实例的最佳方法是什么?我发现的示例使用 Page 类中的属性来标记带有注入框架解析的属性的依赖项。

但是,我更喜欢使用构造函数方法进行测试。

有没有人有意见或可能链接到上述的良好实现。我更喜欢 Ninject,但 StructureMap 或 Windsor 只要流畅就可以。

感谢您的任何反馈。

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());
    }
}

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.

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.

Thanks for any feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新一帅帅 2024-08-17 23:06:45

在我们自己开发的 MVP 框架中,我们有一个所有页面都继承自的类型化基类。类型需要是 Presenter 类型(我们的基本 Presenter 类),

然后在基类中我们使用 IoC 容器初始化控制器。

示例代码:

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
 }
}

希望这有帮助!

In our homegrown MVP-framework we had a typed base-class that all Pages inherited from. The type needed to be of type Presenter (our base presenter class)

In the base-class we then initialized the controller using the IoC-container.

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!

天涯离梦残月幽梦 2024-08-17 23:06:45

如果您使用 Microsoft ServiceLocator,则可以应用 服务定位器设计模式 并向容器请求服务。

在您的情况下,它看起来像这样:

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

源来凯始玺欢你 2024-08-17 23:06:45

我还没有看到在网络表单上进行构造函数注入的通用方法。我认为通过 PageFactory 实现可能是可能的,但由于现在大多数边缘人正在转向 MVC 而不是 Webform,所以这可能不会发生。

但是,autofac(我非常喜欢的 DI 容器)有一个 ASP.NET WebForms 集成模块,无需属性即可进行属性注入 - 您的代码将如下所示:

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

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

我知道这并不能具体解决您使用构造函数注入的愿望,但这是我所知道的最接近的。

I haven't seen a general purpose method for doing constructor injection on 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.

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 :

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文