MVP - 展示器和服务层 - 在哪里声明服务层 [英] MVP - Presenter and the Service Layer - Where to declare Service Layer

查看:25
本文介绍了MVP - 展示器和服务层 - 在哪里声明服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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);
    }

推荐答案

 IPredictionService predictionService = new PredictionService();

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

This will really depend on a lot of factors:

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

所以本质上,它不一定是架构设计 - 它更多的是设计决策.

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

如果您使用 DI 工具,您将:

If you use a DI tool, you would either:

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

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

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

这篇关于MVP - 展示器和服务层 - 在哪里声明服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