10

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

I need to create an utility class where I put functions to convert string into dates(years, months, days, etc...).

In ASP.NET Web Forms I used to place this kind of classes inside the App_Code folder. In MVC there is not such folder and I don't think utility classes belong neither to Models nor to Helpers(a folder I created to put my extensions on HTML Helpers).

I read that is a good practice to place the utility classes in a different assembly. I guess a different project should do the job but what kind of project shall I create? A plain Class Library project seem the most logical choice to me.

However in my case I just need to put one single class with several methods so, if we ignore re-usability, isn't it more logic to put the utility class somewhere in my MVC3 Web application?

CiccioMiami
  • 8,028
  • 32
  • 90
  • 151

4 Answers4

14

You should not have utility classes. Convert them to extension methods which is somewhat better. View models are even better.

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

A "Common" folder is like the trashcan imho. You put all the junk in it.

Update

I would place it in an extension method for DateTime (placed in a class called DateTimeExtensions which is placed in a namespace called Infrastructure).

And I would use it internally in the view model or when generating the view model (in the controller).

As for which project, it doesn't really matter. What's important is that you got small classes with specific tasks (or responsibilities).

Some argue that you should have several class libraries with distinct responsibilities. I don't do that. I create one UI project and one project for the business logic. However, I make sure that the classes implement one or more interfaces to be able to refactor the application later on. KISS should also apply to project structure and not just the code in them.

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

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks! I use extension methods for my HTML Helpers and also ViewModels in order not to use the Viewbag at all, since I perform unit testing. Since you suggest not to use utility classes (I read many debates on the web) where would you place a class that expose methods to convert string into dates, for instance by extracting the year? – CiccioMiami Feb 10 '12 at 15:41
  • 1
    gr8! You have come far then. I would place it in an extension method for `DateTime` (placed in a class called `DateTimeExtensions` which is placed in a namespace called `Infrastructure`). And I would use it internally in the view model or when generating the view model (in the controller). – jgauffin Feb 10 '12 at 15:51
  • Great organization! However I forgot to add that I use these classes in my DataRepository (or DAL), since the person who created the database that provides data for the application, stored all the dates as string. Therefore if I have a method "GetAllProductsCreatedThisYear" I need the function that convert the string into date in my LINQ inline function. Does your organization apply in this case as well? – CiccioMiami Feb 10 '12 at 16:00
3

Call it Common put everything in there.

Then use namespaces like these examples

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

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91
  • 1
    Thanks! I thought also about that but since we are more people working on the same project, I would like to have a strong and clear naming structure, in order to have a clear separation of concerns between folders, beside namespaces – CiccioMiami Feb 10 '12 at 15:36
1

Why don't you create another folder named Utility, Infrastructure or similar in your project (next to the Helpers folder for example) and put the utility class(es) there.

You can always move it to a separate DLL (class library project) once you have a need to reuse it.

M4N
  • 94,805
  • 45
  • 217
  • 260
  • 1
    Thanks, it was also my first thought but since I think many developers found themselves in my same situation I wanted to have more opinions about any standard or best practice that applies to this – CiccioMiami Feb 10 '12 at 15:48
0

I prefer creating a folder and namespace called Helpers as suggested here: Where can I put custom classes in ASP.NET MVC?

Community
  • 1
  • 1
Chris Goodman
  • 161
  • 2
  • 12