2011-03-27 96 views
3

我正在閱讀構建Microsoft .Net解決方案的企業,我嘗試瞭解關於演示者和服務層的一些事情。MVP - 演示者和服務層 - 在哪裏聲明服務層

首先,我的演示需要調用駐留在服務層的方法,如初始化(),保存()等,但我在哪裏放置到服務層中的參考?它應該是在演示一流水平,或者我應該定義演示方法本身就是一個新的服務?

二 - 這是不是在這本書真正明確要麼 - 這是從主持人到服務層的處理是如何工作的?:

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

回答

2
IPredictionService predictionService = new PredictionService(); 

這真的取決於很多因素:

  • 主持人
  • 的服務和壽命的壽命如果你正在使用任何工具DI
  • 如果服務需要被設置
  • 如果服務具有任何空閒超時(例如,如果它是一個WCF代理

因此,在本質上,它不一定是一個建築設計 - 它是更多的設計決定。

如果使用DI工具,你要麼:

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

甚至更​​好,上面這些都不是,只是把它聲明爲財產和DI的工具,當它創建演示者可以填充它。該presenter_的服務和終身的

+0

_LifeTime - 直至現在只是被用來通過主持人的一個方法一次。 _如果您使用任何DI工具_ - 不,我沒有使用任何工具。 _如果服務需要處理_ - 就像在使用?不,不是必需的。 _如果服務有任何空閒超時_ - 否,它沒有。 最後一個選項聽起來不錯。但這意味着在視圖中使用DI工具來實例化演示者和服務,對吧? – 2011-03-27 19:30:13

+0

如果不需要部署和無空閒超時,我要再次聲明它爲成員,並在構造函數初始化它。或者只是使用像溫莎城堡或Unity這樣的東西來照顧對象創作。無論如何,將它作爲成員變量更有意義 – Aliostad 2011-03-27 19:40:16