mvpwebformsmvp

In the MVP pattern, is the presenter responsible for formatting numbers to percent?


So the presenter calls the model to get some data.

That data is returned as a DTO with no formatting.

Let's say the data is simply a decimal .12

Does the presenter then format the number to a percent?

view.Amount = Math.Round(dto.Amount * 100, 2) + "%";

Or is it the views responsibility?

public decimal Amount 
{
    set 
    {
        txtAmount.Text = Math.Round(dto.Amount * 100, 2) + "%";
    }
}

Solution

  • I do not use C# and WebFormsMvp, but I think your question is pretty much related to common MVP architecture. Let me explain it out of WebFormsMvp scope. The only responsiblity of a P should be mediating between a M and a V (in terms of how to "pass" the M data to the V and vice versa), and the V is responsible for how the data is represented to the user.

    Consider the following scenario: you decide to drop the txtAmount field (let's consider it's just a text label or input, etc) in favor of a certain kind of progress bars or simple charts. If you passed "normalized" number values before (as they come from your M in your case) -- you still can replace your amount view as you wish, as they are supposed to accept a raw number value.

    public interface IView {
        decimal Amount { set; }
    }
    

    Compare the above to:

    public interface IView {
        // How to apply a string to a chart as a data item?
        string Amount { set; }
    }
    

    For the latter, you'd be face to face with "string to decimal" problem for the Amount property requiring to rework your string IView.Amount back to decimal IView.Amount (or even worse: parsing the percentaged value dropping the % sign and so on right in an IView instance to make it a number again even not knowing the original number type: was it a decimal or an integer?)

    You might also consider percents as a V for humans: 42% in a percentage view or 420‰ in a promille view is still 0.42 in the model.