views:

149

answers:

6

In the MVC folder structure, where should general class files reside? For example, I have a class that determines the right DataContext to use, so I'm not reinventing the wheel in each of my controllers. Should it live in the Controllers folder even though it's not a controller? Should it be with the Models because it's database related, even though it's not a model? Possibly the Views\Shared folder? Or is Content the catch-all folder for that kind of stuff? I'm sure I could put it anywhere, but I was wondering where the "right" place is.

A: 

If it could be useful by itself (think of a command line tool built around it), put it in the Models folder. If it is used only as a helper for controllers, put it in Controllers folder.

mouviciel
+2  A: 

If you look at Rob's MVC Storefront: Separate class library project (like Commerce.MVC.Data)

dmajkic
It isn't about what Rob does, it is more than that - it is simply a standard approach at separation of responsibilities and such. Data access has no business being in the presentation layer of the application unless we are talking about a one-page application where it wouldn't make as much sense.
Jason Bunting
A: 

It really depends on what it does, if it accesses data it should be in the Data Access Layer, otherwise you can put it in the controller folder.

Al Katawazi
A: 

dmajkic,

Why separate it out into its own area? If its BLL code it should be in the controller folder, if its DAL related item it should be in the Model. I can understand if a project gets huge and you want to create some subfolders, that shouldn't be an issue. But placing code in another tier really defeats the purpose of MVC don't you think?

Al Katawazi
No. MVC is a view pattern. It relates to the presentation layer. Controllers should contain presentation logic only. There should not be an business logic in controller classes. dmajkic said to place the class in a seperate project, not a seperate tier.
liammclennan
@liammclennan - Exactly, I don't know what Al is smoking, but maybe he should quit.
Jason Bunting
+3  A: 

It's not a controller, content or a view, so don't use those. It sounds most closely related to your model, so you could put it in model under a subfolder called "Helpers" or "Utility" or some such. Or you can add another top level folder called Services and put it there. That's where I put all my app logic, the middle man between the controllers and the model.

John Sheehan
You put all of your application logic in the presentation layer project? Sounds horrible....
Jason Bunting
For small projects I put it there. If the services will be reused, they get their own project.
John Sheehan
A: 

Have a separate DataAccess assembly, make that class internal and call it DataContextFactory.

Andrei Rinea