C# – Where to Locate Helper Classes/Methods for Transforming Business Data for the View

Architectureasp.net-mvc-4centity-frameworklayers

I have inherited a badly architected and organised ASP.NET MVC application, which is an online booking system for healthcare providers. It seems to have been designed with very few object oriented principles used at all, and little abstraction or SoC. So, I am rewriting a lot of the backend code.

I have implemented Entity Framework 6 to replace the current direct database calls that populate DataTables that get returned by static method calls. I have created a Business Logic Layer (BLL) and a Data Access Layer (DAL) (in folders/namespaces in the same project, not as class libraries). I keep the database entity classes in the Models folder.

I am rewriting my Controllers as "thin" controllers that take application data, transform it if necessary and create BLL objects which perform all of the actions needed. The controller then manipulates the returned data into views etc.

Where should helper classes/methods be that manipulate the BLL object data for views?

For example:
I have a BookableTreatments BLL class that contains all bookable treatments. The view requires an HTML Select element with an option for each treatment that a specific provider offers. So I might create a helper class SelectListGenerator that has a method that takes a BookableTreatments object and a ProviderID and returns an IList that can be used to generate the HTML select.

Where should my SelectListGenerator class be located? It isn't a controller, it isn't business logic as it is application specific. Should I create a new namespace and folder called ALL (abbreviation for Application Layer Logic, not the word "all") that contains just classes for this sort of purpose? I don't expect to have huge numbers of classes like this.

(Please note, I do have a BookableProviders class that contains provider information, and I could have this class contain a list of treatments that the provider offers. So, I understand it may be better to do this and not have a list of treatments I need to filter for a specific provider, but that's an unrelated issue. And even if that is the case, I would still need to generate a select list for the view. So, the question still stands in its generic form)

Thank you!

Best Answer

There is no "proper" place for helper classes in the ASP.NET MVC framework. I've created a folder named "Helpers" in projects that use this framework, and that seems to work fine. There is no right way or best practice, because the MVC design pattern doesn't provide guidance, and Microsoft (the vendor for the framework) doesn't have any recommendations either.

This question illuminates my biggest criticisms of MVC web frameworks that have folders named "models", "views" and "controllers". Microsoft's ASP.NET MVC framework is not the only offender. Arguably the framework committing this sin originally was the Ruby on Rails MVC framework for Ruby.

Folders named models, views and controllers make people think every file and class must be categorized as a model, view or controller. Don't confuse the Model-View-Controller Design Pattern with application architecture.

The MVC pattern is a user interface design pattern. It does not describe each layer or area of your application architecture. How you organize business logic, authorization, authentication, data access and even user input validation is not covered by the MVC pattern. Beyond models, views and controllers, code organization is up to you and your team. Do what makes sense.