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

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

问题描述

我正在通过构建Microsoft企业版的.Net解决方案进行阅读,我尝试了几个关于Presenter和服务层的事情。



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



第二 - 这在本书中不是很清楚 - 这是从Presenter到服务层的处理如何工作的:

  public void ProcessPrediction()
{
//从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;

//准备发送到服务层
PredictionDTO prediction = new PredictionDTO();
prediction.RaceTitle = raceTitle;
//更多在这里填充DTO ....
// ...
// ...

IPredictionService predictionService = new PredictionService();
predictionService.ProcessPrediction(prediction);
}


解决方案

  IPredictionService predictionService = new PredictionService(); 

这将取决于很多因素:




  • 演示者的服务生命周期和演示者的生命周期

  • 如果您使用任何DI工具



  • 如果服务有空闲超时(例如,如果是WCF代理 ul>

    因此,实质上,它不一定是建筑设计 - 更多的是设计决策。



    DI工具,您可以:

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

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


    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.

    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:

    • Lifetime of the service and lifetime of the presenter
    • If you are using any DI tool
    • If the service needs to be disposed
    • If service has any idle timeout (for example if it is a WCF proxy)

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

    If you use a DI tool, you would either:

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

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

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

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