Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put functions that help me execute controller tasks?

I'm currently working on an ASP.net MVC web site project.

I've put all database related stuff in my model, such as queries and update/delete/save functions.

I also created a couple of controllers that execute the logic. I added a Helpers namespace and inside that namespace there are a few classes that contain logic for pagination, internationalization etc.

I was wondering what is the best practice for placing functions and classes that do some general stuff, like generating an invoice?

like image 903
jao Avatar asked Sep 29 '09 08:09

jao


People also ask

CAN controller have private methods?

If they're private, they're not. So it's completely OK to have private methods in a controller class.

What are controller actions?

An action (or action method) is a method on a controller which handles requests. Controllers logically group similar actions together. This aggregation of actions allows common sets of rules, such as routing, caching, and authorization, to be applied collectively. Requests are mapped to actions through routing.


2 Answers

As I expressed in a comment above, I’m too very much interested in this question.

First, it seems wrong to create additional directories (for the other classes and utilities) directly in your ASP.NET MVC project. Also, I don’t feel that it should be in model. To me, model is more or less data classes which in some way represents the database (or the data we are trying to model). On top of that, often the business functionality (or the "real" pieces of code in your application) deals with several model classes at a time, and so there may not be a natural place for it in some model class.

So I think I am leaning towards the following schema:

  • Make controller actions very small; just few lines of code each.
  • Keep model simple and mostly functionless, and put it into a separate project.
  • Put all your code that does all the "real" work (the "business layer") into a separate project.

This way you will get a complete freedom in choosing your own namespaces, you will be able to create any number of utility classes, functions, and generally able to structure your code as you like without being restricted by ASP.NET MVC.

This is just an idea. At the moment I’m working on my first larger ASP.NET MVC application. So I’m actually going to learn whether and how this works in practice.

like image 118
Jan Zich Avatar answered Oct 27 '22 21:10

Jan Zich


I have Model classes that have Crud and Poco like you have.

Apart from that I have Viewmodels that are used for the typed Views.

My Viewmodels are pretty big and used in a couple of views (around 10-15 viewmodels for the whole appplication). In my application these ViewModels ended up as being the perfect place for the code that seamed to big and repetetive for the controller actions.

For example I have some logic that is pretty near to UI when I add a Product to the Cart. I now have a method in the ViewModel: AddToCart(IProductService productService, ICartService cartService).

like image 30
Mathias F Avatar answered Oct 27 '22 21:10

Mathias F