ASP.NET – Combining MVC and WebForms for Optimal Results

asp.netbusiness-logicmvcwebforms

Rather than asking a general question about WebForms vs MVC (such as in ASP.NET v/s ASP.NET MVC), I have a specific quesiton.

It appears the main differences between the two approaches are

  1. WebForms is Event Driven and uses pre built components
  2. MVC has a built in layer that WebForms does not: the Model
  3. MVC has the Controllers in a separate folder than the Views while the WebForms Controller is the CodeBehind

One could easily add a folder to a WebForm project called "Model" that stores all the business logic which used in the Code Behind (decoupling the two). The main argument against WebForms is that it's "easy" to write business logic in the CodeBehind. But you could easily add business logic in your MVC controller completely violating the separation of concerns.

Now for the question: Isn't it the case that you could write a WebForms project in such a way that the business logic is separate from the Controller/Code Behind which would have all the benefits of MVC (separation of concerns) while keeping the benefits of WebForms: rich controls, Event driven (if you like events)?

Best Answer

What you're describing is the MVP pattern. There is a framework called WebFormsMVP specifically designed to facilitate this.

I'm not sure many will agree that it's the best of both worlds, but it is a considerably more testable way to use WebForms than putting all your code in the codebehind class.

However, putting a service layer in between your forms and your data model also achieves this, much more simply.

Related Topic