The Duhking library works by creating a dynamic proxy for the type you are wanting to "Duhk" which wraps the target object. In order to successfully duhk a type, the object in question must have methods and properties that match all of the methods and properties defined within the interface. If that condition is met, a interface proxy is created which takes care of making all of the calls.

So, why?

For the most part, you will find that you really don't need this library much, however there are a few cases where it comes in very handy. One case is increasing testability by providing the ability to represent a type that is not yours (i.e. a .Net framework/CLR type, or a type from a 3rd party) that is not expendable with an interface that you define. If you have ever tried to test code that is using the built in framework types to send SMTP messages, then you get the idea. This library would allow you to create an interface which defines the functions you need, and then wrap the real SMTPClient class in that interface, thus alloying you to test the code that needs the SMTP Client.

Another example would be the ability to wrap an anonymous type in an interface so that you could pass it between method calls without resorting to ugly reflection code.


Usage is pretty simple at this point. Simply reference the Duhking.dll in your project, and then add a using statement for the Duhking namespace. This will add two extension methods to all objects: CanBe<T>() and AsType<T>(). Both of these take a type parameter that is expected to be an interface (Currently an exception is thrown if a non-interface type is provided). CanBe<T>() returns a boolean which indicates if whether of not the object is compatible with the provided interface type. AsType<T>() will perform the "duhking" on the object to make it function as the provided interface type.

Overly Simplistic Example
    // ...
    var myObj = new MyObject();
        var myInterface = myObj.AsType<IMyInterface>();
    // ...

Overly Simplistic Example Using an Anonymous Type
    // ...
    someObject.SomeMethod(new { Name = "Bob", Id = bobsId, Role = "BigBossMan"}.AsType<IUser>());
    // ...

Last edited Jan 26, 2010 at 12:39 PM by ckramer, version 1


No comments yet.