Is this business logic or presentation logic?

This code exists in the view:

if (Model.Group.IsPremium && null != Model.Group.ContactInfo) { Html.RenderPartial("ContactInfo", Model.Group.ContactInfo); } 

at first glance, this is the logic of presentation, and so everything is fine. But this is not very good with me.

The fact is that this is a business requirement for displaying contact information if the group is classified as premium, which means they paid.

What do you guys think? Should this logic be moved to HtmlHelper or abstracted by other means? Or is this the intended use of View? What is the best thing to do with this code?

+4
source share
2 answers

I would create a ViewModel that encapsulates this logic as a boolean property of DisplayContactInfo. It depends on how β€œclean” you want to see.

+9
source

I would definitely move this to ViewHelper. This is due to the fact that as soon as you start writing presentation logic in representations - aspx files, you start creating "soup soup", which reduces the comprehensibility of the code and, therefore, increases the cost of maintenance.

Another advantage of using ViewHelpers to encapsulate your view logic is that it also makes your application more flexible for unit testing. Therefore, given your code above, I would use it in ViewHelper, for example,

 using System.Linq; using System.Web.Mvc; using System; using System.Text; using System.Web.Mvc.Html; //Need this for Html helper extension method public static class GroupViewHelper { public static void ShowContactInfo(this HtmlHelper helper, ModelType model) { if (model.Group.IsPremium && null != model.Group.ContactInfo) { //Do your rendering here. } } // ... your other ViewHelper methods here. } 

Subsequently, somewhere in your view, I would call this helper method this way,

 <% Html.ShowContactInfo(Model); %> 

This method leads to the fact that looks that avoid the "soup tag" are more convenient to maintain and are immensely verified by the unit.

+1
source

All Articles