Wednesday, December 16, 2009

I'm All About ASP.NET MVC

Tomorrow, when I walk the halls of my corporate job and find myself caught in an eye-to-eye stare-down with a Java web developer I may not be thinking the same thoughts. I've had my share of Java vs. .NET arguments over the years and I hate to admit it but the Java people have a lot going for them. Often times Java is the runway for .NET's future look. Thus it is to no surprise now that we've gotten our hands on ANT, Hibernate, and jUnit we follow up by stealing Swing. .NET makes its rounds with all of Java's daughters--which happens to be one of my favorite interjections in a .NET vs Java discussion. You should see their faces.

What is ASP.NET MVC?


Every ASP.NET developer I've talked to in the last few days about MVC have asked me this question, which kinda surprised me. In general, many of them have never heard of MVC at all. My first interaction with MVC occurred after reading an article over at A List Apart about JavaScript MVC--which is my preferred client-side architecture. Great article.
MVC stands for Model-View-Controller. When you think about web work, isn't it generally you got some data(Model) that you present to the user(View) and allow them to interact with(Controller)? I know that about sums up my work experience. ASP.NET MVC is .NETs response to the Java/Ruby MVC craze. All us .NET-ers are definitely a few seasons behind.
Remember when the .NET web platform released? Almost a decade ago. Honestly though, the web was still emerging as a platform for applications. In my opinion, when Microsoft released ASP.NET they were thinking "how can we easily convert our application developers into web developers" and not "how can we make the best web development platform out there?" They've reaped the consequences for it too-- such as the ViewState, lack of true architecture guidelines, the page life-cycle, oh and how about their ASP.NET Ajax(Atlas) implementation? Horrendous. All ease of use and no performance. ASP.NET MVC is Microsoft's way of trying to come clean. Let's try to be more forgiving.

Bottom Line: Whats the Code Like


Amazing. Simply amazing. I've been working through the Wrox Professional ASP.NET MVC book and have over and over again found myself making "exactly!" outbursts. I'm not going to go through a complete overview of the MVC framework, but here are a few "highlights:"

Using LINQ to SQL as Your Model


ORMs (object relational mappers) are pretty popular right now--especially NHibernate. No surprise, they save you a ton of time defining classes and writing SQL statements. After you have a database table set up and accessible in your Visual Studio Database Explorer, a little Solution Explorer right-click add-new > Data > LINQ to SQL and you're rolling. With the class that is created, one can alter database data by easily interacting with the generated class. Like:

// Retrieve Dinner object that reprents row with DinnerID of 1
Dinner dinner = db.Dinners.Single(d => d.DinnerID == 1);

// Update two properties on Dinner
dinner.Title = "Changed Title";
dinner.Description = "This dinner will be fun";

// Persist changes to database
db.SubmitChanges();

And yes, it supports transactions, SPROCs, views, checks your data for SQL Injection attacks, and is easily extendable using partial classes and partial methods. Not really an MVC feature, but new to me.

ASP.NET MVC HTTP Post Handling


This is one of the things that really impressed me. A situation I find myself programming over and over again is doing GridView FindControl()s to grab an inputted value. With MVC, you can create a method in your controller that accepts one of your model classes, completely avoiding all of the monotonous value/control finding work. The values are automatically mapped using .NET Reflection.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinner dinner)
{
if (ModelState.IsValid)
{
try
{
dinner.HostedBy = "SomeUser";
dinnerRepository.Add(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinner.GetRuleViolations());
}
}
return View(dinner);
}


Theres More


I didn't even get to talk about Validation, separation of concerns, SEO-friendly URLs and more but... I need to get back to studying.

No comments:

Post a Comment