7

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.

JAL
  • 41,701
  • 23
  • 172
  • 300
gfrizzle
  • 12,419
  • 19
  • 78
  • 104

6 Answers6

9

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
  • 77,456
  • 30
  • 160
  • 194
3

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

dmajkic
  • 3,448
  • 1
  • 18
  • 24
  • 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 Feb 17 '09 at 21:40
0

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
  • 66,855
  • 13
  • 106
  • 140
0

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
  • 7,192
  • 6
  • 26
  • 39
0

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?

Ether
  • 53,118
  • 13
  • 86
  • 159
Al Katawazi
  • 7,192
  • 6
  • 26
  • 39
  • 1
    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 Feb 04 '09 at 03:13
  • @liammclennan - Exactly, I don't know what Al is smoking, but maybe he should quit. – Jason Bunting Feb 17 '09 at 21:39
0

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

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166