Where should I put my utility classes in an ASP.NET MVC3 application?

I am developing a web application in ASP.NET MVC3 with C # and Razor.

I need to create a class where I put functions to convert strings to dates (years, months, days, etc.).

In ASP.NET Web Forms, I used these classes in the App_Code folder. There is no such folder in MVC, and I don’t think that utility classes belong to either the Model or the Helpers (the folder that I created to host my extensions in the HTML helpers).

I read that it’s good practice to put utility classes in another assembly . I suppose another project should do the job, but which project should I create? A simple class library project seems to be the most logical choice for me.

However, in my case, I just need to put one class with several methods, so if we ignore reuse, is it not logical to put the utility class somewhere in my MVC3 web application?

+8
class-library asp.net-mvc-3 code-reuse utility-method
source share
4 answers

You should not have utility classes. Convert them to extension methods that are slightly better. Browse models even better.

I usually create a folder called "HtmlHelpers" or "Infrastructure" for plumbing.

The "normal" folder is similar to trashcan imho. You put all the garbage in it.

Update

I would put it in an extension method for DateTime (placed in a class called DateTimeExtensions that fits in a namespace called Infrastructure).

And I would use it inside the view model or when creating the view model (in the controller).

As for the project, it does not really matter. The important thing is that you get small classes with specific tasks (or responsibilities).

Some argue that you should have several class libraries with different responsibilities. I do not do this. I am creating one user interface project and one project for business logic. However, I certify that classes implement one or more interfaces in order to be able to reorganize the application later. KISS should also be applied to the project structure, not just the code in them.

In other words: I would put my assistants in Core (a business logic project) in a namespace called Infrastructure.

+11
source share

Name him. Put everything there.

Then use namespaces like these examples

Common.Formatters Common.Functions Common.Foo Common.Bar

+1
source share

Why don't you create another folder with the name "Utility", "Infrastructure" or the like in your project (for example, next to the "Helpers" folder) and place the class (s) there.

You can always transfer it to a separate DLL (class library project) when you need to reuse it.

+1
source share

I prefer to create a folder and namespace called Helpers, as suggested here: Where can I place custom classes in ASP.NET MVC?

0
source share

All Articles