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

大憨熊 提交于 2019-11-29 08:26:19

问题


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?


回答1:


Ian Cooper had a good post on exactly this recently:

The Fat Controller




回答2:


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.




回答3:


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




回答4:


Rob Conery has the best answer IMO.

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



来源:https://stackoverflow.com/questions/352295/how-should-i-structure-a-simple-asp-net-mvc-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!