patterncsharpMinor
MVP presenter code in C# - possible troubles with DTO's
Viewed 0 times
presenterwithtroublesmvppossiblecodedto
Problem
I am making a website using the MVP pattern. It is a simple website, so for the repository I use Linq2SQL. My architecture is basically:
(View - Presenter) - Service Layer - Domain Models.
The View consists of 10 dropdownlists, and a submit button.
Between the (View - Presenter) and the Service Layer I use DTO's to exchange information. But here is as well where my problem lies; the Service Layer gets the data from the DataContext. This data is in all sorts of types, ints, strings, etc. My View mostly needs everything in strings. Now I need to fill a DTO to send to the Presenter. The question is if I should already format everything so that it can be consumed by the presenter/view, or should the Presenter do this? For now, I have the Presenter do it, but I find the code a bit ghastly. Please review my code (p.s I am aware I should use DI - it will be applied ;-)):
```
using Business;
using System.Collections.Generic;
using Service;
using DTOs;
using System.Text;
namespace Web
{
public class PredictionDetailsPresenter
{
private readonly IDriverService driverService;
private readonly IPredictionDetailsView predictionDetailsView;
private readonly IPredictionService predictionService;
#region constructors
public PredictionDetailsPresenter(IPredictionDetailsView predictionDetailsView)
: this(predictionDetailsView, new DriverService(), new PredictionService())
{
}
public PredictionDetailsPresenter(IPredictionDetailsView predictionDetailsView, IDriverService driverService,
IPredictionService predictionService)
{
this.predictionDetailsView = predictionDetailsView;
this.driverService = driverService;
this.predictionService = predictionService;
}
#endregion
#region public methods
public void Initialize()
{
predictionDetailsView.SetFirstListItemOfPositionDropDownLists(" ---Select
(View - Presenter) - Service Layer - Domain Models.
The View consists of 10 dropdownlists, and a submit button.
Between the (View - Presenter) and the Service Layer I use DTO's to exchange information. But here is as well where my problem lies; the Service Layer gets the data from the DataContext. This data is in all sorts of types, ints, strings, etc. My View mostly needs everything in strings. Now I need to fill a DTO to send to the Presenter. The question is if I should already format everything so that it can be consumed by the presenter/view, or should the Presenter do this? For now, I have the Presenter do it, but I find the code a bit ghastly. Please review my code (p.s I am aware I should use DI - it will be applied ;-)):
```
using Business;
using System.Collections.Generic;
using Service;
using DTOs;
using System.Text;
namespace Web
{
public class PredictionDetailsPresenter
{
private readonly IDriverService driverService;
private readonly IPredictionDetailsView predictionDetailsView;
private readonly IPredictionService predictionService;
#region constructors
public PredictionDetailsPresenter(IPredictionDetailsView predictionDetailsView)
: this(predictionDetailsView, new DriverService(), new PredictionService())
{
}
public PredictionDetailsPresenter(IPredictionDetailsView predictionDetailsView, IDriverService driverService,
IPredictionService predictionService)
{
this.predictionDetailsView = predictionDetailsView;
this.driverService = driverService;
this.predictionService = predictionService;
}
#endregion
#region public methods
public void Initialize()
{
predictionDetailsView.SetFirstListItemOfPositionDropDownLists(" ---Select
Solution
Best practice is to let the presenter handle the formatting. If you are, for example, dealing with multiple views, the code will be much easier to maintain and also be parsed/formatted from just the one location.
By the look of your code, i'd suggest you use a simple POCO-class for the data binding that occurrs between the Presenter and the View. Name and position would be sufficient properties.
Or you could just use a
Best regards,
Mattias
By the look of your code, i'd suggest you use a simple POCO-class for the data binding that occurrs between the Presenter and the View. Name and position would be sufficient properties.
public class DriverDTO
{
public int Position {get;set;}
public string Name {get;set;}
}
public interface IPredictionDetailsView
{
public IList SelectedDrivers {get; set;}
}Or you could just use a
Dictionary to store the values in. That way you will always have unique positions, if that is a requirement.Best regards,
Mattias
Code Snippets
public class DriverDTO
{
public int Position {get;set;}
public string Name {get;set;}
}
public interface IPredictionDetailsView
{
public IList<DriverDTO> SelectedDrivers {get; set;}
}Context
StackExchange Code Review Q#2002, answer score: 2
Revisions (0)
No revisions yet.