MVP - Presenter and the Service Layer - Where to declare Service Layer

旧城冷巷雨未停 提交于 2019-12-12 09:55:09

问题


I'm reading through Architecting Microsoft .Net Solutions for the Enterprise and I try to figure a couple of things out concerning the Presenter and the Service Layer.

First off, my Presenter needs to call methods that reside in the Service Layer, like initialize(), save() etc. But where do I place a reference to the service layer? Should it be at class level in the Presenter, or should I define a new service in the presenter methods itself?

Second - this isn't really clear in the book either - is this how the processing from the Presenter to the Service Layer works?:

public void ProcessPrediction()
    {
        //Get the data from the View
        string selectedForPolePosition = predictionPageView.DriverPolePosition;
        string selectedForSecondPosition = predictionPageView.DriverSecondPosition;
        string selectedForThirdPosition = predictionPageView.DriverThirdPosition;
        string selectedForFourthPosition = predictionPageView.DriverFourthPosition;
        string selectedForFifthPosition = predictionPageView.DriverFifthPosition;
        string raceTitle = predictionPageView.RaceTitle;

        //Prepare for sending to the Service Layer
        PredictionDTO prediction = new PredictionDTO();
        prediction.RaceTitle = raceTitle;
        //More Filling of the DTO here....
        //...
        //...

        IPredictionService predictionService = new PredictionService();
        predictionService.ProcessPrediction(prediction);
    }

回答1:


 IPredictionService predictionService = new PredictionService();

This will really depend on a lot of factors:

  • Lifetime of the service and lifetime of the presenter
  • If you are using any DI tool
  • If the service needs to be disposed
  • If service has any idle timeout (for example if it is a WCF proxy)

So in essence, it is not necessarily an architectural design - it is more of design decision.

If you use a DI tool, you would either:

 IPredictionService predictionService = diContainer.Resolve<IPredictionService>();

Or even better, none of above and just declare it as property and DI tool can populate it when it creates the presenter.



来源:https://stackoverflow.com/questions/5451590/mvp-presenter-and-the-service-layer-where-to-declare-service-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!