DictionaryAdapter is Love (Part 1)
Nestled deep within the Castle Project repository, there is a wonderful library maintained by Improving Enterprises’ own Craig Neuwirt which can forever change how you deal with the assortment of Key/Value pair structures in our programming life. That library is called DictionaryAdapter, or more properly Castle.Components.DictionaryAdapter and the source can be found at : http://github.com/castleproject/Castle.Core
So how does this library work? Let’s take a quick tour around, shall we?
So you’ve got a Dictionary…
static void Main(string[] args) { var dict = new Dictionary<string, object>() { { "UserId", 1234567 }, { "UserFirstName", "Tim" }, { "UserLastName", "Rayburn" } }; }
We deal with dictionaries like this all the time in .NET, the easiest example of which is ASP.NET Session but many others exist. Now lets assume that we wanted to access this information in a structured way. That is to say, instead of writing something like:
public static string UglyWayToGetUserFirstName(Dictionary<string, object> dict) { string userFirstName = "UserFirstName"; if (dict.ContainsKey(userFirstName)) return dict[userFirstName] as string; return null; }
I instead want to write code which access the information using a strongly typed interfaced like this:
public interface IUserData { int UserId { get; set; } string UserFirstName { get; set; } string UserLastName { get; set; } }
Which would produce code like this:
public static string CleanWayToGetUserFirstName(IUserData data) { return data.UserFirstName; }
How do I do that? DictionaryAdapter to the rescue. Add a reference to Castle.Core, and a using statement for Castle.Components.DictionaryAdapter and then the following code will work:
static void Main(string[] args) { var dict = new Dictionary<string, object>() { { "UserId", 1234567 }, { "UserFirstName", "Tim" }, { "UserLastName", "Rayburn" } }; IUserData data = new DictionaryAdapterFactory() .GetAdapter<IUserData>(dict); Console.WriteLine(CleanWayToGetUserFirstName(data)); Console.ReadLine(); }
Simple, eh? But there is much more coming. As you can see, there is a default convention that matches the property name of our interface to the key in the dictionary. Lets assume that you’re the type of person who believes having the word User in front of each property on the interface is a bad idea, but keeping it on the keys which lack the context of being IUserData makes sense. No problem, enter aliasing. Modify your interface like so:
public interface IUserData { [Key("UserId")] int Id { get; set; } [Key("UserFirstName")] string FirstName { get; set; } [Key("UserLastName")] string LastName { get; set; } }
And now your interface main method (no longer using our rather useless CleanWayToGetUserFirstName example above) can be refactored like so:
static void Main(string[] args) { var dict = new Dictionary<string, object>() { { "UserId", 1234567 }, { "UserFirstName", "Tim" }, { "UserLastName", "Rayburn" } }; IUserData data = new DictionaryAdapterFactory() .GetAdapter<IUserData>(dict); Console.WriteLine(data.FirstName); Console.ReadLine(); }
That’s a good introduction to the basics of DictionaryAdapter, but there is a lot more depth here that I hope to cover in future posts.