Twitter Feed Popout byInfofru

Adventures in .NET Software Craftsmanship!

MVC Extensibility–CMAP Code Camp

downloadThe following is the download link for the demo code and slides for my presentation on MVC Extensibility done at the Central Maryland Association of .NET Professionals Code Camp (CMAP Code Camp).  Thanks to all who attended my presentation, it was extremely fun plowing through a ton of code all across the stack of the ASP.NET MVC Framework request execution lifecycle.


Download Demo Code & Slides


Also, big thanks to Christopher Steen and Randy Hayes for hosting and allowing me to participate as a speaker in such a wonderful event.

Codes and Slides on MVC Extensibility

I have made my slides and code available from my presentation on MVC Extensibility at the MVC Conference.   Before I give out the details,  I would like to congratulate the organizers of the event for their great work on making this amazing event happen.  In particular, I would like to extend a personal thanks to Eric Hexter, and Javier Lozano.  Thanks again guys, for giving me the opportunity to speak on such an amazing digital venue.


The the slides and code are available at codeplex in this location.

Presenting at MVCConf

I am excited to announce that I will be participating as a presenter at MVCConf.  MVC Conference (MVCConf) is a free virtual conference focused on one thing only, writing awesome application on top of the ASP.NET MVC applications, for more information go to


I will be doing a presentation on MVC Extensiblity focused on plugging functionality onto your MVC application to enforce separation of concerns therefore enhancing testability. The following are the preliminary details:


MVC2 Extensibility

Learn the basic extensibility points of the ASP.NET MVC Framework by refactoring an existing code base and making changes that will make your application less fragile and easier to test.  The main focus will be how to leverage dependency injection, routes, action filters,  model binders, etc. to build a better all-around application.


Hope to see you online on the 22h of July!

NHibernate Audit w/Database Server Time

Disclaimer: This articles highlights a workaround not a best practice. In fact, if you are so inclined, please provide me with your feedback on how you would have solved this problem differently.


At a recent consulting gig a client’s IT department made the very common request that we store classic audit trail information in all database records.  What that means, at least to me, is that every single record in the database is going to have a [CreatedBy], [CreatedDate], [UpdatedBy], and [UpdatedDate] fields.  I found a lot of help on the internet on how to implement this by using NHibernate and its Event Model introduced (ported from Hibernate) in the 2.0 version.


These are some of the articles I based my solution on:


Both articles are pretty much self explanatory and solve all the business requirements except one, and I quote “All dates in the audit database fields must be the Database Server Time not the Application Server Time”.  Now this might sound like a simple requirement that can be solved this by defining custom SQL queries for insert, update and delete operations in the NHibernate mapping files but doing so will introduce the following problems:

1.- Writing custom xml mapping files defeats the purpose of using FluentNHibernate and its AutoMapping capability.

2.- Our integration testing strategy that relies on the use of SQLite would be become complex as we would have to define custom .hbm mapping files for testing and different ones for production ones that use T-SQL for SQL Server 2008.


My solution, albeit probably not a good one, was to define a named query for retrieving the Database Server Time from the application.  The following is the code for registering that named query using the NHibernate’s fluent configuration API.


public const string SQLSERVER_HBM_MAPPING_XML =
        <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
        <sql-query name='GetServerDateTime'>
            SELECT GETDATE();

public const string SQLITE_HBM_MAPPING_XML =
        <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
        <sql-query name='GetServerDateTime'>
            SELECT datetime('now');

public static void RegisterServerDateTimeMethod(Configuration cfg) {
   if (cfg.Properties["connection.driver_class"].Contains("SQLite"))


The key is to call the RegisterServerDateTimeMethod which registers the named query depending on the connection driver class settings name.   Now to make the process of calling this named query easier I wrote an extension method for NHibernate’s ISession that looks like the following.


public static class ISessionExtensions {
    public static DateTime GetServerDateTime(this ISession session) {
        var resultFromServerDateCall = session
        DateTime result;
        if (resultFromServerDateCall is string)
            result = DateTime.Parse((string)resultFromServerDateCall);
            result = (DateTime) resultFromServerDateCall;
        return result;


The following test illustrates how to use the Extension method.
* Needless to say this test is very brittle and doesn’t really test much, but it gives me some peace of mind as it verifies that the .hbm file mapping syntax is valid.


public void NhRepositoryCanRetrieveServerDateTime() {
    var serverDate = this.Session.GetServerDateTime();
    Assert.AreEqual(serverDate.Year, DateTime.Now.Year);
    Assert.AreEqual(serverDate.Month, DateTime.Now.Month);
    Assert.AreEqual(serverDate.Day, DateTime.Now.Day);

Double Duty at Richmond Code Camp 2010.01

I will be presenting at the upcoming Richmond Code Camp 2010 spring edition, held at the J. Sargeant Reynolds Community College campus on Parham Road.  For additional details visit the Richmond Code Camp website at


I am looking forward to seeing all the familiar faces that make up the Code Camp experience what it is. 


The following are the topics I will be presenting on,


Articulate your Data with FluentNHibernate

Learn how to use NHibernate, arguably the most used and powerful OSS ORM, using strongly typed fluent configurations. Avoid the hassle of building hundreds of XML files for mapping and configuration, and explore the benefits of Convention over Configuration while increasing your productivity.


Designing an ASP.NET MVC2 Application for Testability

The ASP.NET MVC Framework greatly improved the testability of an ASP.NET Web application by allowing us to easily apply unit and integration testing techniques to our Controllers, Filters, Binders, and more. But testing these components in isolation doesn't prove that your site functions as expected and outlined in the business requirements. Learn how to design and implement testability from the view perspective using BDD techniques applied to frameworks such as SpecFlow, Watin, and NUnit.