ASP.NET MVP Injecting Service Dependency

后端 未结 3 503
青春惊慌失措
青春惊慌失措 2021-01-03 08:29

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

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 09:01

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

提交回复
热议问题