Fluent.Interface


Category Archive

The following is a list of all entries from the ASP.NET category.

TDD on show with ASP.NET MVC at Mix09

Its been a long road, but ASP.NET MVC 1.0 is out and now is open source as posted on ScottGu’s blog.  Its a great milestone and one that has been paraded in front of the the Mix09 attendees in Vegas.  I can recommend setting aside the two hours required to get through Phil Haack‘s Nija preso, and Scott Hanselman‘s NerdDinner talk.

Those guys along with Rob Conery and the main man Scott Guthrie have been slaving away on a new book ASP.NET MVC 1.0 from wrox press.  

the extended forehead edition

And have been kind enough to give away from free a chapter that walks through all the steps required to create the NerdDinner sample, which includes details on:

  • Validation
  • Security tips
  • Rendering partial controls
  • Custom routing
  • Authentication/Authorization
  • AJAX (with jQuery).
  • LINQ to SQL mapping with scalar/table MSSQL functions (very cool!) 
  • Dependancy injection with MOQ

It also has 20 pages dedicated to Unit testing, and encourages using the ‘AAA’ approach: Arrange, Act, and Assert which looks like this:

[TestMethod]
public void Dinner_Should_Not_Be_Valid_When_Some_Properties_Incorrect() {
//Arrange
Dinner dinner = new Dinner() {
Title = "Test title",
Country = "USA",
ContactPhone = "BOGUS"
};
// Act
bool isValid = dinner.IsValid;
//Assert
Assert.IsFalse(isValid);
}

PhilHa and ScottHa also spend a good amount of time covering red/green/refactor in their videos, and why it is important to have the test fail first.  Great Stuff!


Billy McCaferty and his S#arp Architecture

I have been following Billy McCaferty’s Sharp Architecture project for some time now – a best practice Domain Driven Design approach to developing ASP.NET MVC web apps.  It has seen a number of iterations, the latest including the configuration with FluentNhibernate and independent ServiceLocator

In his recent article on InfoQ he discusses some of the motivations behind his TDD/DDD implementation.

What’s currently lacking, at least in the world of .NET web development, is a common architecture and foundation for application development which combines best of breed technologies and techniques, using recent technologies based on proven practices, while taking into account the availability of high quality tools developed by the open source community. S#arp Architecture is a response to this need. The open source S#arp Architecture attempts to leverage the proven practices described in this article with carefully selected tools to increase development productivity, while enabling a high level of quality and maintainability

He also makes dudious use of the T4 templating support built into VS2008 to codegen files from the Model objects through to Views and Controllers.  A great kick start for any new project – Well worth a read!


Open source .NET CMS, Oxite

There are numerous open source CMS platforms out there, wordpress being one of the most recognizable (hosting this blog).  But when it comes to .NET the choice is more limited.  Microsoft’s channel 9 community platform started its life from the telligent Community Server codebase.  Since then that team has built a new Blog/CMS platform Oxite which powers MIX online.

The project targets ASP.NET MVC developers and leverages LINQ to SQL for persistence, but hides the implementation behind a clean repository interface.  There is not end-user installation so without VSTS you will need to attach the database and config a login.  Boasting ‘standards compliant’ features such as RSS and Trackback capabilities, it makes good use of modern features such as extension methods to keep the front end clean.  Although still not out of beta, the ASP.NET MVC is proving popular, and powers some high traffic sites such as stackoverflow.com.

Microsoft’s other CMS platform sharepoint is getting behind CMIS. The content management integration services (CMIS) specificiation leverages SOAP, REST and Atom to enable communication with and between ECM systems. Although Roy Fielding isn’t impressed by the initiative, in a Web 2.0 environment a languague neutral API is almost as important as the website – so it should bring for some good vendor competition.  Alfresco in particular has recently released beta support for CMIS on its java-based platform.

Also worth a look the .NET CMS Umbraco and the popular Radiant CMS built on rails.


Javascript Templating within ASP.NET

