问题
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);
}
回答1:
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.
来源:https://stackoverflow.com/questions/5451590/mvp-presenter-and-the-service-layer-where-to-declare-service-layer