.NET 4.5 Raises the Bar for Web Development Productivity
With the release of .NET 4.5 nearing, Microsoft has started to pull back the curtain on the next version of their development platform. While there has been a lot of fanfare around Metro and Windows 8, the improvements made to the core features of .NET, which have traditionally focused on web, service, and data development, may steal the show in the short term.
ASP.NET Web Forms has seemed relatively stagnant over the last few releases of the .NET framework, yielding much of its developer market share to ASP.NET MVC. In .NET 4.5, Microsoft has done a lot of work to decrease the productivity gap between its two web development offerings by supporting model binding in ASP.NET Web Forms. This binding allows developers to skip the service calls and binding in the code behind and directly assign values to the control.
Even though this method does save time, it does not completely separate the page’s rendering from its business logic: the name of the service method which loads the grid is embedded in the control.
Apart from model binding and other improvements in the ASP.NET stack, such as improved HTML 5 support, decreased memory consumption, and the ability to easily write asynchronous code, the ASP.NET family now enjoys a richer development experience within the Visual Studio editor. Among the more visible improvements are Smart Tasks. With a Ctrl + . (dot) from inside a tag, developers can use Smart Tasks to speed along development and not have to know the details about how a given control can be configured.
RESTful web services have been available in some form or another since the introduction of ASP.NET MVC and in the latest version of ASP.NET MVC, Microsoft has introduced Web APIs to standardize this concept. A key feature of Web APIs is their ability to reduce development time by enabling RESTful methods to expose IQueryable<>. This feature can help developers create focused client-side queries without having to produce the mountains of code typically needed to produce a useful service offering. In line with the rest of .NET 4.5’s improvements, the code footprint for this feature is minimal.
For the standard API Controller below:
A REST query based upon GetProducts() which returns all products:
For the queryable API below:
A REST query based upon GetProductsByQuery() which returns all products which cost less than 4:
Filters are applied to the later query at run time to shape its results. This allows one method to service several features but also reduces the focus of the method. Any special cases or side effects needed for a specific feature will likely require their own implementation.
Finally, to manage the data you will need to expose through your Web API, Microsoft offers the Entity Framework 5. While the different models of the Entity Framework have been around for a while (such as Code First, Database First, and Entity First), Entity Framework 5 has introduced a concept to automate the synchronization of the model and the database over the lifespan of a development effort. This can remove many cycles spent in creating SQL change scripts and managing data that has already been persisted. It is reliant on the on the Package Manager Console to orchestrate the migrations, but it is well worth learning the syntax.
From within a Code First application, run this command:
If the “InitialCreate” file is not added to your Migrations directory, run this command:
For the given model:
If we add a property “HasLid”:
We can run another command to update the database with our model change.
If you want to roll back your changes, simply run the add-migration command and identify the target you would like the database to reflect:
While these changes have been largely received as positive and as improvements to the productivity of a .NET developer, there is some concern with the amount of changes introduced into the 4.5 release. Greg Duncan succinctly demonstrates his concern for the speed at which Microsoft is evolving the .NET framework: “I guess you could say that Microsoft (or the teams involved) are being agile and trying to constantly improve with each iteration, learning from the last...? (cough... so I hope at least... cough).”
Stephanie Davis (nee Stewart) Dec 21, 2014