Sean Feldman's profileברוכים הבאיםPhotosBlogLists Tools Help

Blog


    11/07/2007

    Generic Event Handler

    JP Boodhoo has showed a nice way of using generic event handler: http://www.jpboodhoo.com/blog/LeveragingTheEventHandlerDelegateMoreEffectively.aspx
     
    PS: Interestingly, I was going through Microsoft WCSF and the ObjectBuilder they have there is using a type called DataEventArgs, which is the same JP proposed.
    04/07/2007

    Head First Design Patterns Book

     Amazingly simple book on quiet complex things. Recommended. The person who was preaching about it was JP Boodhoo - damn he was right about it.

    Singleton

    Definition of a singleton is simple. So is the usage, but when it comes to testing/mocking/dynamically replacing it - that's a pain. While investigating MVP pattern, and reading materials from Jeremy D. Miller, I ran into on of his blogs about Singleton. It inspired me to adopt the approach and share the thoughts.

    Singleton (noun) -  single card of a particular suit (Cards); individual occurrence or item, single instance or thing

    I liked the 1st definition - a single card of particular suit. In real project a singleton is one and only, but from time to time, you need to replace it with a different implementation. So this is what I learned:

    1. Define an interface ("Code to interface, not a concrete implementation" - that's what they say)
    2. Create Singleton class (variations) with interface being implemented
    3. During run-time/unit testing decide what to do

      public interface ISingleton
      {
        /// <summary>All implementors of <see cref="ISingleton"/> interface will have the method.</summary>
        string DoThisAndThat();
      }

      public class Singleton : ISingleton
      {
        private static readonly Singleton _instance;
        private static readonly string _testData = "Unique data for this variation of singleton";
    
        static Singleton()
        {
          _instance = new Singleton();
        }
    
        private Singleton() {}
    
        public static Singleton Current
        {
          get
          {
            return _instance;
          }
        }
    
        #region ISingleton Members
    
        public string DoThisAndThat()
        {
          return _testData;
        }
    
        #endregion
      }

    Usage:

    ISingleton current = Singleton.Current;
    Console.WriteLine(current.DoThisAndThat());
    Better approach would be to have a factory that would return the Right singleton as a ISingleton, based one what is registered/required. One of the probably the best things about it is the fact that now it can be mocked as well.
    01/07/2007

    MCTS Web

    I am officially certified now.