Question

I am working on a system that generates emails from a template. Templates have been supplied by the business, and they contain a line of code similar to this one:

Thank you for your payment of @((decimal.Parse(Model.PaymentAmount)/100).ToString("C"))

In this case the PaymentAmount is expected as a string representation of an integer (eg the required output £5.54 would be provided as "554").

Now, I am of the opinion that there should be no calculations in the presentation layer - in this case there is the /100 calculation, and a parse. I have suggested a number of alternatives for this, where a transformation is carried out in code before the template to give an altered view such as:

Thank you for your payment of @Model.PaymentAmount.ToString("C")

We have a set of reasonably competent, technical architects on the project but I am having trouble convincing them of a good reason for this.

Can anyone help me with an argument to present to the architects that would work in getting the latter fragment of code implemented over the former - or if you disagree with this approach, why?

Was it helpful?

Solution

One reason to prefer solution #2 over the first one is that you will get in trouble if - for some reason - somebody hands a text to the template that cannot be parsed into a decimal. There are a number of possiblities:

  1. Non-numeric text.
  2. Number formatted for a different culture.

So type-safety is much better in approach #2. From an architectural point of view the one who fills a template does not need to know the implementation details whereas in approach #1 he or she needs to know that it is required to provide a string that is formatted in a specific way.

OTHER TIPS

For me the second approach is better, and I recommend it,coz in software engineering patterns the main thing you will learn is try to make things isolated and dedication, so calculations handling should be separate, coz if the project size is large than segmented and well isolated things are easy to maintain, however if the size is small and or personal use then this comes to just practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top