ASP.NET has rich support for calling AJAX web services, but is usually ignored in preference for a simpler UpdatePanel implementation.

The UpdatePanel is great because it enables developers to slice up their existing pages, and introduce AJAX functionality without significantly changing their post-back logic.  It does however send back all the form variables + ViewState for that page.  It the receives HTML content blocks from the server which is dynamically renders on the client.

But when rendering a data list of objects on a page, you could consider web services:

[WebService(Namespace = "ns")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[
ScriptService]
public class ArticleWebService : System.Web.Services.WebService
{
 
[WebMethod]
  
public List<ArticleDto> GetByName(string name) {
    return null; // TODO: Return list
  }
}

This web service is referenced in the script manager on an ASP.NET client page.

<asp:ScriptManager ID="scriptManager" runat="server" >
  <Services>
    <asp:ServiceReference path="~/services/ArticleWebService.asmx" />
  </Services>
</asp:ScriptManager>

Over the wire the web service call formats objects using JSON format, which ensures a compact and efficient delivery of information.  A list of client-side javascript objects can then be passed onto a templating library which when combined with an in-line template can render HTML to the browser.  I evaluated the following:

  1. JSONT – Transform JSON into text using XSLT style nested rules.
  2. ZPARSE – Lightweight and extensible similar to TrimPath.
  3. FOO Framework – Like ZParse from Rizqi, but Depends on Prototype (1.5+).

They all have their strengths and weakness.  I prefer to minimize the number of AJAX frameworks, and prefer flexible syntax of ZParse (you can customize your parser).  Rich Strahl in his recent What’s Ailing ASP.NET Web Forms post consider’s prototype to be a more well-rounded library, but it requires some tweaks and doesn’t handle character escaping the same way as AJAX does.

The FOO framework is neat in-so-far it annotates in-line HTML but it is much slower (as it iterates over every node with the DOM every time it renders a page).  The alternative approach with JSONT or ZPARSE is to place the template within a hidden textarea control eg:

<!-- Declare the template inside a text area, so we can interegate the .value -->
<textarea id="zparseTemplate" style="display:none">
<!--:foreach a in articles:-->
<div class="wof special-wof">
<h3>
<a href="#" title="{$a.Headline}">{$a.Headline}</a>
</h3>
<p class="cfix">{$a.Abstract}</p>
</div>
<!--:/foreach:-->
</textarea>

The using ZParse the template could be loaded up with a series of ‘articles’ that were loaded from the web service callback.

var parser = new ZParse(Implementation);
var template = $get('zparseTemplate').value;
parser.parse(template);
var content = parser.process(articles);
$get('zparseBlock').innerHTML = content; 
 

A number of factors need to be considered when option for AJAX+Templating for rendering content like this.

  • Content won’t be visible for SEO purposes.
  • Best for dynamic content, where a page would otherwise be cached.
  • Performance test: 2 small hits may prove less scaleable then 1 bigger hit.

I will be following up with an article on Caching.


Practical example for ViewState

There have been considerable improvements in the way ViewState is serialized with ASP.NET 2, and with the added ControlState functionality it is possible for third party controls to behave themselves with ViewState disabled.

There are some great articles written on understanding ViewState like this one by Scott Mitchell. My practical example (see below) list some ways to minimizing view state.

My example utilises a custom TextBox control that raises events when it’s value has changed, or had saved its ViewState. In addition I persist the number of times it has ‘Changed’ to illustrate the key point:

  • A control with ViewState disabled will still fire its Change event if the posted-back value has changed when compared with its initialized value.

Developers often DataBind ListItem controls if (!PostBack) on Page_Load, however this results in the ViewState bloat. An alternative is to always DataBind on Page_Init, this is particularily useful for reference data that doesn’t change often and is ‘cheap’ to load:

  • A control DataBound in Page_Init routine it will still receive it’s PostBack event and post data without requiring ViewState.

Please see my View State Example project for more details.

Julian.