3

我正在阅读Architecting Microsoft .Net Solutions for the Enterprise,并尝试弄清楚有关 Presenter 和服务层的一些事情。

首先,我的 Presenter 需要调用驻留在服务层中的方法,例如 initialize()、save() 等。但是我在哪里放置对服务层的引用?它应该在 Presenter 的类级别,还是应该在 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);
    }
4

1 回答 1

2
 IPredictionService predictionService = new PredictionService();

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

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

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

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

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

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

于 2011-03-27T19:18:58.473 回答