Friday, October 19, 2007

DuckTyping - A Nice Tool to Keep in Your Bag of Tricks

Technorati Tags: , , , , , ,

 

I was recently working on a class designed to manage simple data entry within Windows forms. Essentially, the majority of data objects are always edited with the same business logic; the only difference is the actual properties of the data being edited. Before, I discuss how DuckTyping fits-in, its important to understand the situation:

When I am designing, I perpetually seek to recognize repeated patterns. For example, while creating several data entry forms, it became obvious they all required something like the following:

DataEntryBar

DataEntryBar2

 

"Data entry bars" similar to these are located on every data entry form. I originally added each button manually to each form. Then I recognized a "pattern" and created a UserControl that contained all the buttons. However, I was still left with registering each of the respective click events for Add, Delete, edit, etc. for each respective form and then calling matching methods within my data manager.

Again, I recognized a pattern and realized I wanted my data entry manager to auto-register the events for me and my next change resulted in defining an enumeration for pre-defined "button functions". In other words, how would my data manager know that the Cancel button was actually for canceling? I had to assign something to each button to declare their purpose.

Then... the next problem... I didn't just have bars such as the above, I also had drop-down menu support such as this:

DataEntryMenu

 

Both could be "active" at the same time. Therefore, I chose not to add the bar or menu directly to the manager. I decided to have each control expose an IList<Button> containing all the buttons. Well, not exactly Button either. The control library I use utilizes different buttons for menu support than the buttons used in the tool bar. It meant their was no common interfaces or base class (one is not even derived from Control) between the different button options. I was getting a little frustrated with finding a way to get Visual Studio or C# to recognize that there is usable commonality between different objects.

 

I could create an interface and , in fact, I did, but some objects are contained in a third party library and I didn't necessarily have the option to implement the interface (either I had no access to the source code, or a class was sealed, or it was really just a pain).

 

In came DuckTyping to the rescue! I first experimented with DuckTyping after reading this article on the Code Project: http://www.codeproject.com/cs/library/nduck.asp

 

I use this DuckTyping library which does differ from above: http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx

 

I use the latter library because it seems to be maintained/ upgraded while the version on the code project does not appear to be maintained.

 

What does it do? Well, let's say I know that all the objects I need for my data manager require this interface:

 

Code Snippet

    /// <summary>

    /// Implemented on objects that respond with standard Click event.

    /// </summary>

    public interface IStandardClick {

        /// <summary>

        /// Occurs when the control is clicked.

        /// </summary>

        event EventHandler Click;

    }

 

However, none of the objects actually implement that interface; although, I happen to know they do expose the correct Click event.

 

With DuckTyping I now have an option to dynamically cast at runtime to the interface I need. Here is a sample:

 

Code Snippet

            IStandardClick clickableObject = DuckTyping.Cast<IStandardClick>(myButton);

 

The "myButton" object does not actually implement IStandardClick, but it does contain the required members (the click event). In other words, with DuckTyping I can cast any object to any interface so long as the object actually does implement the required interface members.

For me, DuckTyping is very cool and has an appropriate place in my bag of tricks. I suggest all C# developers download the library (http://www.deftflux.net/blog/page/Duck-Typing-Project.aspx) and experiment, you may never know when such a tool will come in handy.

2 comments:

Anonymous said...

This is great! One thing, can you mention/recommend an icon set like the one you use?

AL FAHAD IT CONSULTING said...

Best IT and software consulting services in UAE, KSA, and Bahrain.
Zoho software integration services

Zoho Integrations