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?
If they're private, they're not. So it's completely OK to have private methods in a controller class.
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.
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:
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With