Thursday, November 28, 2013

Why ASP.NET MVC is a better choice to develop your first dynamic web application

ASP.NET MVC has become very popular in web developments and yet some people are still refusing to move on to it. That happens when you get used to some technology. It's really hard to leave it and move on. I'm not writing this post to promote ASP.NET or something. I'm just telling you why I think it's a good choice, specially for a beginner. Because as a beginner, my experience was not a very good one with PHP. It took a long time to learn how to apply the MVC correctly to my web sites. Some of my views had unnecessary PHP code and controllers had some business logic inside them. Everything was very confusing. So I'm going to explain you how ASP.NET MVC gives you a different experience. 


First of all, if you have created web applications, complicated ones, without using a design pattern like MVC, you must know how hard it is to manage your code. MVC pattern provides a clean separation for your data access, views and business models. So if you want to change the design of your database, you only need to modify your data access classes. If you want to add some business logic to your application you can add them to your model classes. If you want to change the appearance of your web pages you only need to edit your view classes.


All this is because MVC separates HTML from your code. There are few choices to develop your web application using MVC. If you are a Java developer you can use one of the frameworks available such as Spring or Play. If you are using PHP you can use frameworks such as CodeIgnoter or CakePHP. And of course there are many other ways which I may even haven’t heard of. I’ve tried both above options before move on to ASP.NET web development and I found it a little bit hard to adopt as a beginner. Because most of the things had to be done manually even with the use of frameworks. It was a lot of work.


But once you start with ASP.NET MVC you will realize that a lot of things are done by the framework. So as a beginner you won’t have to worry about what goes where or where to put things. All you have to concern about is the functionality you want to add to each action.


Let me give you a brief explanation on the role of ASP.NET framework in running an MVC web application. As I said, all your html files are placed in a separate views folder with ‘.cshtml’ extension. And page files are stored separately as ‘.aspx’ files. When a request comes to a particular page, ASP.NET framework combines both types to generate an HTML file and sends it as a respond.


So why use this? I already told you that there are lots of auto generations. Other than that, it is very easy to combine database to your project with the help of a great framework called Entity Framework. This framework provides you a nice DbContext instance, which does all the communication with the data base. So no more long SQL  :) . This is called model first approach with EF. There is even cooler version called code first. In that, you write your model classes first, and then you build your code, EF creates the DB for you!!!


After combining the database, you have to write controllers and views, right? With Visual Studio you can right click on your controller method and select ‘add view’ then the view will be created in the correct place. You don’t have to manually go and create folders. When you create the views there are lots of auto generation options which I will talk about in another blog post.


Finally in your views, there is a really nice parser engine, Razor which enables method like html code generation and it is really easy to use. So as you can see, there are lots of help to make your web development easier. Once you start with all these help and build few apps on your own you eventually can understand how all these things work.


I’m going to illustrate how to develop a simple web application using all above ‘help’ in my next few posts. Until then, Happy coding!!! 

No comments:

Post a Comment