我正在通读为企业构建 Microsoft .Net 解决方案我试图弄清楚有关演示者和服务层的一些事情。
首先,我的 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);
}
IPredictionService predictionService = new PredictionService();
这实际上取决于很多因素:
- 服务的生命周期和演示者的生命周期
- 如果您使用任何 DI 工具
- 如果服务需要处置
- 如果服务有任何空闲超时(例如,如果它是WCF代理)
所以本质上,它不一定是一个架构设计——它更多的是一个设计决策。
如果您使用 DI 工具,您可以:
IPredictionService predictionService = diContainer.Resolve<IPredictionService>();
或者甚至更好,以上都不是,只需将其声明为属性,DI 工具就可以在创建演示者时填充它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)