MVP - 演示者和服务层 - 在哪里声明服务层

时间:2011-03-27 19:04:21

标签: asp.net architecture c#-4.0 mvp service-layer

我正在阅读为企业构建Microsoft .Net解决方案,我试图弄清楚有关Presenter和服务层的一些事情。

首先,我的Presenter需要调用驻留在服务层中的方法,比如initialize(),save()等。但是我在哪里放置对服务层的引用?它应该是Presenter中的类级别,还是应该在演示者方法本身中定义新服务?

第二 - 这在书中也不是很清楚 - 这是从Presenter到服务层的处理方式吗?:

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 个答案:

答案 0 :(得分:2)

 IPredictionService predictionService = new PredictionService();

这实际上取决于很多因素:

  • 服务的生命周期和演示者的生命周期
  • 如果您使用的是任何DI工具
  • 如果需要处理服务
  • 如果服务有任何空闲超时(例如,如果它是 WCF代理

所以从本质上讲,它不一定是建筑设计 - 更多的是设计决策。

如果您使用DI工具,您可以:

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

甚至更好,以上都不是,只是将其声明为属性,DI工具可以在创建演示者时填充它。

相关问题