Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I structure a simple ASP.NET MVC app?

I've been reading a few things about ASP.NET MVC, SOLID and so on, and I am trying to figure out a simple "recipe" for small-to-medium ASP.NET MVC apps that would put these concepts together; the issue that I am most concerned with is ending up with controllers that are too complex and being like code-behind files in webforms, with all type of business logic into them.

I am considering the following architecture, for a small data-driven app:

  • Controllers: only handle requests, call an appropriate service and return the action result to the View;
  • Models: POCO, handle all the business logic, authorization etc. Depends on repositories, totally ignorant of persistence infrastructure.
  • Repositories: implement IRepository<T>, use dependency injection and is where my db code will reside; only receives and returns POCO.

I am considering having services between the controllers and the models, but if they will just pass forward method calls I am not sure how useful it would be.

Finally there should have unit tests covering the model code, and unit+integration tests covering the repository code (following the "red-green" practice, if possible)

Thoughts?

like image 463
rodbv Avatar asked Dec 09 '08 10:12

rodbv


4 Answers

Ian Cooper had a good post on exactly this recently:

The Fat Controller

like image 107
Zhaph - Ben Duguid Avatar answered Oct 17 '22 21:10

Zhaph - Ben Duguid


Simple recipe: (view)Presentation Layer using ASP.NET, (controller)Code Behinds or AJAX Services Layer, (model)Application Services layer, Business Model layer, and Persistance/Data Access layer.

Of course you can slice and dice numerous ways to deal with complexities in order to build a clearly readable and understandable application.

For a recent discourse on the subject, which I have found to be very good, check out this newly published book: Microsoft .NET: Architecting Applications for the Enterprise.

like image 23
daduffer Avatar answered Oct 17 '22 21:10

daduffer


These walkthroughs are quite helpful:

MVC Framework and Application Structure

Walkthrough: Creating a Basic MVC Project with Unit Tests in Visual Studio

Also see: aspnet-mvc-structuring-controllers

like image 42
Galwegian Avatar answered Oct 17 '22 21:10

Galwegian


Rob Conery has the best answer IMO.

Check out his MVC Storefront Application, which comes with complete source code and video tutorials.

like image 28
Pure.Krome Avatar answered Oct 17 '22 19:10

Pure.Krome