Saturday, November 14, 2009

Wrapping objects with interfaces

Have you ever had to map data to or from 3rd party components? Have you ever had to use reflection to access properties or methods you know the signature for on object you dont' have type for at compile time? This has happened a few times for me, especially when I have to deal with webservices defined by autogenerated classes.

My solution to this is to create an interface that resembles the objects I need to handle. I have made a small helper that will make proxy object for you that implements the provided interface and forwards all property calls and method calls to the object you need access to. This is how you can use it.

IMyInterface wrapped = anyObject.WrapAs<IMyInterface>();

Now you are able to create an interaction that can operate on your object. This might also be a way integrate with components you don't want a direct dependency on.

Tuesday, November 10, 2009

Dynamic duck typing in .Net 3.5

I just saw a post about wrapping reflection as extension methods.

Reflection as such is not rocket science. Reflection is for discovering features of objects you don't know how is structured at compile time or it is for interaction with objects you know how operate but don't have a type for.
It's not a big problem to use reflection to access regular properties or methods. The extension methodsI created for wrapping properties and non generic methods are merely wrapping some simple reflection calls.

Alright, let's get to it:

Setting the value of a property.
o.SetValue<string>("Title","Some title");

Getting the value of a property.
string title = o.GetValue<string>("Title");

Calling a method

With generic methods its a different story. I can't find it just by a name. I have to find the version that supports generics. Not only do I need to provide parameters for the method call but also the generic types. This means I can't wrap a generic method call in just one extension method.

This is the situation I want to handle. I have an object and want to find a Dao object for it to be able to save it. If you know the type compile it could be something like this:

IDao dao = factory.GetDao<Blog>();

The same code executed with my new extension methods:

object o;// object you want to save
object dao = factory.GetMethod<object>("GetDao",o.GetType()).Call();

It's a bit more verbose than direct code, but a lot more compact and precise than if you had to use the System.Reflection namespace. The GetMethod method gathers the name of the method together with a list of types that defines the methods generic properties. The Call method accepts the parameters for the method call and invokes the method.

The source for the extension methods can be found here.

Friday, November 06, 2009

Extensions methods FTW

Extension methods is my favorite language feature of .Net. Although they are just symantic sugar they provide a nice way of compacting code to express its real intent.

Here is a collection of my favourite homemade extension methods. Most have been included in the plain framework.

#1: My extension method for making !string.IsNullOrEmpty(somestring) more readable.

#2: Bringing the IN operator from sql to .net. It's implemented on enums but could be any type.

#3: Execute an operation on a collection. I use this all the time and I also have a version that can return the result of an operation as a new collection. That is basically equal to what the .Select extension from System.Linq does. The functional approach of F# inspired me to this.

#4: Get a typesafe value from a web request

#5: Gets all the controls of a certain type from a form. Originally I also had a version that operated on any type of object. I soon found out that searching an object structure without any constraints is a performance disaster. Being limited to finding controls on a form is much better as it's a well defined hierarchy that easy and fast to traverse. The idea springs from looking at JQuery. Some examples of usage can be found in my blogspot about oneliners in .net

#6 Save an object. This operates on types not defined yet. If you implement the IBusinessEntity<> interface you will automatically get a Save() method. It gives a feeling and ease of use as that of the Active Record pattern, without the object knowing anything about persistence. The plain framework also contains extension methods for Delete() and Validate().

What would make this perfect is the ability to define extension methods to be used in a static context. If my business class was Product, I would then be able have a Product.Get(...) method. That would be cool.

Thursday, November 05, 2009

Flank your problems

The other day I took my youngest daughter to football practice. She is 5 years old and has a lot of energy to spare. Its great fun to watch these small kids play around on the field.

When I saw them playing I noticed a pattern I have seen in software development. All the kids were chasing the ball at once. This made it a somewhat confusing match to watch. The problem was not that the girls lacked the technical skills to play football or that they didn't understand what the goal of the game was. The problem was that they were so entutiastic about getting the ball to the other end of the field that they all rushed to the ball to help the team get the ball to the other end.

I think this anti pattern should be called something like "Beeing too specialized" or "Follow the leader". The pattern to overcome this would be the "Think before you act" or "Flank your problems".

My point is that you shouldn't rush for a quick solution as there might be a better and faster solution if you're open minded.