<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' version='2.0'><channel><atom:id>tag:blogger.com,1999:blog-24760385</atom:id><lastBuildDate>Tue, 26 Aug 2008 08:33:55 +0000</lastBuildDate><title>NET Progress</title><description/><link>http://dotnet.agilekiwi.com/blog/</link><managingEditor>noreply@blogger.com (John Rusk)</managingEditor><generator>Blogger</generator><openSearch:totalResults>35</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-8783795275666879832</guid><pubDate>Tue, 26 Aug 2008 08:14:00 +0000</pubDate><atom:updated>2008-08-26T01:20:08.886-07:00</atom:updated><title>A Practical Step in the Stored Proc Debate</title><description>I've taken a practical step in the long-running debate for and against stored procs.&lt;br /&gt;&lt;br /&gt;I've logged a &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=357997"&gt;formal suggestion&lt;/a&gt; with Microsoft asking them to correct their erroneous advice on the subject. :-)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms978510.aspx"&gt;This&lt;/a&gt; Patterns and Practices page says:&lt;br /&gt;&lt;blockquote&gt;Stored procedures generally result in improved performance because the database can optimize the data access plan used by the procedure and cache it for subsequent reuse.&lt;/blockquote&gt;&lt;br /&gt;That's been factually incorrect for the last decade! In terms of query plan caching, Parameterized SQL and Stored Procs are &lt;a href="http://weblogs.asp.net/fbouma/archive/2003/11/18/38178.aspx"&gt;handled in the same way&lt;/a&gt; - and they have been since SQL Server 7. &lt;br /&gt;&lt;br /&gt;So, if you too are disappointed by the level of erroneous information in this &lt;a href="http://weblogs.asp.net/fbouma/archive/2006/05/26/Yay_2100_-A-new-Stored-Proc-vs.-Dyn.-Sql-battle_2100_.aspx"&gt;long running&lt;/a&gt; debate, head over to the &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=357997"&gt;feedback page&lt;/a&gt; and support my request to fix the P&amp;amp;P page. You can support it via commenting on the page or by using the Validation link at the top. &lt;br /&gt;&lt;br /&gt;Let's hope we can get the page corrected, and achieve a small step for rationality in an often irrational debate :-)&lt;br /&gt;&lt;br /&gt;(Apologies to Microsoft if my tone sounds hash. I know you've already got the right information up on other parts of your site. Here's hoping to see P&amp;amp;P updated too.)</description><link>http://dotnet.agilekiwi.com/blog/2008/08/practical-step-in-stored-proc-debate.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4621056278700105749</guid><pubDate>Fri, 18 Jul 2008 09:11:00 +0000</pubDate><atom:updated>2008-07-18T03:03:35.765-07:00</atom:updated><title>Software Development Meme</title><description>I've been tagged by &lt;a href="http://andrewpeters.net/2008/07/11/when-memes-attack/"&gt;Andrew&lt;/a&gt;, in a software development &lt;a href="http://thedailymeme.com/what-is-a-meme/"&gt;meme&lt;/a&gt; that started way back &lt;a href="http://www.michaeleatonconsulting.com/blog/archive/2008/06/04/how-did-you-get-started-in-software-development.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;How old were you when you started programming?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I was exactly 13.  My parents bought me my first computer for my 13th birthday.  It was a second-hand &lt;a href="http://www.technology.niagarac.on.ca/staff/mcsele/OhioScientific.html"&gt;Ohio Scientific Superboard II&lt;/a&gt;.  &lt;br /&gt;&lt;br /&gt;By the way, the Superboard shipped in 1978 as a single circuit board.  It had keys mounted on the front and chips mounted on the back.  That’s it.  No case, no nothing – just one big circuit board with stuff on it.  By the time it reached me a few years later, mine had been fitted into a hand-built wooden case, and its memory had been expanded to 16&lt;span style="font-style:italic;"&gt;k&lt;/span&gt;.  (I still have the machine.  If anyone knows how to fix a Superboard II that won’t respond to the Break key after power-up, please &lt;a href="http://www.agilekiwi.com/contact.htm"&gt;contact me&lt;/a&gt; :-)  &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;How did you get started in programming?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Before my SuperBoard, I’d only seen two other computers.  I guess they must have made a big impression on me.  I got Mum and Dad to buy me a book on programming in BASIC, and the purchase of the actual computer followed sometime after.  I don’t recall how much nagging was involved ;-)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;What was your first language?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The Superboard’s version of BASIC, which was &lt;a href="http://blogs.sun.com/Drew/entry/has_anyone_else_heard_of"&gt;apparently&lt;/a&gt; "Microsoft 6502 BASIC version 1.0 revision 3.2".  In hindsight, I love the fact that it was simple enough for a 13 year-old to learn the whole language.  If I recall correctly, there was IF .. THEN, but no ELSE; and GOTO but no GOSUB.  Definitely a minimal language.  It made me appreciate "real" languages when I finally got to use them.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;What was the first real program you wrote?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It was a clone of the first computer program I’d ever seen – a game where you had to steer a tank through a minefield.  My version had the randomly-generated minefield, and the tank that was stuck on full-throttle while you steered your way though, but it lacked the system-generated comments that poked fun at your crashes.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;What languages have you used since you started programming?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I’ve done serious development in Pascal, Excel 4.0 Macro language, VBA, Ada, PowerBuilder, PL-SQL, Delphi and C#.  I’ve dabbled in various others including C++, JavaScript and even VB.NET (although it makes my eyes hurt when I read it ;-)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;What was your first professional programming gig?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I did a few small jobs in BASIC and Excel when I was a student, but my first really serious job was in PowerBuilder for an IT consultancy.  PowerBuilder’s (in)famous DataWindow really was a fast way to get things done.  It set the bar for productivity, and it's only now in 2008 that I feel like we've replicated that speed in .NET - thanks to a good ORM and our in-house framework. (And we get a proper domain model with layering, which we never had in PB!)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;If you knew then what you know now, would you have started programming?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Yes.  &lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;If there is one thing you learned along the way that you would tell new developers, what would it be?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I might tell them that it’s not about technology, it’s really about people – but I don’t think they’d believe me.  &lt;br /&gt;&lt;br /&gt;So instead I’d tell them to read &lt;a href="http://samizdat.mines.edu/howto/HowToBeAProgrammer.html"&gt;How to be a Programmer&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;What’s the most fun you’ve ever had... programming?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I’ve particularly enjoyed my old Superboard; writing (most of) a compiler at university; doing some funky stuff with COM in Delphi; and releasing &lt;a href="http://www.codeplex.com/ActiveSharp"&gt;a&lt;/a&gt; &lt;a href="http://www.codeplex.com/Close2Poco"&gt;couple&lt;/a&gt; of open source projects.  &lt;br /&gt;&lt;br /&gt;But the thing I look back on most fondly is a .NET project at &lt;a href="http://www.optimation.co.nz"&gt;Optimation&lt;/a&gt;.  The highlight wasn’t the technology, which was interesting but not amazing; the highlight was the project as a whole.  We had great collaboration with the customer, very much "one team" with great dialogue and innovation, and together we exceeded expectations instead of merely meeting requirements.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Over to you&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.nichesoftware.co.nz/"&gt;Bevan&lt;/a&gt;&lt;br /&gt;&lt;a href="http://www.smartbeanz.net/blog/"&gt;Eduard&lt;/a&gt;&lt;br /&gt;and&lt;br /&gt;&lt;a href="http://blogs.msdn.com/kirillosenkov/"&gt;Kirill&lt;/a&gt;</description><link>http://dotnet.agilekiwi.com/blog/2008/07/software-development-meme.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-817866053730141821</guid><pubDate>Tue, 15 Jul 2008 10:08:00 +0000</pubDate><atom:updated>2008-07-15T03:13:56.166-07:00</atom:updated><title>POCO LINQ Update</title><description>&lt;a href="http://www.codeplex.com/Close2Poco"&gt;Here &lt;/a&gt;is my &lt;a href="http://dotnet.agilekiwi.com/blog/2007/12/interested-in-poco-linq.html"&gt;long-promised&lt;/a&gt; near-POCO solution for LINQ to SQL.&lt;br /&gt;&lt;br /&gt;As planned, it is deliberately not 100% pure POCO, but it does implement these key goals:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Entity classes are simple enough to be hand-written (no need to generate them with SQL Metal or VS 2008)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Mapping is easy (no need to manually map every property via attributes or external XML)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Lazy-loading works&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Bi-directional relationships work (update one end, and the other is synchronized automatically)&lt;/li&gt;&lt;/ol&gt;</description><link>http://dotnet.agilekiwi.com/blog/2008/07/poco-linq-update.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-2258890035993646136</guid><pubDate>Sat, 07 Jun 2008 21:20:00 +0000</pubDate><atom:updated>2008-06-07T14:40:55.449-07:00</atom:updated><title>Another reason why architects should write code</title><description>Johanna Rothman explains why &lt;a href="http://www.jrothman.com/weblog/2006/04/architects-must-write-code.html"&gt;architects should write code&lt;/a&gt;.  I completely agree.  &lt;br /&gt;&lt;br /&gt;Not only does hands-on involvement reduce risks on the project, it also reduces embarassment when talking to your kids ;-)  Here's an actual conversation with my daughter:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;What did you do today Dad?&lt;/span&gt;&lt;br /&gt;I drew some diagrams.  Do you know what a diagram is?&lt;br /&gt;&lt;span style="font-style:italic;"&gt;No&lt;/span&gt;&lt;br /&gt;It's a picture that shows how something works.  I drew some diagrams, then I talked with some people about them.&lt;br /&gt;&lt;span style="font-style:italic;"&gt;So you didn't do any work then?&lt;/span&gt;</description><link>http://dotnet.agilekiwi.com/blog/2008/06/another-reason-why-architects-should.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-736508247811786036</guid><pubDate>Wed, 30 Apr 2008 08:51:00 +0000</pubDate><atom:updated>2008-04-30T02:26:30.744-07:00</atom:updated><title>Only 4 New Features in C# vNext?</title><description>I've always admired the effort that Microsoft put into the design of C#.  It's  difficult to find the right balance between power and flexibility, and on the whole I think they've done an excellent job.&lt;br /&gt;&lt;br /&gt;At the same time, it's been great to see the language grow, particularly with the addition of LINQ.  As Jon Skeet has written recently, C# today is noticably more complicated than it was at version 1.0.  Personally, I don't think that's a bad thing. The C# community has grown up along with the langauage, learning the new features as they have been released.&lt;br /&gt;&lt;br /&gt;I can see this process continuing for some time, with the language continuing to grow.  So I was suprised to see this comment from Microsoft's Mads Torgersen, in response to &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=233329"&gt;some&lt;/a&gt; &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=278510"&gt;suggestions&lt;/a&gt; that I posted at &lt;a href="http://connect.microsoft.com"&gt;Microsoft Connect&lt;/a&gt;:&lt;br /&gt;&lt;blockquote&gt;"We have to do some harsh prioritization, both because of our implementation and testing resources, but also because we need to keep the number of new langauge features at a manageable level - depending on how you count, &lt;span style="font-weight:bold;"&gt;we are adding only four language features to C# this time around&lt;/span&gt;".&lt;/blockquote&gt;We more-or-less know that one will be some form of &lt;a href="http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx"&gt;dynamic lookup&lt;/a&gt;. That just leaves three others.  They might be big "killer" features (like generics and lambda expressions in previous versions) but there's still only &lt;span style="font-style:italic;"&gt;three&lt;/span&gt; of them.&lt;br /&gt;&lt;br /&gt;I don't know what to think. Should we be trilled that Microsoft are continuing their excellent track record of keeping the language simple, or should we be dissapointed that there will be so few improvements?</description><link>http://dotnet.agilekiwi.com/blog/2008/04/only-4-new-features-in-c-vnext.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-1766524731146027945</guid><pubDate>Sun, 24 Feb 2008 05:30:00 +0000</pubDate><atom:updated>2008-04-11T15:00:07.629-07:00</atom:updated><title>Summary of C# vNext Ideas in this Blog</title><description>Here's a summary of the ideas I've suggested in this blog, for future versions of C#:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://dotnet.agilekiwi.com/blog/2007/11/making-extension-methods-safer.html"&gt;Fix the versioning issues&lt;/a&gt; with extension methods&lt;/li&gt;&lt;li&gt;Consider "&lt;a href="http://kirillosenkov.blogspot.com/2007/11/john-rusk-on-extension-interfaces.html"&gt;extension interfaces&lt;/a&gt;" to provide mixin-like behaviour&lt;/li&gt;&lt;li&gt;Also, consider improving the &lt;a href="http://dotnet.agilekiwi.com/blog/2008/01/resolving-extension-methods.html"&gt;resolving&lt;/a&gt; of extension methods&lt;/li&gt;&lt;li&gt;Retain some means of serializing iterator state (even if the underlying iterator implementation is changed) (See comments &lt;a href="http://dotnet.agilekiwi.com/blog/2007/05/implementing-workflow-with-persistent.html"&gt;here&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;Provide an &lt;a href="http://dotnet.agilekiwi.com/blog/2006/06/direct-support-for-symbol-names.html"&gt;infoof operator&lt;/a&gt;, or similar&lt;/li&gt;&lt;li&gt;&lt;a href="http://dotnet.agilekiwi.com/blog/2007/04/quoted-strings.html"&gt;Better handling of @-quoted strings&lt;/a&gt; that contain double quotes&lt;li&gt;&lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/enhanced-automatic-properties.html"&gt;Enhance automatic properties&lt;/a&gt; to retain their concise syntax but to offer greater flexibility to use programmer-defined code. Particularly helpful for &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html"&gt;automatic INotifyPropertyChanged implementation&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;</description><link>http://dotnet.agilekiwi.com/blog/2008/02/summary-of-c-vnext-ideas-in-this-blog.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-7489636117821045320</guid><pubDate>Sun, 24 Feb 2008 04:41:00 +0000</pubDate><atom:updated>2008-03-12T01:04:49.755-07:00</atom:updated><title>Enhanced Automatic Properties</title><description>Recently there's been a lot of blog posts discussing things people would like to see in the next version of C#.  Here's my addition to the wish-list:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;"Automatic properties that let me supply a method body."&lt;/i&gt;  It sounds like a contradiction in terms - but it doesn't have to be.  Instead, it would be a compact syntax for defining properties that use standardised method bodies (where &lt;span style="font-style:italic;"&gt;you&lt;/span&gt; define your own "standard" bodies).&lt;br /&gt;&lt;br /&gt;Some places where it would be useful:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Cases where  many properties follow a standard format.  For instance, all the properties generated by the LINQ designer could be single-line automatic properties.  (Much less verbose than the current approach).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;ORM's and similar products that require properties to be written according to certain conventions.  (E.g. &lt;a href="http://www.mindscape.co.nz/products/LightSpeed/"&gt;Mindscape LightSpeed&lt;/a&gt;)&lt;/li&gt;&lt;/ol&gt;&lt;strong&gt;How would it look?&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Right now, we can write automatic properties like this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Foo { get; set; }&lt;/pre&gt;My suggestion is that the words "get" and "set" could be replaced by the names of methods.  For instance:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; AdvancedFoo { GetValue; SetValue; }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Where the methods "GetValue" and "SetValue" are generic instance methods with these signatures:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// When passed the field, return the property value&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;T GetValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;ref&lt;/span&gt; T field)&lt;br /&gt;{&lt;br /&gt;    &lt;span class="rem"&gt;// ... your own code goes here...&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;       &lt;br /&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;/// When passed the field, by ref, and the new property value, set the field&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;ref&lt;/span&gt; T field, T &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;    &lt;span class="rem"&gt;//...your own code goes here...&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;  Given the above property, "AdvancedFoo", the compiler would compile it as if I had written this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; AdvancedFoo&lt;br /&gt;{&lt;br /&gt;    get { &lt;span class="kwrd"&gt;return&lt;/span&gt; GetValue(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _advancedFoo); }&lt;br /&gt;    set { SetValue( &lt;span class="kwrd"&gt;ref&lt;/span&gt; _advancedFoo, &lt;span class="kwrd"&gt;value&lt;/span&gt;); }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _advancedFoo; &lt;span class="rem"&gt;// compiler-generated backing field&lt;/span&gt;&lt;/pre&gt; Just as in today's automatic properties, the compiler would generate the backing field for me.  The difference is that it reads and writes that backing field via methods which I define.&lt;br /&gt;&lt;br /&gt;This goes a long way towards giving the best of both worlds: the compact syntax of automatic properties, and the flexibility of my own code (plus standardisation - all me properties that are supposed to work the same way &lt;span style="font-style:italic;"&gt;do&lt;/span&gt; work the same way, because they all use the same code).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Questions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Why does the "GetValue" method take the field "by ref"?&lt;/span&gt;  SetValue needs it by ref, but why GetValue?  GetValue takes it by ref simply so that you can use &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html"&gt;this trick&lt;/a&gt; to find out &lt;span style="font-style:italic;"&gt;which&lt;/span&gt; property is being read - just in case you need to know.  This is something you are more likely to need to know when setting, e.g. to raise change notificiations, but maybe some people want to identify the property in GetValue too.  Using the ref param makes that possible.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;What type should the complier use for the backing field?&lt;/span&gt;  Simply put, it should use whatever field type is implied by the field parameters of the "GetValue" and "SetValue" methods.  E.g. an automatic property for a LINQ EntityRef might look like this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;T GetEntity(&lt;span class="kwrd"&gt;ref&lt;/span&gt; EntityRef&amp;lt;T&amp;gt; field) &lt;span class="kwrd"&gt;where&lt;/span&gt; T:&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;br /&gt;{}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; SetEntity(&lt;span class="kwrd"&gt;ref&lt;/span&gt; EntityRef&amp;lt;T&amp;gt; field, T &lt;span class="kwrd"&gt;value&lt;/span&gt;) &lt;span class="kwrd"&gt;where&lt;/span&gt; T:&lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;br /&gt;{}&lt;br /&gt;&lt;/pre&gt; allowing me to write this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Customer Bar{ GetEntity; SetEntity; }&lt;br /&gt;&lt;/pre&gt; and have the compiler compile it as if I had written this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Customer Bar&lt;br /&gt;{&lt;br /&gt;    get { &lt;span class="kwrd"&gt;return&lt;/span&gt; GetEntity(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _bar);}&lt;br /&gt;    set { SetEntity(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _bar, &lt;span class="kwrd"&gt;value&lt;/span&gt;);}&lt;br /&gt;}&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; EntitySet&amp;lt;Customer&amp;gt; _bar;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note that the compiler is able to infer that the backing field should be EntitySet&lt;Customer&gt;, not Customer, based on the signatures of Get/SetEntity.&lt;br /&gt;&lt;br /&gt;&lt;small&gt;(And how would Get/SetEntity actually work?  That's a question for another day ;-)  I do have some (working) prototype code...)&lt;/small&gt;&lt;br /&gt;&lt;br /&gt;What do you think of this idea for enhanced automatic properties?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update 2 March:&lt;/span&gt; posted on MS Connect &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=278125"&gt;here&lt;/a&gt; (in the comments)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update 12 March:&lt;/span&gt; And here &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=332659"&gt;here&lt;/a&gt; as an MS Connect item in its own right.</description><link>http://dotnet.agilekiwi.com/blog/2008/02/enhanced-automatic-properties.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4950512805883917891</guid><pubDate>Thu, 14 Feb 2008 08:43:00 +0000</pubDate><atom:updated>2008-02-14T00:46:39.167-08:00</atom:updated><title>Extension Methods - Feedback to Microsoft</title><description>If you've been following the disussions about &lt;a href="http://dotnet.agilekiwi.com/blog/2007/11/making-extension-methods-safer.html"&gt;extension methods&lt;/a&gt; in this blog, you might be interested to know that I've submitted the issue to Microsoft's feedback site.  &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=328527"&gt;Jump over there&lt;/a&gt; and vote for it, if you'd like to see something done.</description><link>http://dotnet.agilekiwi.com/blog/2008/02/extension-methods-feedback-to-microsoft.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4877283526663654030</guid><pubDate>Fri, 08 Feb 2008 22:58:00 +0000</pubDate><atom:updated>2008-04-11T14:55:10.696-07:00</atom:updated><title>Better Property Change Notification</title><description>&lt;span style="font-weight:bold;"&gt;aka Automatic INotifyPropertyChanged&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Some time ago I developed a new way to do property change notification.  It consists of code which can automatically figure out which property has changed, without you having to tell it.&lt;br /&gt;&lt;br /&gt;Details are &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I've now put together an updated version, and posted it on CodePlex &lt;a href="http://www.codeplex.com/ActiveSharp"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Here's how to use the new version:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Option 1:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here's the easiest option...&lt;br /&gt;&lt;br /&gt;Add this code to your class or base class:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;PropertyChangeHelper _propertyChangeHelper = &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyChangeHelper();&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged&lt;br /&gt;{&lt;br /&gt;    add { _propertyChangeHelper.Add(&lt;span class="kwrd"&gt;value&lt;/span&gt;); }&lt;br /&gt;    remove { _propertyChangeHelper.Remove(&lt;span class="kwrd"&gt;value&lt;/span&gt;); }&lt;br /&gt;}&lt;br /&gt;            &lt;br /&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;ref&lt;/span&gt; T field, T &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;    _propertyChangeHelper.SetValue&lt;br /&gt;                          (&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;ref&lt;/span&gt; field, &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;Then write your property setter methods like this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Foo&lt;br /&gt;{&lt;br /&gt;    get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _foo; }&lt;br /&gt;    set { SetValue(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _foo, &lt;span class="kwrd"&gt;value&lt;/span&gt;); }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Option 2:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you want more control, you can find which property changed like this (and then do whatever you like):&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;ref&lt;/span&gt; T field, T &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    field = &lt;span class="kwrd"&gt;value&lt;/span&gt;;   &lt;span class="rem"&gt;//Actually assign the new value&lt;/span&gt;&lt;br /&gt;    PropertyInfo changedProperty = PropertyMap.GetProperty(&lt;span class="kwrd"&gt;this&lt;/span&gt;, &lt;span class="kwrd"&gt;ref&lt;/span&gt; field);&lt;br /&gt;     &lt;span class="rem"&gt;// do whatever you like, now that you know which prop was changed&lt;/span&gt;&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;Key differences in the new version are:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;No "trial sets".  The old version used to call all your property setters, to figure out which fields they corresponded to.  That could be annoying, because you ended up recieving calls to the set methods when your objects were not necessarily ready for them.  The new version doesn't need to do this.  Instead, it inspects the MSIL of the set methods, to figure out what it needs to know.  (The inspection is a one-off runtime operation, so it won't be a performance problem.)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Fewer contraints on how classes are coded.  The old version required that you nominated a special field as the "reference field", to which all others were compared.  The new version does not require that.  Also, the new version lets you use your own base class (with your own SetValue method, named whatever you want).  Fianlly, you can put your own code in the property setter methods (along with the call to your "SetValue" method), something which could cause problems in the old version&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Helper class for easy implemetation of property change notifications (as above)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Granualar implementation, so that you can call some of the implmentaiton classes, but still write the outer "layer" of the API yourself if you need to&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Lock-free. The new version is threadsafe without explict locks (and without and [ThreadStatic]s). &lt;span style="font-style:italic;"&gt; Update 10 Feb:&lt;/span&gt; This used to rely on a little trick with generics and static fields; but that doesn't play well with inheriance.  It now uses a class I wrote called LazyDictionary, which is basically a dictionary with double-checked locking to ensure that locks are only requested during initialization, and not during the on-going running of the application.  (Didn't want a lock request on every property set call!)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;No separate C++ assembly.  The old version used managed C++ to do stuff that C# cannot.  The new version uses reflection emit, to produce the corresponding routine on the fly, so that the separate assembly is not required.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Finally, I have resurrected the name "ActiveSharp" (which I used for a previous, &lt;a href="http://dotnet.agilekiwi.com/blog/2007/06/no-more-development-on-my-side-project.html"&gt;abandonned project&lt;/a&gt;) for this new version of the code.  I did that for two reasons, firstly the new version borrows code from the old ActiveSharp (particulaly the MSIL inspection); and secondly I liked the old name and couldn't think of a better one ;-)  Just like the previous "ActiveSharp", this new version is a product of my &lt;a href="http://en.wikipedia.org/wiki/Micro_ISV"&gt;microISV&lt;/a&gt;, Tasman Logic Ltd.  (The microISV is not exactly a money-making venture, given that its only product is open-source ;-)</description><link>http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-3344880181427922176</guid><pubDate>Tue, 15 Jan 2008 07:11:00 +0000</pubDate><atom:updated>2008-01-15T00:03:18.241-08:00</atom:updated><title>Resolving Extension Methods</title><description>Paul Stovell (author of &lt;a href="http://paulstovell.net/blog/index.php/why-synclinq-should-matter-to-you/"&gt;SyncLINQ&lt;/a&gt;) has pointed out another issue with Extension Methods.  As he &lt;a href="http://www.paulstovell.net/blog/index.php/interface-methods-versus-extension-methods#comment-3403"&gt;writes&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;The problem is the methods are *resolved* at compile time.&lt;br /&gt;&lt;br /&gt;Suppose you had this:&lt;br /&gt;&lt;br /&gt;void DoStuff(this IFoo foo);&lt;br /&gt;&lt;br /&gt;class FooFactory&lt;br /&gt;IFoo Create()&lt;br /&gt;&lt;br /&gt;If your factory returns an object that provides its own instance implementation of DoStuff, it WILL NOT be invoked - the extension method will instead. This breaks many of the rules around polymorphism and good OO in general...&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;In other words, the compiler &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-more-than-sugar.html"&gt;favours instance methods over extension methods&lt;/a&gt;, &lt;span style="font-style:italic;"&gt;but only if it can "see" the instance method at compile time&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;The reason is simple: currently, resolving an extension method is just a compiler trick, so compile time is the only time when it CAN be done.  &lt;br /&gt;&lt;br /&gt;But Paul's comment got me thinking.  It occured to me that, if my idea about &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;using interfaces&lt;/a&gt; was implemented, then it would be possible to properly solve the problem he described. It would work like this:&lt;br /&gt;&lt;br /&gt;First, to recap: &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;I suggest&lt;/a&gt; that there should be a mapping between extension methods and interfaces.  Think of a set of extension methods as an "extension contract".  You can make calls on the methods in that contact either by calling intstance methods (on objects which implement the interface) or by extension methods (for objects which do not implement the interface). This is to solve the &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-problem.html"&gt;versioning problem&lt;/a&gt;, but it can also be used to solve the problem Paul described.&lt;br /&gt;&lt;br /&gt;How? I'll try to illustrate with an example.  Imagine that our "extension contract" is called IExtensionContract.  It contains two methods, Foo and Bar.  We can call Foo and Bar on objects which &lt;span style="font-style:italic;"&gt;don't&lt;/span&gt; support IExtensionContract, simply by calling them as extesion methods.  But, when we call Foo and Bar on objects which DO support IExtensionContract, we want to call the version that is on the object &lt;i&gt;even if we can't tell that the object supports them at compile time&lt;/i&gt;.  (That's the heart of the issue, right Paul?)&lt;br /&gt;&lt;br /&gt;That could be done if the complier worked like this:&lt;br /&gt;&lt;br /&gt;Given this code to compile&lt;br /&gt;&lt;br /&gt;ISomething s = ...&lt;br /&gt;s.Foo();&lt;br /&gt;&lt;br /&gt;The compiler should compile it as if the programmer had written&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;ISomething = ...&lt;br /&gt;IExtensionContract c = s &lt;span class="kwrd"&gt;as&lt;/span&gt; IExtensionContract;&lt;br /&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt;(c == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;   StaticExtender.Foo(s);  &lt;span class="rem"&gt;// invoke the extension method&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;   c.Foo();                &lt;span class="rem"&gt;// invoke the instance method&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;This would be possible, if the &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;interface-based versioning solution&lt;/a&gt; was adopted, because that solution introduces just enough &lt;span style="font-style:italic;"&gt;runtime&lt;/span&gt; information about the extension methods - namely the "contract" to which the methods belong.  Contrast that to the current situation, where there is &lt;span style="font-style:italic;"&gt;no&lt;/span&gt; runtime information about extension methods.&lt;br /&gt;&lt;br /&gt;Here is the user-authored (i.e. not compiler-generated) code for the example above:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; StaticExtender : IExtensionContract &lt;br /&gt;    &lt;span class="rem"&gt;// "inheriting" from interface here is my suggested addition to the language&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Foo(&lt;span class="kwrd"&gt;this&lt;/span&gt; ISomething s){...}&lt;br /&gt;&lt;br /&gt;     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Bar(&lt;span class="kwrd"&gt;this&lt;/span&gt; ISomething s){...}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IExtensionContract&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;void&lt;/span&gt; Foo();&lt;br /&gt;   &lt;span class="kwrd"&gt;void&lt;/span&gt; Bar();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Finally, all the suggested logic I have blogged about previously still applies. The compiler still needs to decide, at compile time, whether the call is to a method in the extension contract or to a like-named (but otherwise unrelated) instance method.  All that logic still applies as I described it &lt;a href="http://dotnet.agilekiwi.com/blog/2007/11/making-extension-methods-safer.html"&gt;here&lt;/a&gt;.</description><link>http://dotnet.agilekiwi.com/blog/2008/01/resolving-extension-methods.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-2370804097451603149</guid><pubDate>Sun, 13 Jan 2008 00:59:00 +0000</pubDate><atom:updated>2008-01-12T18:56:00.809-08:00</atom:updated><title>Readability in Validation</title><description>My favourite pattern for validation code is to have one method per validation rule.  I think it makes it easier to read and manage the rules.&lt;br /&gt;&lt;br /&gt;For instance, I like to be able to write something like this:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;[Validate]&lt;br /&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; EndDateMustBeAfterStartDate()&lt;br /&gt;{&lt;br /&gt;  &lt;span class="kwrd"&gt;return&lt;/span&gt; EndDate &amp;gt; StartDate;&lt;br /&gt;}&lt;/pre&gt;I like to refine the pattern a little, in order to: &lt;br /&gt;&lt;br /&gt;(a) Construct error messages based on the method names.  That makes the code more concise and encourages good method names.  E.g. the failure message from the rule above will be "End date must be after start date".  &lt;br /&gt;(b) Allow you to, alternatively, specify dynamically-generated failure messges from within the method body (see below)&lt;br /&gt;(c) Easily specify if the rule is not applicable in a particular situation (again see below).&lt;br /&gt;&lt;br /&gt;I've attached some &lt;a href="http://www.agilekiwi.com/dotnet/SimpleValidation.cs"&gt;sample code&lt;/a&gt; (with &lt;a href="http://www.agilekiwi.com/dotnet/SimpleValidationTests.cs"&gt;tests&lt;/a&gt;).  &lt;br /&gt;&lt;br /&gt;By the way, I &lt;a href="http://dotnet.agilekiwi.com/blog/2007/06/no-more-development-on-my-side-project.html"&gt;once&lt;/a&gt; wrote some code to fire rules automatically, whenever a property used by the rule was changed.  But that's not included here.  In this sample, you have to ask the system explictly to run the rules at a particular time. &lt;br /&gt;&lt;br /&gt;Here are some example rules, and the code to call them:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Animal&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; AnimalName&lt;br /&gt;    {&lt;br /&gt;        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _animalName; }&lt;br /&gt;        set { _animalName = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; NumberOfLegs&lt;br /&gt;    {&lt;br /&gt;       get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _numberOfLegs; }&lt;br /&gt;       set { _numberOfLegs = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Validate]&lt;br /&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; Result NumberOfLegsMustBeEven()  &lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; NumberOfLegs % 2 == 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Validate]&lt;br /&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; Result MythicalSpeciesAreNotAllowed()&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (AnimalName == &lt;span class="str"&gt;"Unicorn"&lt;/span&gt; || AnimalName == &lt;span class="str"&gt;"Dragon"&lt;/span&gt;)&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; Validation.Error(&lt;span class="str"&gt;"'"&lt;/span&gt; + AnimalName + &lt;span class="str"&gt;"' is not a real species"&lt;/span&gt;);&lt;br /&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; Validation.NotApplicable;&lt;br /&gt;    }  &lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Note the use of the OR operator here to give a compact syntax for returning a custom error&lt;/span&gt;&lt;br /&gt;    [Validate]&lt;br /&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; Result NumberOfLegsMustBePlausible()&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; NumberOfLegs &amp;lt;= 10 || Validation.Error(&lt;span class="str"&gt;"Your '"&lt;/span&gt; + AnimalName +  &lt;span class="str"&gt;"' has too many legs"&lt;/span&gt;);&lt;br /&gt;    } &lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;span class="rem"&gt;// execute like this&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(Result r &lt;span class="kwrd"&gt;in&lt;/span&gt; Validator&amp;lt;Animal&amp;gt;.ExecuteRulesOn(myAnimal))&lt;br /&gt;     &lt;span class="kwrd"&gt;if&lt;/span&gt;(!r.Pass)&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;// or, like this, if you don't want to use the generic Validator class&lt;/span&gt;&lt;br /&gt;Validator v = Validator.GetInstanceFor(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Animal));&lt;br /&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Result r &lt;span class="kwrd"&gt;in&lt;/span&gt; v.ExecuteRulesOn(myAnimal))&lt;br /&gt;  ...&lt;/pre&gt;</description><link>http://dotnet.agilekiwi.com/blog/2008/01/readability-in-validation.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-7336175904973793108</guid><pubDate>Sat, 29 Dec 2007 08:43:00 +0000</pubDate><atom:updated>2008-07-15T03:16:33.252-07:00</atom:updated><title>Interested in POCO LINQ?</title><description>I've been investigating using LINQ to SQL with a POCO approach.  (&lt;a href="http://en.wikipedia.org/wiki/POCO"&gt;POCO&lt;/a&gt; = Plain Old CLR Objects.)  My ideal solution would be something like this:&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Entity classes are simple enough to be hand-written (no need to generate them with SQL Metal or VS 2008)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Mapping is easy (no need to manually map every property via attributes or external XML)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Lazy-loading works&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Bi-directional relationships work (update one end, and the other is synchronized automatically)&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;The last two points are the most problematical. In LINQ to SQL lazy loading requires the use of EntitySet and EntityRef.  However, it's not easy to use those classes in hand-written code.  In fact, it is generally recommended to &lt;a href="http://blogs.msdn.com/digital_ruminations/archive/2007/08/28/linq-to-sql-poco-support.aspx"&gt;dispense with these classes&lt;/a&gt; if you want to "go POCO".  But that deprives you of lazy loading.  You &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/06/21/linq-to-sql-objects-all-the-way-down.aspx"&gt;also&lt;/a&gt; lose out on Linq’s support for bi-directional relationships, because it is also based on EntitySet and EntityRef.&lt;br /&gt;&lt;br /&gt;I want a solution that supports all four goals outlined above. [&lt;strong&gt;Update&lt;/strong&gt;: &lt;a href="http://www.codeplex.com/Close2Poco"&gt;here&lt;/a&gt; it is]&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Question:&lt;/span&gt; If I can find one, would you be interested in it?  &lt;br /&gt;&lt;br /&gt;If enough people say yes (either in comments below or via email), I’ll try to post something within the next few weeks.  &lt;br /&gt;&lt;br /&gt;I understand that knowledgable people have said this can't be done.  I respectfully disagree ;-)  Based on work so far, I’m satisfied that it &lt;span style="font-style:italic;"&gt;can&lt;/span&gt; be done.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update:&lt;/span&gt; The idea would be that you'd hand-write .NET classes, according to a few simple conventions. Do that and you'd automatically get working bidirectional relationships and lazy loading, plus automatic runtime generation of the correct mappings. (You'd never use SQL Metal or the Visual LINQ designer.)  &lt;br /&gt;&lt;br /&gt;You could also &lt;span style="font-style:italic;"&gt;generate&lt;/span&gt; the database &lt;span style="font-style:italic;"&gt;if you want&lt;/span&gt;. (I.e. the usual model is generate-code-from-database; this would allow (but not require) generate-database-from-code). If you use TDD, you could create and test the objects using TDD (running in-memory tests) and &lt;span style="font-style:italic;"&gt;then&lt;/span&gt; generate the database. &lt;br /&gt;&lt;br /&gt;Other simple little benefits would be: e.g. could have one file per class (which you can't with MS's generators); could put comments on the properties of those classes (which you can't with MS's generators, as far as I can tell), could put your own &lt;span style="font-style:italic;"&gt;attributes&lt;/span&gt; on the entity/domain object classes, e.g. for attribute-based validation frameworks (again, you can't with MS's generators), entity classes compact enough to actually &lt;span style="font-style:italic;"&gt;read&lt;/span&gt; them ;-)&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Footnote: In this post I’m talking specifically about LINQ to SQL.  I know that there are (or soon will be) several fine independent ORMs that use the LINQ language features to create their queries, but execute those queries with their own backend. A number of these products support POCOs but, for my current project, I need a solution for the full Microsoft "stack" - including LINQ to SQL.&lt;/span&gt;</description><link>http://dotnet.agilekiwi.com/blog/2007/12/interested-in-poco-linq.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-2652371036824461455</guid><pubDate>Fri, 21 Dec 2007 07:19:00 +0000</pubDate><atom:updated>2007-12-20T23:24:14.807-08:00</atom:updated><title>Update on Persistent Iterators</title><description>Some time ago I wrote about &lt;a href="http://dotnet.agilekiwi.com/blog/2007/05/implementing-workflow-with-persistent.html"&gt;implementing workflow with persistent iterators&lt;/a&gt;.  This is just a reminder (in case you missed it in the comments on the original post) that Microsoft are going to change the implementation details.  Until they announce details of the change, we can't be sure whether the technique I posted will work in future versions of C#.  &lt;br /&gt;&lt;br /&gt;Mads Torgersen, the C# Language PM, wrote:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;We are likely to in fact change the way iterators are implemented in future versions. That said, we might be able to implement a serialization format that would work across such future changes. I agree with you that this would be a very useful feature.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;If you want to use this technique in future versions, head over to &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=278510"&gt;this page&lt;/a&gt; and vote for it.</description><link>http://dotnet.agilekiwi.com/blog/2007/12/update-on-persistent-iterators.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-7277822061719432713</guid><pubDate>Sat, 17 Nov 2007 07:59:00 +0000</pubDate><atom:updated>2008-01-14T23:49:18.271-08:00</atom:updated><title>Making Extension Methods Safer</title><description>(Note: This post refers to my previous posts about extension methods. It won't make much sense unless you read &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-problem.html"&gt;those&lt;/a&gt; &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;posts&lt;/a&gt; first).&lt;br /&gt;&lt;br /&gt;Last year I blogged about extension methods, explaining a &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-problem.html"&gt;risk&lt;/a&gt; and suggesting a &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;solution&lt;/a&gt;.  Now that Visual Studio 2008 is (almost) released, without any solution to the problem, I'm wondering if a solution could be added in a future release.&lt;br /&gt;&lt;br /&gt;Yes, it could.&lt;br /&gt;&lt;br /&gt;It would be as simple as this:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Get the compiler to raise warnings when an instance method takes precedence over an extension method. (This would apply to all extension methods, whether they are in extension interfaces or not).&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Allow the warning to be dealt with in these ways.  Either:&lt;br /&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;(a) Suppress the warning using #pragma warning or the IDE's list of warnings to supress.  If you do this, you get the existing Visual Studio 2008 behaviour. OR&lt;br /&gt;&lt;/li&gt;&lt;li&gt;(b) Make the intent of the code explict at the call site.  If you want the extension method, call it explicitly as a static method; if you want the instance method, call it explicitly with a syntax such as the hypothetical one I proposed &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-solution.html"&gt;here&lt;/a&gt;.  OR&lt;/li&gt;&lt;li&gt;(c) Let the complier supress the warning for you automatically.  To do so, put all the extension methods into an extension interface and make the instance implement the same interface. Then, the compiler can &lt;span style="font-style:italic;"&gt;safely&lt;/span&gt; choose the instance method without issuing a warning.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;For instance, in Visual Studio 2010 (or whenever it may be), Microsoft could safely put all the LINQ extension &lt;span style="font-style:italic;"&gt;methods&lt;/span&gt; into extension &lt;span style="font-style:italic;"&gt;interfaces&lt;/span&gt;. Users who don't care about this stuff could simply ignore or suppress the warning (option a), ending up with exactly the same behaviour they have now.  &lt;br /&gt;&lt;br /&gt;Users who do care could choose option (c), simply by implementing Microsoft's new interfaces on their own classes.  That's easy, because the users already have the methods that are in the interface (that's why they are getting the warning!) so all they need to do is add the interface name to their class's list of ancestors.  &lt;br /&gt;&lt;br /&gt;The user is then left with warnings for only those rare cases of genuine ambiguity, cases where the semantics/purpose of the instance method differs from that of the extension.  That's exactly when a warning is &lt;a href="http://dotnet.agilekiwi.com/blog/2006/04/extension-methods-problem.html"&gt;needed&lt;/a&gt;.  These rare cases would be addressed with option (b).&lt;br /&gt;&lt;br /&gt;In conclusion, Microsoft &lt;span style="font-style:italic;"&gt;could&lt;/span&gt; safely fix the extension method problem in future version of Visual Studio.&lt;br /&gt;&lt;br /&gt;(Update: &lt;a href="http://dotnet.agilekiwi.com/blog/2008/01/resolving-extension-methods.html"&gt;another reason&lt;/a&gt; to adopt this solution)</description><link>http://dotnet.agilekiwi.com/blog/2007/11/making-extension-methods-safer.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-7451711472853951142</guid><pubDate>Fri, 02 Nov 2007 09:25:00 +0000</pubDate><atom:updated>2007-11-02T02:42:05.426-07:00</atom:updated><title>SmartRegex</title><description>Jeff Attwood blogged about his &lt;a href="http://www.codinghorror.com/blog/archives/000989.html"&gt;dislike of fluent interfaces&lt;/a&gt;.  He happened to use Joshua Flanagan's fluent regex API as his example.&lt;br /&gt;&lt;br /&gt;When Joshua first posted his example, last year, it inspired me write &lt;a href="http://dotnet.agilekiwi.com/blog/2006/10/shorthand-interfaces.html"&gt;a more compact alternative&lt;/a&gt;. While the fluent style began in the Java community, the compact alternative uses the unique features of C# to deliver an easy-to-read result - without the verbosity of the fluent style.&lt;br /&gt;&lt;br /&gt;Thanks to a little &lt;a href="http://dotnet.agilekiwi.com/blog/2006/10/shorthand-interfaces.html#comments"&gt;encouragement from Joshua&lt;/a&gt;, today I am posting the source code. I've called it "SmartRegex". Here's the &lt;a href="http://www.agilekiwi.com/dotnet/SmartRegex.cs"&gt;source code&lt;/a&gt;, and here are the &lt;a href="http://www.agilekiwi.com/dotnet/SmartRegexTests.cs"&gt;unit tests&lt;/a&gt;.  The last of the tests is a full test of the exact example used by Joshua and Jeff.&lt;br /&gt;&lt;br /&gt;Note that this version of SmartRegex contains just enough to implement Joshua's example.  It does not support the rest of the regex syntax.  If you are interested in a more complete example, email me or leave a comment below and I'll see what I can do.</description><link>http://dotnet.agilekiwi.com/blog/2007/11/smartregex.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-1277863162872557471</guid><pubDate>Sun, 02 Sep 2007 05:45:00 +0000</pubDate><atom:updated>2008-08-26T01:33:55.556-07:00</atom:updated><title>Query Errors and Identity Map</title><description>I’ve encountered a “gotcha” in O/R Mapping. I’ll describe it here, and outline some of the possible solutions. To make matters worse, the default solution in nHibernate has side effects which can cause problems of their own. I’ll cover those side effects too.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Problem&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Imagine doing this in one &lt;a href="http://www.martinfowler.com/eaaCatalog/unitOfWork.html"&gt;Unit of Work&lt;/a&gt; (aka Session in Hibernate-speak):&lt;br /&gt;&lt;br /&gt;1. Load customer “Bob Smith”&lt;br /&gt;2. Change his surname to “Jones” (but don’t save yet)&lt;br /&gt;3. Now, ask the ORM to load all customers named “Smith”.&lt;br /&gt;&lt;br /&gt;Depending on how your ORM works, the list of Smiths may include one guy named Jones!&lt;br /&gt;&lt;br /&gt;Why? Because he is still “Bob &lt;i&gt;Smith&lt;/i&gt;” in the database. The ORM grabs the database row for Bob Smith, sees that the corresponding object has been loaded already (via the &lt;a href="http://www.martinfowler.com/eaaCatalog/identityMap.html"&gt;Identity Map&lt;/a&gt; pattern), and so returns the already-loaded object – even though that object is now named Jones.&lt;br /&gt;&lt;br /&gt;There are several possible solutions to this problem, as follows:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. Automatic “eager updates”&lt;/strong&gt;. This is the default solution used in Hibernate and nHibernate. When FlushMode is set to its default value (Auto), nHibernate will send updates to the database early (i.e. before you ask it to) just to make sure that your in-memory changes also exist in the database, and therefore are reflected in the results of your queries. In the example above, nHiberate would automatically save Jones before querying for the list of Smith’s. This solution guarantees accurate results, but it has a problem (which I’ll describe below).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. Cross check database results with in-memory state&lt;/strong&gt;. This solution would involve scanning all objects already in the identity map, in addition to running each database query. Due to the obvious performance implications, particularly if there are a lot of objects in the identity map, I won’t discuss this option further. &lt;em&gt;[See comments below for discussion of performance implications]&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;3. “Don’t worry about it”.&lt;/strong&gt; Simply don’t solve the problem at all. Go ahead and return Jones in the list of Smiths. Rely on the application programmer to know when this might be dangerous. In cases where the programmer thinks this will cause a problem, the programmer can either:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;strong&gt;3.a. Explicitly save changed objects before running the problematic query&lt;/strong&gt; – this is a manual version of option 1 above, but it only kicks in when the programmer thinks they need it. I believe this is the approach taken by LINQ-to-SQL.(*)&lt;br /&gt;&lt;strong&gt;3.b. Explicitly run two queries&lt;/strong&gt;: the first runs against the database and loads the objects into the unit of work (aka session). The second runs in memory, scanning over &lt;i&gt;all&lt;/i&gt; the objects in the unit of work, returning those that match. Only results from the memory query are actually used. In our example above, Jones would be processed by the first (database) query, but not by the second (memory) one. I’m not aware of any ORM that implements this approach(**)&lt;/blockquote&gt;&lt;strong&gt;4. Solve the worst half of the problem...&lt;/strong&gt; There are actually several types of error that may occur. Arguably, the worst type of error is the one I showed above. Say you query for all "Smith"s, and the proceed to perform some operation on each object returned, an operation which is only valid for people named Smith. It's not valid for people named Jones, so you may execute it in error when the ORM returns a Jones mixed in with the Smiths. This problem can be solved by a variant of option 2: query the database, let the identity map do its thing, and then execute the criteria again in memory &lt;i&gt;on the final result set&lt;/i&gt;. Strip out all objects that fail the in-memory test. This option performs better than 2 because it doesn't scan &lt;i&gt;all&lt;/i&gt; objects in memory, just those that were returned by the database query &lt;i&gt;and&lt;/i&gt; were already in the identity map. (i.e. just Jones, in the example above). This approach guarantees to &lt;i&gt;exclude all bad data&lt;/i&gt;, but it does not guarantee to &lt;i&gt;include all good data.&lt;/i&gt; (E.g. it won't include new Smiths which have not yet been saved.) I implemented this option in &lt;a href="http://dotnet.agilekiwi.com/blog/2007/06/no-more-development-on-my-side-project.html"&gt;ActiveSharp.&lt;/a&gt; &lt;em&gt;[See comments below for discussion of why this is worth doing and why, unlike option 2, it can be implemented efficiently]&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;The Problem with the nHibernate Approach&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Let’s introduce the problem with a quote from the &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/transactions.html#transactions-optimistic"&gt;nHibernate documentation&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;i&gt;Many business processes require a whole series of interactions with the user interleaved with database accesses…[This is called] a long running application transaction. A single application transaction usually spans several database transactions. &lt;strong&gt;It will be atomic if only one of these database transactions (the last one) stores the updated data, all others simply read data&lt;/strong&gt;.&lt;/i&gt;&lt;/blockquote&gt;But, when automatic flushing is enabled, &lt;i&gt;any&lt;/i&gt; transaction may update data! Even if you roll back the last one (the one which is &lt;i&gt;supposed&lt;/i&gt; to do all the updates) Hibernate may have already committed some “eager" updates in earlier transactions. Therefore, automatic flushing is incompatible with long application transactions.&lt;br /&gt;&lt;br /&gt;In summary, you have two choices in nHibernate:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;(a) Automatic flushe&lt;/strong&gt;&lt;strong&gt;s&lt;/strong&gt;, usually in the session-per-request pattern.&lt;br /&gt;&lt;strong&gt;Pro:&lt;/strong&gt; accurate query results;&lt;br /&gt;&lt;strong&gt;Con:&lt;/strong&gt; must use one database transaction for entire session (to prevent unwanted commits of automatic flushes)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;(b) Manual flushes&lt;/strong&gt;, usually in the session-per-conversation pattern (aka session-per-application-transaction).&lt;br /&gt;&lt;strong&gt;Pro:&lt;/strong&gt; can use multiple database transactions per session, flushing only in the last one;&lt;br /&gt;&lt;strong&gt;Con:&lt;/strong&gt; query accuracy may suffer as described above.&lt;br /&gt;&lt;br /&gt;I am disappointed to see nHiberate documentation which says that “&lt;i&gt;the ISession.Find(..) methods will never return stale data; nor will they return the wrong data&lt;/i&gt;”, without specifically spelling out that this is only true with automatic flushes. Likewise I am disappointed to see the session-per-conversation pattern described, with a perfectly valid recommendation to turn off automatic flushes, but with no mention of the adverse effect on query accuracy.&lt;br /&gt;&lt;br /&gt;[&lt;span style="font-weight:bold;"&gt;Update 26 Aug 08&lt;/span&gt;: Looks like nHibernate 2 has tightened things up somewhat, since &lt;a href="http://www.infoq.com/news/2008/08/nhibernate2-arrived"&gt;it has&lt;/a&gt; "No AutoFlush outside a transaction - Database transactions are never optional, all communication with a database has to occur inside a transaction, no matter if you read or write data."  So at least if it is going to send your updates to the database, it will be in a transaction that you have created yourself.  Still no guarantee (as far as I can tell) that the transaction used for the updates will be the "last" one - i.e. the one where you intend all updates to happen]&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;With manual flushing, accuracy of query results is a potential problem. Admittedly, it will be a rare problem in practice, but sometimes the rare problems are the most dangerous -- because they’re the hardest to find in testing.  &lt;br /&gt;&lt;br /&gt;I can’t find much about the problem on Google. Perhaps I’m not searching well enough… or perhaps the information is out there but I can’t find it because Martin Fowler hasn’t given it a catchy name yet ;-) …&lt;br /&gt;&lt;br /&gt;Certainly, I agree with the usual recommendation to make sessions as short as practically possible. That may minimize the problem – but it won’t 100% eliminate it, particularly for application transactions that span multiple database transactions.&lt;br /&gt;&lt;br /&gt;I think it’s important to understand when an ORM may give “incorrect” results. It’s also important to understand the side effects of Hibernate’s eager-update solution.&lt;br /&gt;&lt;br /&gt;Finally, I’m relatively new to nHibernate. If I’ve missed something important, please let me know.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;br /&gt;Hibernate transaction patterns:&lt;br /&gt;&lt;a href="http://www.hibernate.org/hib_docs/reference/en/html/transactions.html"&gt;http://www.hibernate.org/hib_docs/reference/en/html/transactions.html&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;Stale data in LINQ-to-SQL:&lt;br /&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2078814&amp;amp;SiteID=1&amp;amp;mode=1"&gt;http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2078814&amp;amp;SiteID=1&amp;amp;mode=1&lt;br /&gt;&lt;/a&gt;(at the time of writing this post, my own question in the thread has not been answered)&lt;br /&gt;&lt;br /&gt;Another post on LINQ issues, which mentions the stale data issue:&lt;br /&gt;&lt;a href="http://damieng.com/blog/2007/05/16/linq-to-sql-details-issues-and-patterns"&gt;http://damieng.com/blog/2007/05/16/linq-to-sql-details-issues-and-patterns&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Uniquing (aka Identity Map):&lt;br /&gt;&lt;a href="http://www.ayende.com/Blog/archive/2006/09/03/7294.aspx"&gt;http://www.ayende.com/Blog/archive/2006/09/03/7294.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;(*)The programmer will generally need to make sure that the explict "early" save is in the same database transaction as the rest of the intended updates. This is for the same reasons outlined above with regard to automatically-flushed updates in Hibernate.&lt;br /&gt;&lt;br /&gt;(**)This option is achievable, because with LINQ, it is relatively easy to run the same query once against the database and once against the objects in memory. But, it is basically just a manual version of option 2 above, but it only kicks in when the programmer thinks they need it.&lt;br /&gt;Importantly, to be truly accurate, options 2 and 3b must include &lt;i&gt;new&lt;/i&gt; objects that have been added to the unit of work but not yet saved to the database. This is not possible in most (all?) true &lt;a href="http://www.ayende.com/Blog/archive/2007/03/20/Plain-old-.Net-classes.aspx"&gt;POCO&lt;/a&gt; ORMs, since their &lt;a href="http://www.hibernate.org/hib_docs/nhibernate/html/manipulatingdata.html"&gt;persistence-by-reachability&lt;/a&gt; code only kicks in when objects are saved. That’s too late for options 2 and 3b, which require the ORM to know about objects &lt;i&gt;a&lt;/i&gt;&lt;i&gt;s soon as&lt;/i&gt; they become reachable from any other object in the Unit of Work, even if they have not been saved yet.</description><link>http://dotnet.agilekiwi.com/blog/2007/09/errors-from-identity-map.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-5502131385806402904</guid><pubDate>Sun, 19 Aug 2007 02:48:00 +0000</pubDate><atom:updated>2008-02-14T11:01:17.520-08:00</atom:updated><title>Workarounds to Simulate Static Imports</title><description>Sometimes, it would be handy to write methods without preceeding them with an object or type.  E.g. it would be nice to write&lt;br /&gt;&lt;br /&gt;    Sin(x) / Cos(x)&lt;br /&gt;&lt;br /&gt;instead of&lt;br /&gt;&lt;br /&gt;    Math.Sin(x) / Math.Cos(x)&lt;br /&gt;&lt;br /&gt;The shorter version is  not  possible in C#, so this post outlines all the workarounds I know of.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;In Java, and some other languages, you can omit "Math." by doing a "static import" of the "Math" class.  I asked Eric Gunnerson (ex C# Progamme Manager at MS)  why static imports are not allowed in C#.  He responded &lt;a href="http://blogs.msdn.com/ericgu/archive/2004/06/30/169861.aspx"&gt;here&lt;/a&gt;, and I paraphrased his response as follows:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span&gt;"Regardless of whether you see&lt;br /&gt;&lt;br /&gt;&gt; ...Color.Red&lt;br /&gt;or&lt;br /&gt;&gt;...System.Drawing.Color.Red&lt;br /&gt;&lt;br /&gt;you always know that Red is something defined outside of the class you're current reading.&lt;br /&gt;&lt;br /&gt;Compare that to&lt;br /&gt;&lt;br /&gt;&gt;Math.Sin(x)&lt;br /&gt;versus&lt;br /&gt;&gt;Sin(x)&lt;br /&gt;&lt;br /&gt;In the latter you have lost the clear hint that Sin is "&lt;span&gt;outside the current class&lt;/span&gt;". It could be a method on the class you're reading, or it might not. So static class imports create a bit more ambiguity than namespace imports, which I think was Eric's point."&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Nevertheless, something like static imports can be useful for &lt;a href="http://dotnet.agilekiwi.com/blog/2006/10/shorthand-interfaces.html"&gt;DSL-like solutions&lt;/a&gt;.  Anders Noras gives a good example (and an interesting discussion) &lt;a href="http://andersnoras.com/blogs/anoras/archive/2007/07/15/is-it-a-bird-is-it-a-plane-it-s-a-dsl.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;His sample Java code looks like this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;Dim&lt;/span&gt; doc &lt;span class="kwrd"&gt;As&lt;/span&gt; XmlDocument&lt;br /&gt;doc = Document( _&lt;br /&gt;   Element(&lt;span class="str"&gt;"Employees"&lt;/span&gt;, _&lt;br /&gt;       Element(&lt;span class="str"&gt;"Employee"&lt;/span&gt;), _&lt;br /&gt;           Comment(&lt;span class="str"&gt;"Highly valued employee..."&lt;/span&gt;), _&lt;br /&gt;           Element(&lt;span class="str"&gt;"Id"&lt;/span&gt;, 42), _&lt;br /&gt;           Element(&lt;span class="str"&gt;"Name"&lt;/span&gt;, &lt;span class="str"&gt;"Arthur Dent"&lt;/span&gt;), _&lt;br /&gt;           Element(&lt;span class="str"&gt;"ContactInfo"&lt;/span&gt;, _&lt;br /&gt;               Element(&lt;span class="str"&gt;"PhoneNumber"&lt;/span&gt;, &lt;span class="str"&gt;"555-5555"&lt;/span&gt;), _&lt;br /&gt;               Element(&lt;span class="str"&gt;"Address"&lt;/span&gt;, &lt;span class="str"&gt;"Upda road 42"&lt;/span&gt;), _&lt;br /&gt;               Element(&lt;span class="str"&gt;"Country"&lt;/span&gt;, &lt;span class="str"&gt;"England"&lt;/span&gt;))))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here are all the ways I know of to simulate that kind of API in C#:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. Simple ugly static methods&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;XmlDocument doc = DocHelper.Document(&lt;br /&gt;   DocHelper.Element(&lt;span class="str"&gt;"Employees"&lt;/span&gt;,&lt;br /&gt;       DocHelper.Element(&lt;span class="str"&gt;"Employee"&lt;/span&gt;), _&lt;br /&gt;           DocHelper.Comment(&lt;span class="str"&gt;"Highly valued employee..."&lt;/span&gt;),&lt;br /&gt;           DocHelper.Element(&lt;span class="str"&gt;"Id"&lt;/span&gt;, 42),&lt;br /&gt;           DocHelper.Element(&lt;span class="str"&gt;"Name"&lt;/span&gt;, &lt;span class="str"&gt;"Arthur Dent"&lt;/span&gt;),&lt;br /&gt;           DocHelper.Element(&lt;span class="str"&gt;"ContactInfo"&lt;/span&gt;,&lt;br /&gt;               DocHelper.Element(&lt;span class="str"&gt;"PhoneNumber"&lt;/span&gt;, &lt;span class="str"&gt;"555-5555"&lt;/span&gt;),&lt;br /&gt;               DocHelper.Element(&lt;span class="str"&gt;"Address"&lt;/span&gt;, &lt;span class="str"&gt;"Upda road 42"&lt;/span&gt;),&lt;br /&gt;               DocHelper.Element(&lt;span class="str"&gt;"Country"&lt;/span&gt;, &lt;span class="str"&gt;"England"&lt;/span&gt;))));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Ugly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. Make the thing before the dot look nicer.&lt;br /&gt;&lt;br /&gt;2a. Creative naming of static classes&lt;/strong&gt;&lt;br /&gt;Sometimes you can do this with creative naming of static classes.  For instance, instead of writing "CommonFunctions.EnsureNotMissing(...)" name the static class "Ensure" and write "Ensure.NotMissing(...)".&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2b. Use instance methods, so you just have a single letter before the dot:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;DocHelper a = &lt;span class="kwrd"&gt;new&lt;/span&gt; DocHelper();&lt;br /&gt;XmlDocument doc = a.Document(&lt;br /&gt;   a.Element(&lt;span class="str"&gt;"Employees"&lt;/span&gt;,&lt;br /&gt;       a.Element(&lt;span class="str"&gt;"Employee"&lt;/span&gt;), _&lt;br /&gt;           a.Comment(&lt;span class="str"&gt;"Highly valued employee..."&lt;/span&gt;),&lt;br /&gt;           a.Element(&lt;span class="str"&gt;"Id"&lt;/span&gt;, 42),&lt;br /&gt;           a.Element(&lt;span class="str"&gt;"Name"&lt;/span&gt;, &lt;span class="str"&gt;"Arthur Dent"&lt;/span&gt;),&lt;br /&gt;           a.Element(&lt;span class="str"&gt;"ContactInfo"&lt;/span&gt;,&lt;br /&gt;               a.Element(&lt;span class="str"&gt;"PhoneNumber"&lt;/span&gt;, &lt;span class="str"&gt;"555-5555"&lt;/span&gt;),&lt;br /&gt;               a.Element(&lt;span class="str"&gt;"Address"&lt;/span&gt;, &lt;span class="str"&gt;"Upda road 42"&lt;/span&gt;),&lt;br /&gt;               a.Element(&lt;span class="str"&gt;"Country"&lt;/span&gt;, &lt;span class="str"&gt;"England"&lt;/span&gt;))));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;3. Use constructors instead.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;In this option, you write "new Element(...)" instead of "a.Element(...)"&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;4. Chain methods together "fluent" style&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The downside of this is that you have to change your methods to return objects that support the next methods that you may call.  While clever, this adds a level of complexity and so should be used with care.  In general, &lt;a href="http://dotnet.agilekiwi.com/blog/2007/11/smartregex.html"&gt;I think&lt;/a&gt; there are better options.&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;XmlDocument doc = DocHelper.Document()&lt;br /&gt;   .Element(&lt;span class="str"&gt;"Employees"&lt;/span&gt;,&lt;br /&gt;       .Element(&lt;span class="str"&gt;"Employee"&lt;/span&gt;), _&lt;br /&gt;           .Comment(&lt;span class="str"&gt;"Highly valued employee..."&lt;/span&gt;),&lt;br /&gt;           .Element(&lt;span class="str"&gt;"Id"&lt;/span&gt;, 42),&lt;br /&gt;           .Element(&lt;span class="str"&gt;"Name"&lt;/span&gt;, &lt;span class="str"&gt;"Arthur Dent"&lt;/span&gt;),&lt;br /&gt;           .Element(&lt;span class="str"&gt;"ContactInfo"&lt;/span&gt;,&lt;br /&gt;               .Element(&lt;span class="str"&gt;"PhoneNumber"&lt;/span&gt;, &lt;span class="str"&gt;"555-5555"&lt;/span&gt;),&lt;br /&gt;               .Element(&lt;span class="str"&gt;"Address"&lt;/span&gt;, &lt;span class="str"&gt;"Upda road 42"&lt;/span&gt;),&lt;br /&gt;               .Element(&lt;span class="str"&gt;"Country"&lt;/span&gt;, &lt;span class="str"&gt;"England"&lt;/span&gt;)));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;5. Define methods in a base class.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;This gives the desired syntax, but limits usage to derived classes.  Depending on your particular needs, this may be a show-stopper.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;6. Define methods in a base class, call from special one-off subclasses&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Consider Anders' example.  We are trying to create an XML document.  As long as we are willing to define a class that corresponds to that particular document instance, we can write this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; SomeMethod()&lt;br /&gt;{&lt;br /&gt;   XmlDocument doc = &lt;span class="kwrd"&gt;new&lt;/span&gt; OurCoolDocument();&lt;br /&gt;   &lt;span class="rem"&gt;//...&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; OurCoolDocument: DocumentWithHelperMethods&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; OurCoolDocument():&lt;span class="kwrd"&gt;base&lt;/span&gt;(&lt;br /&gt;       Element(&lt;span class="str"&gt;"Employees"&lt;/span&gt;,&lt;br /&gt;           Element(&lt;span class="str"&gt;"Employee"&lt;/span&gt;), _&lt;br /&gt;               Comment(&lt;span class="str"&gt;"Highly valued employee..."&lt;/span&gt;),&lt;br /&gt;               Element(&lt;span class="str"&gt;"Id"&lt;/span&gt;, 42),&lt;br /&gt;               Element(&lt;span class="str"&gt;"Name"&lt;/span&gt;, &lt;span class="str"&gt;"Arthur Dent"&lt;/span&gt;),&lt;br /&gt;               Element(&lt;span class="str"&gt;"ContactInfo"&lt;/span&gt;,&lt;br /&gt;                   Element(&lt;span class="str"&gt;"PhoneNumber"&lt;/span&gt;, &lt;span class="str"&gt;"555-5555"&lt;/span&gt;),&lt;br /&gt;                   Element(&lt;span class="str"&gt;"Address"&lt;/span&gt;, &lt;span class="str"&gt;"Upda road 42"&lt;/span&gt;),&lt;br /&gt;                   Element(&lt;span class="str"&gt;"Country"&lt;/span&gt;, &lt;span class="str"&gt;"England"&lt;/span&gt;)))&lt;br /&gt;   {}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;All we've done is moved the methods into a class named DocumentWithHelperMethods.  We can call them directly, with no prefix, from the constructor of the derived class.  The only constraint is that, to call them from inside "base(..)", we must make them static methods.  You can see a full example of this approach in one of the unit tests &lt;a href="http://dotnet.agilekiwi.com/blog/2007/11/smartregex.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;7. Put extension methods on "object"&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;I haven't tried this, but I presume that if you defined Element, Comment etc as extension methods on "object" then the desired syntax would work anywhere (needing only a "using" of the namespace of the extension class).   Ugly. (Update Feb 08: and it doesn't work anyway, since &lt;a href="http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=287962"&gt;an explicit "this" would be required&lt;/a&gt;, which defeats the purpose).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;I find that 2a often works well. Whenever the desired method name consists of more than one word, you can consider using the first word as a class name.&lt;br /&gt;&lt;br /&gt;But there is no one-size fits all solution.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;It's not Rocket Science&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Finally, this has really just been a post about one simple thing: finding ways to make methods that can be used without prefixing them.  As Anders Noras says, this feels like introducing new "keywords" into the language. While this can be useful (and might even be interesting)&lt;a href="http://www.oreillynet.com/onlamp/blog/2007/05/the_is_it_a_dsl_or_an_api_ten.html"&gt; it's not necessarily a DSL&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;It is not rocket science.  We're just trying to use meaningful method names, with no prefixes. Years ago, when I moved from BASIC to Pascal, I was thrilled to discover that I could write my own functions that looked "just like the built-in ones". It seems ironic that it's now so much harder to write methods without prefixes!</description><link>http://dotnet.agilekiwi.com/blog/2007/08/workarounds-to-simulate-static-imports.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4137609494488259066</guid><pubDate>Sat, 04 Aug 2007 05:18:00 +0000</pubDate><atom:updated>2007-08-03T22:31:11.860-07:00</atom:updated><title>Sparklines for WinForms</title><description>I wanted a Windows Forms implementation of Edward Tufte's "&lt;a href="http://en.wikipedia.org/wiki/Sparkline"&gt;Sparklines&lt;/a&gt;".  I couldn't find one, so wrote my own.&lt;br /&gt;&lt;br /&gt;It's just very basic at present.  You provide it a collection of numbers, representing the data points to be graphed in a sparkline.  You also tell it how many horizontal pixels to allocate to each data point, and how the total height of the sparkline in pixels.  Then it renders the sparkline for you.&lt;br /&gt;&lt;br /&gt;You can use the code as a WinForms Control (SparklineControl) or you can programatically render to any Graphics object you like, using SparklineReneder.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.agilekiwi.com/dotnet/Sparklines.cs"&gt;Here's the source code&lt;/a&gt;, and here is an example of using it.  Please leave any questions as comments on this post.  &lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Form1 : Form&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; Form1()&lt;br /&gt;    {&lt;br /&gt;        InitializeComponent();&lt;br /&gt;&lt;br /&gt;        spark.Height = 20;&lt;br /&gt;        spark.Renderer.PixelsPerElement = 5;&lt;br /&gt;        panel1.Controls.Add(spark);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    SparklineControl spark = &lt;span class="kwrd"&gt;new&lt;/span&gt; SparklineControl();&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; button1_Click(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;?&amp;gt; values = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;?&amp;gt;();&lt;br /&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; textBox1.Lines)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; trimmed = s.Trim();&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(trimmed))&lt;br /&gt;                values.Add(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;br /&gt;                values.Add(&lt;span class="kwrd"&gt;int&lt;/span&gt;.Parse(trimmed));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        spark.Renderer.DataElements = values;&lt;br /&gt;        spark.Invalidate();&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;</description><link>http://dotnet.agilekiwi.com/blog/2007/08/sparklines-for-winforms.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-2439036836418794849</guid><pubDate>Mon, 23 Jul 2007 09:06:00 +0000</pubDate><atom:updated>2007-12-07T20:22:00.645-08:00</atom:updated><title>Want a good job in Wellington?</title><description>Are you a good .NET developer looking for a job in &lt;a href="http://www.wellingtonnz.com"&gt;Wellington&lt;/a&gt;, New Zealand?&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.optimation.co.nz"&gt;My employer&lt;/a&gt; is currently looking for .NET developers (junior, senior or anywhere in between).  We may also be interested if your skills are mainly in testing or business analysis.&lt;br /&gt;&lt;br /&gt;What is the company like?  Good!  I'm very glad to recommend it.  Skilled, friendly people to work with; great customer feedback.&lt;br /&gt;&lt;br /&gt;As I've often suggested suggested in &lt;a href="http://www.agilekiwi.com"&gt;my agile blog&lt;/a&gt;, working in an IT services company is full of challenges and opportunities.  We're addressing those challenges and opportunities unusually well. &lt;br /&gt;&lt;br /&gt;If you enjoy working on successful projects, please get in touch via email. You can find my address on my other site, &lt;a href="http://www.agilekiwi.com/contact.htm"&gt;here&lt;/a&gt;.</description><link>http://dotnet.agilekiwi.com/blog/2007/07/want-good-job-in-wellington.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-8067164915752934959</guid><pubDate>Sun, 08 Jul 2007 10:44:00 +0000</pubDate><atom:updated>2008-02-09T19:15:49.247-08:00</atom:updated><title>Claytons Interception</title><description>Ayende posted &lt;a href="http://ayende.com/Blog/archive/2007/07/02/7-Approaches-for-AOP-in-.Net.aspx"&gt;7 different interception approaches&lt;/a&gt; for AOP in .NET. Here's one more...&lt;br /&gt;&lt;br /&gt;...well, actually, it's a "cheat's way" to do interception.  But, it's has proved to be useful in the real world and it avoids all of the disadvantages mentioned in Ayende's post.  &lt;br /&gt;&lt;br /&gt;It is "the interception you do, when you're not doing interception"(*).  It simply means that you write your property getters and setters in such a way that "interception" is built right into the code.  Here's an example from &lt;a href="http://blog.bluecog.co.nz/archives/2007/06/11/getting-started-with-mindscape-lightspeed/"&gt;Mindscape LightSpeed&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DisplayName&lt;br /&gt;{&lt;br /&gt;  get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _displayName; }&lt;br /&gt;  set { Set(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _displayName, &lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="str"&gt;"DisplayName"&lt;/span&gt;); }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The Set method can do whatever is required, both before and after it sets the actual value.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Pros:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Simple, understandable, debuggable.  Can still use "new" to create objects (unlike some alternatives).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Cons:&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;Only works for properties.  While properties are the &lt;span style="font-style:italic;"&gt;main&lt;/span&gt; place where interception is required (at least in ORMs and &lt;a href="http://www.matshelander.com/wordpress/?p=30"&gt;related functionality&lt;/a&gt;) some people do want interception on methods too.&lt;br /&gt;&lt;br /&gt;Another disadvantage is that you have to put the property name into the setter method.  While this is not too bad if your objects are generated (just re-generate if names change) it's still a bit ugly to have string names in the code.  I'm particularly uncomfortable with it if anyone is likely to rename the property (using Resharper's Rename feature for instance) while forgetting to also change the string name in the property setter.  And, given Murphy's Law and human nature, forgetting probably &lt;span style="font-style:italic;"&gt;is&lt;/span&gt; likely.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Property Name Solutions&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What if you don't like that property name sitting inside the setter methods?&lt;br /&gt;&lt;br /&gt;Rocky Lhotka put together a solution to the property naming problem. The solution is based framework code crawling back up the stack to find the property setter.  In my own tests, that can be slow and, as Rocky notes, it can be &lt;a href="http://www.lhotka.net/Article.aspx?area=4&amp;id=0708c745-f009-4d09-9f91-6a349b5b0317"&gt;broken by method inlining&lt;/a&gt;.  &lt;br /&gt;&lt;br /&gt;I have put together an alternative solution, which does not suffer from those drawbacks.  It lets you write the code above like this (note that the property name string is not inside the setter method any more):&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DisplayName&lt;br /&gt;{&lt;br /&gt;  get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _displayName; }&lt;br /&gt;  set { Set(&lt;span class="kwrd"&gt;ref&lt;/span&gt; _displayName, &lt;span class="kwrd"&gt;value&lt;/span&gt;); }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It works by leveraging the fact that the first parameter is a ref parameter.  With a bit of managed C++, you can convert that ref param into a managed pointer. Then you can look at how far "into" the object it points, and use that to identify the property.  (Technically, it identifies the field.  But, if each field is used by one property, then we can easily associate field names with property names.)&lt;br /&gt;&lt;br /&gt;Note that the managed C++ code is pure .NET code, marked as "&lt;a href="http://www.codeproject.com/csharp/unsafe_prog.asp"&gt;unsafe&lt;/a&gt;" so that it can use pointers.  It has to be written in C++, not C#, because while C# can use pointers, it cannot create one from a ref parameter.&lt;br /&gt;&lt;br /&gt;You can find a full working implementation &lt;a href="http://www.agilekiwi.com/dotnet/ClaytonsInterceptionDemo.cs"&gt;here&lt;/a&gt;.  Please note that I am not a C++ developer.  This was the first (and probably the last) managed C++ that I have written!  While I have undertaken a number of tests to check the validity of this appoach, the fact remains that I'm not experienced in managed C++.  Use at your own risk.  Please post any questions below.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update, Feb 08:&lt;/span&gt; I've posted an updated, re-written version &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;(*) Austrialian and New Zealand readers may recognise the theme in naming this post.  It's based on &lt;a href="http://en.wikipedia.org/wiki/Claytons"&gt;Claytons&lt;/a&gt;, marketed many years ago as "The drink you have when you're not having a drink."</description><link>http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-1043206998409466690</guid><pubDate>Fri, 29 Jun 2007 22:55:00 +0000</pubDate><atom:updated>2008-04-11T15:56:09.134-07:00</atom:updated><title>No More Development on My Side Project</title><description>&lt;i&gt;UPDATE April 2008: this is an old post.  I have since released one small part of the product as an Open Source project.  I have kept the ActiveSharp name, even though the empahsis has moved away from business rules and onto property change interception. The new implementation is, to a large extent, a ground up re-design of the old one. Both did property change notification, but the new one does it in a much better way. The rest of this post remains below, as orginally written.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Six months ago I posted an alpha release of a library for .NET development, called &lt;a href="http://www.activesharp.com"&gt;ActiveSharp&lt;/a&gt;.  It's unique feature was a &lt;a href="http://en.wikipedia.org/wiki/Dataflow"&gt;dataflow-style&lt;/a&gt; rules engine.  (You specify the rules, system calls them at the right time.)  It also included an ORM designed specifically for ease-of-use.&lt;br /&gt;&lt;br /&gt;This post is to announce that I will not be developing ActiveSharp any further.  I still have lots of ideas about it's future potential, including some useful unique features, but I simply don't have the time to develop them.  &lt;br /&gt;&lt;br /&gt;It has been an interesting journey - both in terms of the technology and in terms of trying to produce a significant product while also working full-time and raising a family. In the end, something had to give.  The ActiveSharp website will be taken down shortly (so this is your last chance to have a look at it :-)</description><link>http://dotnet.agilekiwi.com/blog/2007/06/no-more-development-on-my-side-project.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4253389350086801217</guid><pubDate>Sun, 06 May 2007 03:39:00 +0000</pubDate><atom:updated>2008-06-02T13:50:55.979-07:00</atom:updated><title>Implementing Workflow with Persistent Iterators</title><description>Why do workflow tools use diagrams and XML instead of regular programming languages?  &lt;a href="http://www.secretgeek.net/workflow_jerkflow.asp"&gt;It’s a good question&lt;/a&gt;.  When it comes to the core programming tasks - looping, conditionals and abstractions - XML does a lousy job.  (As &lt;a href="http://www.koziarski.net/archives/2007/4/3/what-am-i-missing"&gt;Koz’s post on BPEL&lt;/a&gt; explains.) As for workflow diagrams, they're just a visual representation of the XML.&lt;br /&gt;&lt;br /&gt;So why are smart people writing workflows in XML instead of Ruby, C# or Java?  If we ignore marketing, hype and that fact that XML is "just so darn cool" &lt;g&gt;, we’re left with one good reason.  It is the "showstopper" that prevents  workflow development in ordinary languages.&lt;br /&gt;&lt;br /&gt;In this post, I’m going to describe that showstopper.  I’m also going to solve it.  With the solution presented here, workflows really &lt;span style="font-style:italic;"&gt;can&lt;/span&gt; be written in ordinary languages.&lt;br /&gt;&lt;blockquote&gt;By the way, the solution presented here is in C#.  As far as I can tell it is not possible in Java.  It might be possible in Python or Ruby...&lt;/blockquote&gt; &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;The Problem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The problem is simple: workflows have really big pauses in them.  The execution of a workflow may be paused for days, weeks or even months waiting for some external event.  Normal programming languages don’t handle this well.  Imagine a long routine which represented the logic for a particular workflow.  You can’t suspend the method at the half way point, wait for three weeks (maybe even rebooting the machine while you wait), and then have it pick up exactly where it left off – resuming at the exact line where it paused, with all local variables exactly as they where before.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Update 3 Jun 08: &lt;a href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3409090&amp;SiteID=1"&gt;This Microsoft posting&lt;/a&gt; confirms big pauses as the key driver behind the development of their Windows Workflow product.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Here’s a simplified example of the problem:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ReviewDocument()&lt;br /&gt;{&lt;br /&gt;    SendToReviewer();&lt;br /&gt;    WaitForReviewersReply(); &lt;span class="rem"&gt;// might take weeks&lt;/span&gt;&lt;br /&gt;    PublishDocument();&lt;br /&gt;}&lt;/pre&gt;How do get the method to resume at the right place when the reviewer finally replies?  You can’t just call the method again, since if you do it will start again from the beginning. &lt;br /&gt;&lt;br /&gt;I believe this is why smart people are writing workflows in XML instead of real languages.  They construct a representation of the logic in XML, and write a workflow engine which can (re)serialize that representation (complete with its internal state) at each pause in the workflow.  When the wait is over, everything gets deserialized and it resumes where it left off.&lt;br /&gt;&lt;br /&gt;Ironically, all this XML stuff is unnecessary in C# 2.0 and later – which leads us to the solution...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Solution: Part 1 – Persistent Iterators&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In C# 2.0 and later, you really can get a method to stop and then resume where it left off.  The secret is the new "yield" keyword.  In .NET, a method with one or more "yields" in it is called an iterator.&lt;br /&gt;&lt;br /&gt;Here’s an example.  It returns "One" the first time it is called, "Two" the second, and "Three" the third:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; Foo()&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"One"&lt;/span&gt;;&lt;br /&gt;    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Two"&lt;/span&gt;;&lt;br /&gt;    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Three"&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;C# makes this possible with some special behind-the-scenes magic.  But there’s a catch, all that magic happens in memory.  It’s no use if we want a 3-week pause complete with system reboots.  &lt;br /&gt;&lt;br /&gt;To achieve our goal we have to serialize all that magic out to disk.  We call the method once, serialize the iterator state to disk, then three weeks later we deserialize the iterator state and make the next call to the method.&lt;br /&gt;&lt;br /&gt;That might sound difficult, but as it turns out, it’s not.  Thanks to Microsoft’s implementation of "yield" (and the flexibility of .NET serialization) it only takes a few lines of code. Here it is in action:&lt;br /&gt;&lt;br /&gt;&lt;img src="http://www.agilekiwi.com/dotnet/blog/CountingDemo.GIF" /&gt;&lt;br /&gt;&lt;br /&gt;Note that I ran the program three times.  Each time it got the next value from the sample method.  That confirms that state is being persisted &lt;span style="font-style:italic;"&gt;outside&lt;/span&gt; the process, which means it can cope with arbitrarily long delays.  You could run it today, and have it yield "One".  Then you can reboot overnight and still have it yield "Two" when you run it tomorrow.  &lt;br /&gt;&lt;br /&gt;(&lt;a href="http://www.agilekiwi.com/dotnet/CountingDemo.cs"&gt;Download source code&lt;/a&gt;.)&lt;br /&gt;&lt;br /&gt;This is the first part of our solution: &lt;span style="font-weight:bold;"&gt;persist state between yields&lt;/span&gt;.  (Update: See Microsoft's comments &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=278510"&gt;here&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Solution: Part 2 – Yield "When to Call Me Back"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's recap on what we've got.  We now have a way to call a method, have it stop part way through, pause for a &lt;span style="font-style:italic;"&gt;reeeeeealy&lt;/span&gt; long time, and then have it resume where it left off.  That’s exactly what we need to solve our workflow problem.&lt;br /&gt;&lt;br /&gt;There is only one more thing we need to figure out: what should the method return when it yields?  Assuming that the method will be called by some hypothetical workflow engine, it should return something which tells the engine when to make the &lt;span style="font-style:italic;"&gt;next&lt;/span&gt; call.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;For instance, if the method needs to pause until 7:30 am, it should yield an object which says "Call me back at 7:30".&lt;/li&gt;&lt;br /&gt;&lt;li&gt;If the method needs to pause until data is updated for customer number 123, it should yield an object which says, "Call me back when customer #123 is updated".&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;This lets us &lt;span style="font-weight:bold;"&gt;write our workflow as a perfectly ordinary method, yielding only when a long pause is required&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;There must be a degree of polymorphism in the yielded objects – since some of them represent a delay until a fixed time, some represent a delay until certain data is updated, and others may represent delays until users reply to system-generated emails and so on. There’ll also need to be some "smarts" in the engine to interpret theses objects appropriately – roughly  equivalent to the logic in an ordinary XML workflow engine, although different in the implementation details.&lt;br /&gt;&lt;br /&gt;At this point I must apologize, because I have not had time to put together a working example.  If there is enough interest in this post, I’ll try to publish one in the future.  In the meantime, here’s how you might write a simple workflow:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerator&amp;lt;EventToWaitFor&amp;gt; Foo()&lt;br /&gt;{&lt;br /&gt;    DoSomething();&lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Ask the engine to call back in 5 hours&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; TimeDelayEvent(&lt;span class="kwrd"&gt;new&lt;/span&gt; TimeSpan(5, 0, 0));&lt;br /&gt;&lt;br /&gt;    DoSomethingElse();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;To make this really workable, a little syntactic sugar may help.  Therefore, in the next example, I have introduced a method called "Until", just to make things read more smoothly.  Again, I must apologize for not having more time to explain this example, so please leave a comment below if I have failed to explain it properly.&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerator&amp;lt;EventToWaitFor&amp;gt; ApproveDocument(Document doc)&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;do&lt;/span&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="rem"&gt;// Initiate a review and yield (i.e. wait) until it is complete&lt;/span&gt;&lt;br /&gt;        ReviewAction review = &lt;span class="kwrd"&gt;new&lt;/span&gt; ReviewAction(&lt;span class="str"&gt;"qa@example.com"&lt;/span&gt;, doc);&lt;br /&gt;        &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; Until(review.Completed);  &lt;span class="rem"&gt;// might take weeks&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// (omitted) ... email original author to notify them&lt;/span&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// of review outcome ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    } &lt;span class="kwrd"&gt;while&lt;/span&gt; (!review.DocumentIsAcceptable);&lt;br /&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// Yield (i.e. wait) until scheduled publication date&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; Until(doc.PublicationDate);  &lt;span class="rem"&gt;// might take days&lt;/span&gt;&lt;br /&gt;    PublishDocument(doc);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This is the second part of our solution: &lt;span style="font-weight:bold;"&gt;yield for large pauses&lt;/span&gt;, returning an object which says when to resume.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Conclusion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Modern programming languages express logic far better than XML.  With persistent iterators, they can also handle the long pauses required in workflows.&lt;br /&gt;&lt;br /&gt;At present, there is no production-quality workflow engine which supports this model.  But perhaps there might be soon...&lt;br /&gt;&lt;br /&gt;Update: see &lt;a href="http://dotnet.agilekiwi.com/blog/2007/12/update-on-persistent-iterators.html"&gt;this&lt;/a&gt; - Microsoft may change implementation details that affect this approach.  But they recognise the usefulness of the technique, so hopefully it will be a case of &lt;span style="font-style:italic;"&gt;changing&lt;/span&gt; it, rather than breaking it.</description><link>http://dotnet.agilekiwi.com/blog/2007/05/implementing-workflow-with-persistent.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-4267030602886921765</guid><pubDate>Wed, 18 Apr 2007 09:36:00 +0000</pubDate><atom:updated>2007-04-18T02:38:43.371-07:00</atom:updated><title>Symbols Part 3</title><description>&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1487253&amp;SiteID=1&amp;mode=1"&gt;Here's another alternative&lt;/a&gt;, which would unify lambda expressions and "symbols" nicely.&lt;br /&gt;&lt;br /&gt;Also note that the solution in my &lt;a href="http://dotnet.agilekiwi.com/blog/2007/04/symbols-part-2.html"&gt;previous post&lt;/a&gt; would be good for "mock" stype unit testings, since it works just fine in .NET 2.0 an later.  It would allow you to specify method call expectations by writing the methods names directly into your code (rather than using string representations of their names).</description><link>http://dotnet.agilekiwi.com/blog/2007/04/symbols-part-3.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-1144158055458874021</guid><pubDate>Thu, 12 Apr 2007 02:00:00 +0000</pubDate><atom:updated>2007-04-18T02:49:15.232-07:00</atom:updated><title>Symbols, Part 2</title><description>I've &lt;a href="http://dotnet.agilekiwi.com/blog/2006/06/direct-support-for-symbol-names.html"&gt;previously written&lt;/a&gt; about how useful it would be to have compiler-checked symbols in C#.  It allows you to identify methods, properties etc without having to use uncheckable (and un-refactorable) string literals.  Insteads, you can use the method or property name and have it checked by the compiler (and refactored by your refactoring tool.)&lt;br /&gt;&lt;br /&gt;I have some good news and some bad news:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;The good news: &lt;/span&gt;you can simulate named symbols for &lt;span style="font-style:italic;"&gt;methods&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;right now&lt;/span&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;The bad news:&lt;/span&gt; the technique doesn't work for &lt;span style="font-style:italic;"&gt;properties&lt;/span&gt; (which is probably where you want it most).&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Anyway, here's now to get a "named symbol" effect for methods.  It relies on &lt;a href="http://unboxedsolutions.com/sean/archive/2004/03/29/203.aspx"&gt;delegate inference&lt;/a&gt; to let you to use a method name and have the system automatically create a delegate.  (Inspecting the delegate then gives the method info, when you need it.)&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="rem"&gt;// Declare a delegate for a parameterless method &lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// which returns void&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SimpleDelegate();&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;// This is the method to which you want to pass other&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// methods. Its param is typed as SimpleDelegate&lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// Return type can be void or whatever you want.  &lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// I've used bool just for the sake of the example &lt;/span&gt;&lt;br /&gt;&lt;span class="rem"&gt;// below &lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; DoStuffWithMethod(SimpleDelegate d)&lt;br /&gt;{&lt;br /&gt;   &lt;span class="rem"&gt;// here d.Method identifies the method&lt;/span&gt;&lt;br /&gt;   ...&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;// Example of use:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;// This is the method we want to refer to&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public void&lt;/span&gt; Foo()&lt;br /&gt;   { ... }&lt;br /&gt;&lt;br /&gt;&lt;span class="rem"&gt;// here o.Foo is the compiler-checked "symbol"&lt;/span&gt;&lt;br /&gt;MyObject o = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyObject();&lt;br /&gt;Assert.IsTrue(DoStuffWithMethod(o.Foo));&lt;/pre&gt;&lt;br /&gt;It would be great to have something similar for properties.  Perhaps delegate inference could be extended so that if you use a property name instead of a method name, then the inferred delegate will point to the property's "getter" method.&lt;br /&gt;&lt;br /&gt;P.S. as others have &lt;a href="http://themechanicalbride.blogspot.com/2007/03/symbols-on-steroids-in-c.html"&gt;already pointed out&lt;/a&gt;, another solution for properties is to use LINQ-based syntax.  Personally, I'd like something cleaner.  I don't like the idea of writing "o =&gt; o.Foo" every time I want to say o.Foo (although I can't argue with the fact that it does work :-)  &lt;br /&gt;&lt;br /&gt;Finally, as noted &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1463023&amp;SiteID=1"&gt;here&lt;/a&gt;, a solution which worked in atttributes would be great (which is admitedly not the case for any of the solutions mentioned in this post).&lt;br /&gt;&lt;br /&gt;Also see followup &lt;a href="http://dotnet.agilekiwi.com/blog/2007/04/symbols-part-3.html"&gt;here&lt;/a&gt;.</description><link>http://dotnet.agilekiwi.com/blog/2007/04/symbols-part-2.html</link><author>noreply@blogger.com (John Rusk)</author></item><item><guid isPermaLink='false'>tag:blogger.com,1999:blog-24760385.post-117616934710290718</guid><pubDate>Tue, 10 Apr 2007 01:41:00 +0000</pubDate><atom:updated>2007-04-09T18:42:27.113-07:00</atom:updated><title>Quoted Strings</title><description>I posted &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=233329"&gt;this suggestion&lt;/a&gt; about an alternative form of @-quoted strings.  It would be useful in cases where the string itself contains lots of double quotes.</description><link>http://dotnet.agilekiwi.com/blog/2007/04/quoted-strings.html</link><author>noreply@blogger.com (John Rusk)</author></item></channel></rss>