<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx</link><description>Cyrus then incorporated the Weak/Strong reference stuff into the LazyLoader. He also refactored the factory to give you a reliable, predictable default &amp;amp; be a bit simpler. (You also need Optional&amp;lt;&amp;gt; and the Lock&amp;lt;&amp;gt; code.) First, the delegate</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>This is easy?</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#128429</link><pubDate>Sat, 08 May 2004 14:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:128429</guid><dc:creator>JD</dc:creator><description>This is an interesting exploration of using generic programming.&lt;br&gt;&lt;br&gt;Pardon the naivete, but how does this make the resulting code any better? I suppose that it's better to avoid re-implementing Lazy Loading in every place it's used. &lt;br&gt;&lt;br&gt;This is quite complex though, and I consider the code written in the class to be much less readable than it would if the lazy-loading were encapsulted in a single method on the class.  I'd like to see a sample that uses an instance (not static) creator doing some reasonably instance-based work (instead of using a constant string for the bitmap), and compare it to the corresponding code without LazyLoader.&lt;br&gt;Again, maybe I'm just missing the point and need it banged in with samples. I like the investigation regardless, very interesting.&lt;br&gt;&lt;br&gt;&lt;br&gt;Side question; do you use Optional instead of Nullable for a reason?</description></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#128535</link><pubDate>Sat, 08 May 2004 20:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:128535</guid><dc:creator>Kevin Pilch-Bisson</dc:creator><description>Nullable&amp;lt;T&amp;gt; is being changed to only be allowed for ValueTypes later in Whidbey.  The reason for this is that the language designers want to avoid seeing API's introduced that take Nullable&amp;lt;string&amp;gt;, and the resulting fracturing that can occur.</description></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#128601</link><pubDate>Sun, 09 May 2004 00:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:128601</guid><dc:creator>jaybaz [MS]</dc:creator><description>The code we wrote here violates YAGNI.  We've written a lot of generality in that we don't know we need.  The LazyLoader class varies on 3 axes:&lt;br&gt;&lt;br&gt;1. Locked or not.&lt;br&gt;2. Weak or strong reference.&lt;br&gt;3. delegate construction or new() construction &lt;br&gt;&lt;br&gt;If you remove one of these features by selecting the option on the left, the class becomes much simpler.  If you always select the option on the right, the difference is small.&lt;br&gt;&lt;br&gt;If you have a need for exactly one combination of these features, you could write that in less code.  If you only need it once, you could write it in place, without a new class.  &lt;br&gt;&lt;br&gt;If you need that specific combination in 20 places in your app, and no other combo, then a class that supported exactly that, with no options, would make your code well factored.&lt;br&gt;&lt;br&gt;If you need the flexibility of all 3 of the above options, then I think the code we've written is about as clear as it possibly can be, with the least duplication.  What we've done is write a very full-featured solution, as simply as possible.  We're acting like library writers, not application writers.  App writers should write the simplest solution, as simply as possible.&lt;br&gt;&lt;br&gt;It does assume that the reader is comfortable with generics, delegates, an OO concepts.  I think that's a perfectly reasonable assumption.  </description></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#129458</link><pubDate>Tue, 11 May 2004 00:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129458</guid><dc:creator>Cyrus Najmabadi</dc:creator><description>JD:&lt;br&gt;&lt;br&gt;When Kevin Jay and I wrote the initial implementation, we were trying to see if it was possible to cleanly and usuably extract the following common pattern from your source:&lt;br&gt;&lt;br&gt;public T SomeProperty {&lt;br&gt;    get {&lt;br&gt;        lock (this) {&lt;br&gt;             if (!someField.IsInitialized) {&lt;br&gt;                 someField.Initialize();&lt;br&gt;             }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        return someField;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;However, the locking, testing and  initialization can be arbitrarily complex (although in most cases it's just the initialization that's complex).&lt;br&gt;&lt;br&gt;However, after writing that I realized that this wouldn't be useful for me.  Why?  Because a large number of my lazy loaded properties are, in fact, weakly referenced.  I don't want to create them until necessary (costly to create) and i don't want to keep them when the system is under memory pressure (costly to maintain).  So I end up writing this code quite often:&lt;br&gt;&lt;br&gt;private WeakReference cachedFoo;&lt;br&gt;public T Foo{&lt;br&gt;    get {&lt;br&gt;        lock (this) {&lt;br&gt;             T foo = default(T);&lt;br&gt;             bool initialized = false;&lt;br&gt;             if (cachedFoo != null) {&lt;br&gt;                object o = cachedFoo.Target;&lt;br&gt;                if (o != null) {&lt;br&gt;                    foo = (T)o;&lt;br&gt;                    initilized = true;&lt;br&gt;                }&lt;br&gt;             }&lt;br&gt;&lt;br&gt;             if (initialized == false) {&lt;br&gt;                foo = SomeComplexBitOfCode();&lt;br&gt;                cachedFoo = new WeakReference(foo);&lt;br&gt;             }&lt;br&gt;&lt;br&gt;             return foo;&lt;br&gt;         }&lt;br&gt;     }&lt;br&gt;}&lt;br&gt;&lt;br&gt;That's some pretty ugly code.  It's much much much uglier when you have 10 properties.&lt;br&gt;&lt;br&gt;Now I can replace it with:&lt;br&gt;&lt;br&gt;private Creator&amp;lt;T&amp;gt; getFoo = LazyLoaderCreator.Create&amp;lt;T&amp;gt;(false, true, elegate { return SomeComplexBitOfCode(); });&lt;br&gt;public T Foo {&lt;br&gt;    get {&lt;br&gt;        return getFoo();&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;Now, all the locking and referencing issues are taken care of for me.  All i need to care about is the initialization logic.  That's a savings of 15 identical lines every time i need this pattern.  &lt;br&gt;&lt;br&gt;As Jay said above, this was not meant to solve a specific application's needs.  Rather, we wanted to provide a library that people could use in their own application to solve all of these needs.&lt;br&gt;&lt;br&gt;Note: No user ever need be concerned with the implementation of LazyLoader, IOptional, ILock, or IReference.  Rather, they should only care about these following two things:&lt;br&gt;&lt;br&gt;delegate A Creator&amp;lt;A&amp;gt;();&lt;br&gt;and&lt;br&gt;public static Creator&amp;lt;A&amp;gt; LazyLoaderCreator.Create&amp;lt;A&amp;gt;(Creator&amp;lt;A&amp;gt; create);&lt;br&gt;&lt;br&gt;And that method basically says &amp;quot;Given a function that will create an A, i will give you back a function that when invoked will lazily create an A and then return that on successive calls&amp;quot;.&lt;br&gt;&lt;br&gt;So the complexity that you see is, IMO, the complexity that we've kept you from having to introduce into your own class _every_ place you need lazy loading.</description></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#129459</link><pubDate>Tue, 11 May 2004 00:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129459</guid><dc:creator>Cyrus Najmabadi</dc:creator><description>Sorry if the above comment is hard to read (both codewise and grammarwise).  Looks like you can't edit a comment after you've posted.  If anyone would like me to clarify anything about that, please let me know.</description></item><item><title>First blog entry</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#132641</link><pubDate>Sun, 16 May 2004 01:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:132641</guid><dc:creator>Cyrus' WebLog</dc:creator><description /></item><item><title>So many posts in so little time...</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#133031</link><pubDate>Mon, 17 May 2004 09:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:133031</guid><dc:creator>Code/Tea/Etc...</dc:creator><description /></item><item><title>Theo schools me on OO over functional programming</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#134723</link><pubDate>Wed, 19 May 2004 08:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:134723</guid><dc:creator>Cyrus' Blather</dc:creator><description /></item><item><title>Theo schools me on OO over functional programming</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#134745</link><pubDate>Wed, 19 May 2004 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:134745</guid><dc:creator>Cyrus' Blather</dc:creator><description /></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#135177</link><pubDate>Wed, 19 May 2004 18:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:135177</guid><dc:creator>JD</dc:creator><description>I appreciate the explanation that shows the code you wanted to replace with it. I'm more concerned about the readbility and usability of the code that the user sees:&lt;br&gt;&lt;br&gt;private Creator&amp;lt;T&amp;gt; getFoo &lt;br&gt;= LazyLoaderCreator.Create&amp;lt;T&amp;gt;(false, true, delegate { return SomeComplexBitOfCode(); }); &lt;br&gt;&lt;br&gt;public T Foo { &lt;br&gt;get { return getFoo(); } &lt;br&gt;}&lt;br&gt;&lt;br&gt;I don't think my dislike for this code is based soley on its use of generics. Maybe some of it is naming. Creating a creator, the bool params, even the delegate (though I understand that's the most general case),&lt;br&gt;&lt;br&gt;private LazyLoader&amp;lt;T&amp;gt; lazyGetFoo &lt;br&gt;= LazyLoaderCreator.Create&amp;lt;T&amp;gt;(); &lt;br&gt;&lt;br&gt;public T Foo { &lt;br&gt;get { return lazyGetFoo(); } &lt;br&gt;}&lt;br&gt;&lt;br&gt;With overloads:&lt;br&gt;LazyLoaderCreator.CreateLoaderWithLocks&amp;lt;T&amp;gt;()&lt;br&gt;LazyLoaderCreator.CreateLoaderWithWeakReferences&amp;lt;T&amp;gt;()&lt;br&gt;LazyLoaderCreator.CreateLoaderWithLocksAndWeakReferences&amp;lt;T&amp;gt;()&lt;br&gt;&lt;br&gt;Those aren't the best either, maybe enums, maybe some other factorization that makes clearer what the code is doing. The maintenance programmer isn't going to understand your brilliance without some help, is all.</description></item><item><title>re: The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#135279</link><pubDate>Wed, 19 May 2004 19:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:135279</guid><dc:creator>jaybaz [MS]</dc:creator><description>JD: I hear ya.  I've had some of the same concerns, but not sure where to go with it next.&lt;br&gt;&lt;br&gt;We'll keep pondering.</description></item><item><title>Singleton Pattern</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#136531</link><pubDate>Fri, 21 May 2004 10:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:136531</guid><dc:creator>More .NET Yumminess</dc:creator><description /></item><item><title>Actors and Actresses  &amp;raquo; Archive du blog   &amp;raquo; jaybaz [MS] WebLog : The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#6983185</link><pubDate>Fri, 04 Jan 2008 23:46:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6983185</guid><dc:creator>Actors and Actresses  » Archive du blog   » jaybaz [MS] WebLog : The new new lazy loader</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://actors.247blogging.info/?p=4241"&gt;http://actors.247blogging.info/?p=4241&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Dinner and a Movie &amp;raquo; jaybaz [MS] WebLog : The new new lazy loader</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#8327877</link><pubDate>Thu, 20 Mar 2008 23:32:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8327877</guid><dc:creator>Dinner and a Movie » jaybaz [MS] WebLog : The new new lazy loader</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://dinnermoviesblog.info/jaybaz-ms-weblog-the-new-new-lazy-loader/"&gt;http://dinnermoviesblog.info/jaybaz-ms-weblog-the-new-new-lazy-loader/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> jaybaz MS WebLog The new new lazy loader | fix my credit</title><link>http://blogs.msdn.com/jaybaz_ms/archive/2004/05/07/128185.aspx#9765204</link><pubDate>Wed, 17 Jun 2009 05:40:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9765204</guid><dc:creator> jaybaz MS WebLog The new new lazy loader | fix my credit</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://fixmycrediteasily.info/story.php?id=4662"&gt;http://fixmycrediteasily.info/story.php?id=4662&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>