Friday, April 16, 2010

Linfu Duck Typing Part 1 – Revealing Secrets

Don’t you just hate it when a class in the .NET Framework or another third-party library or framework provides that particular feature you’re looking for only to realize that this class has not been made accessible? One blatant example of this is the SqlCommandSet class in the System.Data.SqlClient namespace.

One can use reflection in order to access and use this private class. An even better and more efficient approach would be to use delegates instead. But there’s also another simple and elegant solution which I briefly mentioned in a previous post namely duck typing.

Now in order to access the SqlCommandSet class we need to define an interface with all of the methods we want to access from our code. Reflector can come in handy in this case to determine which particular methods we need.

public interface ISqlCommandSet : IDisposable
    SqlConnection Connection { get; set; }

    void Append(SqlCommand command);
    Int32 ExecuteNonQuery();

This is pretty much what we need from a SqlCommandSet. Now we can create a factory class for creating instances of the SqlCommandSet class.

public class SqlCommandSetFactory
    private readonly Type _typeOfSqlCommandSet;

    public SqlCommandSetFactory()
        var system_data = Assembly.Load("System.Data, 
        _typeOfSqlCommandSet = 

    public ISqlCommandSet CreateSqlCommandSet()
        var instance = Activator.CreateInstance(_typeOfSqlCommandSet, true);
        var dynamicObject = new DynamicObject(instance);            
        return dynamicObject.CreateDuck<ISqlCommandSet>();

The System.Data assembly is loaded and the type of the SqlCommandSet is determined in the constructor of the factory class. In the factory method itself we first create an instance of the SqlCommandSet class and then use LinFu’s DynamicObject to ‘cast’ it to our ISqlCommandSet interface. And this is pretty much it. Now we can use a SqlCommandSet at will.

using(var connection = new SqlConnection(@"..."))

    using(var command1 = new SqlCommand("INSERT INTO BLABLA ..."))
    using(var command2 = new SqlCommand("INSERT INTO BLA BLA ..."))
        var commandSet = new SqlCommandSetFactory().CreateSqlCommandSet();
        commandSet.Connection = connection;

Again I’d like to mention that I’ve been using the DynamicObject class from Linfu 1.0 which you can download here.

Saturday, April 10, 2010

Book Review: ASP.NET MVC in Action

ASP.NET MVC in Action

Earlier this week I finished reading ASP.NET MVC in Action written by Jeffrey Palermo, Ben Scheirman and Jimmy Bogard.  I came across several good reviews about this book so I finally decided to give a read. 

This book is not just about the API’s of the ASP.NET MVC framework but also about good design principles for creating long-lived web applications. When you’re not familiar with the S.O.L.I.D. principles and also want to learn about the very basics of Domain-Driven Design in the context of web applications, then this book is definitely for you. For me personally, being indoctrinated with this stuff for quite some time now, I didn’t pick up that many new things except for some of the ins and outs of the  ASP.NET MVC framework. 

Nonetheless, I strongly believe that this kind of books are still highly needed to raise the bar of quality in the .NET community. Practices like unit testing, continuous integration, DRY, etc. are sadly enough not part of the mainstream yet. The authors also had the courage to provide an entire chapter where they explore other MVC frameworks like MonoRail and Ruby on Rails which, from my point of view, adds tremendous value to the awareness of the unsuspecting .NET developer.

The first three chapters provide an in dept overview of the parts that constitute the MVC pattern, namely the Model, the View and the Controller. I must say that I’m still not a big fan of the way ASP.NET MVC passes a dictionary of data to its views. I know that you don’t have to use it this way and that a single strongly typed model can be passed to the view. But the presence of a dictionary for me is just a too big a risk that can easily result in creating a mess.

Chapter 5 provides a solid overview of the routing capabilities of the .NET framework including some nice examples. Chapter 6 contains information on how to extend ASP.NET MVC using controller factories, action filters, custom view engines, etc. … . Chapter 7 discusses how to leverage view helpers and creating partial views which I find are quite important features for composing scalable web applications.

Chapter 8 is the one I skimmed through very quickly as it shows how to use existing ASP.NET features with ASP.NET MVC. The part on using existing ASP.NET server controls is totally unnecessary and although you can pull it off in a couple of cases, I advise you to not apply these techniques in a production application.

Chapter 9 shows how to use AJAX in ASP.NET MVC. Pretty basic examples there.

Chapter 10 talks about deployed and contains some interesting ideas on how to use MsBuild, NAnt, Rake, … for automatically deploying a web application into a testing and/or production environment including configuration and environmental setup. 

As already mentioned, chapter 11 shows some basic features of other MVC frameworks. In the last two chapters, the authors provide some best practices and recipes for ASP.NET MVC.

As I already mentioned, if you’re only goal is to get up to speed with the API of ASP.NET MVC then I guess there are better resources out there. For that reason I also bought the Mastering ASP.NET MVC 2.0 series on TekPub to fill in the potential gaps and also to learn more about the new stuff in ASP.NET MVC 2.0.

All in all, I enjoyed reading this book. If I have to give one suggestion for improvement, then I would advocate to add more cohesion to the book. I feel that some topics are scattered all over the place. For example, the use of Castle validators is discussed in two different chapters in the book with some degree of overlap. If only one part of the book was devoted to validation, then its far more easier to go back there for future reference.

I noticed that a second edition of ASP.NET MVC in Action is already available as an early access edition. While looking at the table of contents, some of the content from the first edition has certainly been moved around which I consider to be a good thing. I also hope that the second edition will also include some comparisons with features from Fubu MVC and OpenRasta.

Until next time