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

3
我正在阅读《为企业架构Microsoft .Net解决方案》,并尝试理解关于Presenter和Service Layer的几个问题。
首先,我的Presenter需要调用位于Service Layer中的方法,例如initialize()、save()等。但是我应该在哪里放置对Service Layer的引用?它应该在Presenter类级别上,还是应该在Presenter方法本身中定义一个新的service?
其次 - 这在书中并不是很清楚 - Presenter到Service Layer的处理是否是这样工作的?
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个回答

2
 IPredictionService predictionService = new PredictionService();

这将取决于很多因素:
  • 服务寿命和演示者寿命
  • 是否使用任何 DI 工具
  • 如果服务需要被处理
  • 如果服务有任何空闲超时(例如,如果它是一个 WCF 代理)
因此,本质上,这不一定是一个架构设计 - 它更多的是一个设计决策。
如果您使用 DI 工具,您可以选择以下操作之一:
 IPredictionService predictionService = diContainer.Resolve<IPredictionService>();

甚至更好的方法是,不使用上述任何一种方式,而是将其声明为属性,当DI工具创建Presenter时,它会自动填充。

服务的生命周期和Presenter的生命周期 - 目前只被一个Presenter方法使用。如果您正在使用任何DI工具 - 不,我没有使用任何工具。如果服务需要被处理 - 像Using一样吗?不,不必要。如果服务有任何空闲超时 - 不,它没有。最后一个选项听起来不错。但这意味着DI工具在视图中用于实例化Presenter和服务,对吗? - Garth Marenghi
如果不需要处理和空闲超时,我会将其声明为成员变量,并在构造函数中初始化。或者只需使用类似Windsor Castle或Unity的工具来管理对象的创建。无论哪种情况,将其作为成员变量更有意义。 - Aliostad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接