3 Ways ASP.NET MVC Can Simplify Your Development

Oct 1, 2019 | ASP.NET

Hey guys, welcome back Craig from craig on code here. Today we’re going to talk about three ways asp.net MVC can simplify your development.

#1 ASP.NET MVC Separates Concerns

In this example here we have an asp.net piece of code for a controller. And this controller worries about getting the data and displaying have you.

And this logic here doesn’t indicate what view I’m going to display or how I’m going to get the data, but it knows how to do that. So there is this separation of concern here. This repository pattern knows how to go get my data and if I wanted to change this to go pull from a database, I could easily do that. This view here easily allows me to pull in in display data. In fact, there’s an override for views to display a specific view if I wanted to.

So now I can easily display specific views of data by making logical decisions. Here in the controller in web forms, you have something called a dot ASP x page. And this page is tightly coupled to the actual HTML. So if I go to the HTML of this, you can see that this page is tightly coupled to a master page in is tightly coupled to code behind and to a class behind there.

If we go to the code page behind this, we can see that it renders a partial class two this underscore default class and inherits from the page, and then we have two events, page load, and then low data underscore click. So the functionality here is greatly tied to the functionality too. For example, this click button and they can’t be separated very easily, and on top of that, there’s a designer class that references to these different controls so that you can access them from your code-behind.

#2 ASP.NET MVC Is Easier To Test

Next, asp.net MVC makes it easier to test each component in a webforms scenario. It can be very hard to test a click event, but an asp.net MVC you can test almost anything. In this case, this action method right here can be tested by simply right-clicking and say, create a unit test. I can simply add this unit test to my MVC example tests.

I can assert for failure and hit okay and this will create a unit test here in my mic test project. If I go ahead and open that up, you can see that I can simply see the test here and I can invoke that method very easily from this standpoint. Now with web forms, it can be very difficult to create a test. If you right-click on this event, you could create a unit test off of this, but there is no output from this and the same goes with this low data click so that it’s not very easy to test this.

So in a lot of cases, in a web forms project, you’re going to test the business logic, but there’s really no way to test the UI logic in a web forms project without going through a lot of extra work. And finally, the HTML generated by an MVC application is way simpler than a web forms application.

#3 ASP.NET Produces Simpler Code

So if you’re a web application developer and you’re used to digging into the HTML, MVC is a lot easier to deal with. To prove that example. You can see this page here. This is the page that I displayed in my previous video displaying product information and if we look at the source of this page, it’s very simple. I have some metadata tags here, title tag, normal head, normal body stuff and what you’ll see here is that there’s nothing complicated here in a webforms scenario. You can see that we have this application here and I’ve created this data load button and this data load button produces this chart of prices. If I look at this source code for this page, you can see that first, we have, this is being done. A hidden section which has something like view stayed in it. Our MVC example didn’t have anything like this.

View state is how asp.net web forms keep’s track of all of these server controls and you state ends up becoming very bloated and very large in web forms applications. You also have this logic here for postbacks and things like that, so in general Web Forms tends to get more bloated, more HTML loaded. This last section really is just code that was written for the page, but you can see there are pieces of code that are written in an injected into this page for Web Forms application.

One other piece that is very interesting in a web forms application is that the whole body is wrapped in a form and that tends to be a problem for a lot of people because you have this form that’s wrapped in this entire body. You can’t inject another form inside of here, so then you get some weird scenarios where you’re trying to put another form inside of a form and you really can’t do that in an asp.net web forms scenario.

Thanks for reading and stay craig on coded.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

11 + 15 =

Pin It on Pinterest