ASP.NET MVP Injecting Service Dependency

后端 未结 3 510
青春惊慌失措
青春惊慌失措 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

    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 {
    }
    
    public class EditController : Presenter {
     public EditController(IService service) { }
    }
    
    public class BasePage : Page where T: Presenter
    {
     T Presenter { get; set; }
     public BasePage() { 
      Presenter = ObjectFactory.GetInstance(); //StructureMap
     }
    }
    

    Hope this helps!

提交回复
热议问题