14

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?

1
  • If I could, I would give this question +100 votes. I really want to know what how other people are addressing this issue. I just started experimenting with ASP.NET MVC (on a middle sized project), and I find myself often cornered by the directory structure and some of the MVC concepts.
    – Jan Zich
    Sep 30, 2009 at 14:27

6 Answers 6

5

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.

0
1

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).

0

You might consider creating some services that you inject into your controllers.

It's almost too wide a question.

3
  • What exactly do you mean? Do you have links to examples?
    – jao
    Sep 29, 2009 at 9:55
  • @jao are you using Dependency Injection? there are lots of examples out there. look at s#arp architecture for one
    – dove
    Sep 29, 2009 at 10:59
  • So for example, I have an entity called Reservation, is it common practice to add a GenerateInvoice to it? So that I can call Reservation.GenerateInvoice()? Or am I totally lost here?
    – jao
    Sep 29, 2009 at 14:09
0

This kind of business logic should be somewhere in your Model.

However, I find that when there is just something that doesn't really "fit" anywhere - and you might be tempted to create a Utilities class - this is usually a good place to utilize Extension methods.

Perhaps you can add extension methods on your dataset to help you with pagination?

0

I you really need the best practice, consider looking at Domain Driven Design. It doesn't suit all projects and does require good OOP skills, but I think it is without doubts a "best practice"... as long as you can afford it ;-)

Notice that you do already violate DDD since you use Active Record pattern (put persistence logic into entities). So, I do not say that you have to follow DDD. But it will be useful to grok anyway.

0

I think the best solution to this question about practice is: Put the logic in the Model if it's going to be used across controllers. If it's controller specific, just drop it in your controller. When I say the Model, this could be a separate project that conatins your Entity Data Model, or it could be a View Model, or it could be just the Models folder of your MVC project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.