Code generation of Silverlight DPs

8 views
Skip to first unread message

Colin Eberhardt

unread,
Apr 1, 2009, 12:08:51 PM4/1/09
to WPF Disciples
After much frustration I finally bit-the-bullet. I have created a T4
template (thanks for the tip Mr. Anson), for generating partial
classes with DP declarations:

http://www.scottlogic.co.uk/blog/wpf/2009/04/silverlight-dependency-property-code-generation/

Let me know what you think.

Regards,
Colin E.

Josh Smith

unread,
Apr 1, 2009, 12:25:52 PM4/1/09
to wpf-di...@googlegroups.com
Excellent work, sir!  Very very cool. 

One question...if the auto-generated C# file (ex. RangeControl.cs) is checked into source control, would it be checked out and edited every time you compile the project in which it exists?

Josh

Shawn Wildermuth

unread,
Apr 1, 2009, 1:22:08 PM4/1/09
to wpf-di...@googlegroups.com

The SilverlightContrib project has snippets for DPs and APs (and other Silverlight tasks).  Well worth the investment (since its free).

 

http://silverlightcontrib.codeplex.com

 

Thanks,

 

Shawn Wildermuth

http://wildermuth.com

https://agilitrain.com

Microsoft MVP (C#), MCSD.NET, Author and Speaker

 

The Silverlight Tour is coming to a city near you!

David Anson

unread,
Apr 1, 2009, 1:51:14 PM4/1/09
to wpf-di...@googlegroups.com

Nicely done, Colin!

 

Shawn: The problem I have with snippets is that they're applied once and then never changed again. It's like when folks do copy+paste of big code blocks to implement something new - it becomes a maintainability challenge very quickly. The nice thing about the T4 templates approach is that if you decide to make a change to the way you do your DependencyProperties, you can make it in one place and ALL your code automatically benefits. Plus the size of your code files is kept way down since there's not all this redundant stuff in your face. Something like on-demand code generation seems the right way to go here - and T4 seems to do the job pretty conveniently.

 

PS - Yes, I'll admit it, I'm a fan of C-style macros, too - when used with care... :)

Colin Eberhardt

unread,
Apr 1, 2009, 1:56:03 PM4/1/09
to WPF Disciples
Glad you like it :-)

I am already finding it useful, just applied it to a little project I
am playing with and zapped about 40% of the code!

> One question...if the auto-generated C# file (ex. RangeControl.cs) is
> checked into source control, would it be checked out and edited every time
> you compile the project in which it exists?

As I mentioned I am a T4 newbie, however Oleg recommends checking in
both the T4 template and the generated source:

http://www.olegsych.com/2008/10/t4-tutorial-reusing-code-generators-on-multiple-projects/

However, he does not give a reason why. My guess is that VS only
regenerates the generated code file when the template changes, or at
leas that is what I can deduce from trial and error. It does however
mean that if you change the XML file which describes your SL classes
and DPs you have to manually run the templatees.

You can also generate multiple files from a single template:

http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

This means you could use the XML file format I described and a single
template to generate all the files. The only reason why I did not use
this approach is because it requires that you install the T4 Toolkit.
I wanted to present a method without any pre-requisites so that it was
more accessible.

I might also create a WPF version ...

Regards,
Colin E.

Josh Smith

unread,
Apr 1, 2009, 2:00:04 PM4/1/09
to wpf-di...@googlegroups.com
> I might also create a WPF version ...

Do elaborate please... :)

Corrado Cavalli

unread,
Apr 1, 2009, 2:41:39 PM4/1/09
to wpf-di...@googlegroups.com
Cool, and great T4 tutorial too...

Colin Eberhardt

unread,
Apr 2, 2009, 4:28:16 AM4/2/09
to WPF Disciples


On Apr 1, 7:00 pm, Josh Smith <flappleja...@gmail.com> wrote:
> > I might also create a WPF version ...
>
> Do elaborate please... :)

OK ... well what I was thinking is that Silverlight and WPF are very
similar frameworks. They both have the concept of DPs and ....

Oh. I see what you mean ;-)

I'll get to work Mr. Smith.

BTW. Congrats on the very much deserved MVP.

Colin E.
Reply all
Reply to author
Forward
0 new messages