Scott’s lazy loader

Published 30 April 04 03:37 PM

Scott Wisniewski posted a lazy loader implementation on his blog.  He coded it blind, without the help of a compiler that could do C# generics.  I went back through and made it legal C# code.  I also did a little refactoring.

 

  • The ILazyLoader is now nested in the LazyLoad<T> class, which means it doesn’t need its own type parameter.
  • The getter has only one return.
  • A DefaultLoad derives from LazyLoad, and adds the ‘new()’ constraint to T.

 

The code is now legal C#, but I haven’t verified that it actually works yet.

 

public class LazyLoad<T>

{

      public interface ILazyLoader

      {

            T CalculateValue();

      }

 

      private T value_;

      private bool didFetch_ = false;

      private ILazyLoader lazyLoader_;

 

      public LazyLoad(ILazyLoader lazyLoader)

      {

            lazyLoader_ = lazyLoader;

      }

 

      public T Value

      {

            get

            {

                  if (!didFetch_)

                  {

                        didFetch_ = true;

                        value_ = lazyLoader_.CalculateValue();

 

                  }

                  return value_;

            }

      }

 

}

 

public class DefaultLazyLoad<T> : LazyLoad<T>

            where T : new()

{

      public class DefaultConstructLazyLoader : ILazyLoader

      {

            public DefaultConstructLazyLoader() { }

            public T CalculateValue()

            {

                  return new T();

            }

      }

 

      public DefaultLazyLoad() : base(new DefaultConstructLazyLoader()) { }

}

 

 

Comments

# Wizzy's World said on April 30, 2004 6:38 PM:
# Scott Wisniewski said on April 30, 2004 6:38 PM:
Thanks for fixing my code :)
# ZirakZigil said on April 30, 2004 10:39 PM:
Cool idea. Note that the getter is not thread-safe (but this is easily fixed)
# Julien Couvreur (Dumky) said on May 5, 2004 12:54 PM:
Nice.
An interface (ILazyLoader) with just one method, maybe you want to use a delegate there...

I've been thinking about the same problem applied to the Singleton pattern. Can Generics be used to implement it once and for all? Any thoughts?
# Julien Couvreur (Dumky) said on May 5, 2004 1:14 PM:
About the Singleton: it's easy to have the generic Singleton be a seperate class. It would look like: Singleton<MyClass>.GetInstance().MyMethod() or Singleton<MyClass>.Instance.MyMethod() if you use a property.

But often you see the class and the singleton merged, so that you ensure MyClass can't be instanciated outside of the singleton.
Plus if you change your mind and decide you want 3 pooled instances instead of 1 (a "Threepleton"?) you need to change code all over the place.

I'm not sure if/how you can do in C#+generics.
# jaybaz [MS] said on May 5, 2004 8:19 PM:
singleton: In C++, I wrote a singleton mixin. It was nice & simple, and did wonders for clarity. I'm disappointed that mixins in C# are so clunky. I hope that a future C# language change would let mixins into C# programs.
# jaybaz_MS's WebLog said on May 6, 2004 4:58 PM:
# jaybaz_MS's WebLog said on May 6, 2004 5:51 PM:
# Wizzy's World said on May 6, 2004 10:21 PM:
New Comments to this post are disabled

This Blog

Syndication

Page view tracker