Should controllers in an ASP.NET MVC web app call repositories, services, or both?

后端 未结 7 1424
生来不讨喜
生来不讨喜 2021-01-30 09:22

The controllers in my ASP.NET MVC web app are starting to get a bit bloated with business logic. The examples on the web all show simple controller actions that simply pull dat

7条回答
  •  一向
    一向 (楼主)
    2021-01-30 10:10

    Well, its really up to you, i like to keep the controllers as simple as possible, and to archive this i need to encapsulate the bussines logic in a separate layer, and here is the big thing, mainly you have 2 options, assuming you are using Linq2SQL or Entity Framework:

    • You can use the extender methods and partial class to validate your Models just before Saving the Changes (hooks method, you can see an example of this in Nerdinner sample by Scott Gu).

    • The other way (and my favorite, because i feel more control over the app flow), is to use a totally separate layer for bussines logic like Services Layer (you can see this aprroach in the series of tutorials by Stephen Walther in asp.net/mvc zone).

    With this two methods you got DRY and clean up your otherwise messy controllers.

提交回复
热议问题