English 中文(简体)
ASP.NET MVP Injecting Service Dependency
原标题:

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.

最佳回答

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.

问题回答

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!

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签