Wednesday, May 06, 2009

Proper 0.2: INotify, DependencyProperty Support

{

Several years ago, in a land of Visual Studio 2005 and no CodeRush, I made a tool called Proper to automate the monotony of creating properties. You just type something like “int Foo;string Bar;” and you’d get basic property generation. You can use shortcuts to types by typing an undercore “_” character. For example, “i_” will pop up int, “dt_” would pop up DateTime (You can also create your own). It’s funny how the stuff that you’re half serious about can last so long – at the time I wanted to write something useful entirely in JavaScript and to experiment a little with JQuery.

The tool suffered neglect for the next few years until recently when I began working with Silverlight and was amazed at how a similar monotony (and more code!) accompanies using INotifyPropertyChanged and implementing dependency properties. I remembered my old tool and added support for generating them there. Here are some screenshots that should illustrate how it works (you select “advanced options” and then select a few checkboxes, in a nutshell). I’ll update the feature to report bugs – I’m sure there are a few. I also neglected to update it for VB.NET – I’ll do that over the next few weeks.

If you do spot a bug, let me know. There are a few new goals I have with the tool, the first of which is refactoring some of the embarrassing JavaScript I crufted together to make this work. I remember Kevin Dangoor talking about Embarrassment Driven Development (when your code is so embarrassing that you’re motivated to update it); that’s the one thing about JavaScript – you can’t hide the way things are constructed. :)

}

2 comments:

Dave said...

Nice idea... few questions though (I promise the fourth one is easy):

We could talk for hours why this isn't easier to do withe the lang primitives... but in the meantime people (using VS) could use code snippets for a more integrated experience... why not do it as snippets?

To address the lack of VB support... why not output a CodeDom object and leave it to a language provider to write the code... that is, recently I discovered the XsdDataContractImporter class that lets you take a set of XML schema documents and generate a CodeCompileUnit which is then sent to a code provider that does the "semi-harry" details of writing code (give me a little room here... I might have gotten a few class names wrong, I didn't verify it all, it's "Memory Driven Development"). I realize you've got a website here and CodeCompiieUnit probably isn't a serializable types... but you could probably stop the "strcat" and move to the CodeDom (assuming you are doing strcat... "Assumption Driven Development")?

As I understand it (mem driven dev again) dep props are only necessary for the TARGET of a data binding the the DubPeeEff/"web killer" (aka Silverlight) world so why bother... just make the source props INotifyPropertyChanged (don't "ultra"-long interface names make you think something is inherently wrong) instead?

What is the answer to life, the universe, and everything?

David Seruyange said...

Hey Dave,

Thanks for the questions, I'll try to be brief and avoid "one time at band camp" stories... :)

> Why not use VS Snippets

It's a valid alternative - something tells me a majority of developers prefer to stay in VS. There are a few reasons I went down this alternative path which may or may not resonate with other developers:

1. The built in snippets require a "one by one" approach. You'd need to type in prop A, B, C sequentially. This approach is "batched" in that you set everything up and then generate the code.
2. Snippets involve more typing. At the time I made this tool you'd need to tab through 3 items on a snippet: type, private member, and property name. This uses a convention to allow you to type less and get more faster.
3. Customized snippets tether a person to an installation of Visual Studio (or customizing each one they touch). Because I am constantly developing across several machines and because I don't like tying myself to an instance of Visual Studio, that's another reason I do a lot of webby type tools like this.

> Why not output a CodeDom object
That's an interesting concept. I think I'd be leaving the world of JavaScript but it could be much more flexible in the end. Perhaps an Ajaxy implementation where the CodeDom stuff lived on the server would be apropos.

> dep props are only necessary for the TARGET of a data binding the the DubPeeEff/"web killer"

You're right in the above although as I've been building more UserControls that are templates for displaying data within the context of some larger list bound control (ItemsControl/DataGrid/etc) I see the need cropping up more and more.

> What is the answer to life, the
> universe, and everything?

http://www.youtube.com/watch?v=hkbdP7sq0w8