Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A static riddle (or: how to avoid lazy instantiation?)

A static riddle (or: how to avoid lazy instantiation?)

Scheduled Pinned Locked Moved C#
learningtutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luca Leonardo Scorcia
    wrote on last edited by
    #1

    Let's say I have a resource broker, a 'lookup resource' event and a main program that will invoke the lookup resource event. All of these reside in decoupled modules, there may be only one resource broker and of course only one lookup event definition. I created the following classes to implement the situation above:

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Program::Main");
    EventBroker.Current.FireLookupResource(null, EventArgs.Empty);

       Console.ReadLine();
    

    }
    }

    public class EventBroker
    {
    private static readonly EventBroker _current = new EventBroker();
    public static EventBroker Current { get { return _current; } }

    public event EventHandler LookupResource;
    
    private EventBroker() { }
    
    public void FireLookupResource(object sender, EventArgs args)
    {
        Console.WriteLine("EventBroker::FireLookupResource");
    
        if (LookupResource != null)
           LookupResource(sender, args);
    }
    

    }

    public class ResourceBroker
    {
    private static readonly ResourceBroker _current = new ResourceBroker();
    public static ResourceBroker Current { get { return _current; } }

    private ResourceBroker()
    {
        Console.Write("ResourceBroker::ResourceBroker");
    
        EventBroker.Current.LookupResource += EventBroker\_LookupResource;
    }
    
    private static void EventBroker\_LookupResource(object sender, EventArgs e)
    {
        Console.Write("ResourceBroker::EventBroker\_LookupResource");
    }
    

    }

    However the output is not what I expected: the LookupResource event never gets called. That's understandable, since the ResourceBroker never gets referred in the executing code, it will not get instantiated. But what should I do to get the 'correct' output? Creating a NoOp method in the ResourceBroker, and calling it in the Main function, works but it seems to me like an ugly hack. Am I missing something in the language keywords/features? Thanks in advance

    Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

    C 1 Reply Last reply
    0
    • L Luca Leonardo Scorcia

      Let's say I have a resource broker, a 'lookup resource' event and a main program that will invoke the lookup resource event. All of these reside in decoupled modules, there may be only one resource broker and of course only one lookup event definition. I created the following classes to implement the situation above:

      class Program
      {
      static void Main(string[] args)
      {
      Console.WriteLine("Program::Main");
      EventBroker.Current.FireLookupResource(null, EventArgs.Empty);

         Console.ReadLine();
      

      }
      }

      public class EventBroker
      {
      private static readonly EventBroker _current = new EventBroker();
      public static EventBroker Current { get { return _current; } }

      public event EventHandler LookupResource;
      
      private EventBroker() { }
      
      public void FireLookupResource(object sender, EventArgs args)
      {
          Console.WriteLine("EventBroker::FireLookupResource");
      
          if (LookupResource != null)
             LookupResource(sender, args);
      }
      

      }

      public class ResourceBroker
      {
      private static readonly ResourceBroker _current = new ResourceBroker();
      public static ResourceBroker Current { get { return _current; } }

      private ResourceBroker()
      {
          Console.Write("ResourceBroker::ResourceBroker");
      
          EventBroker.Current.LookupResource += EventBroker\_LookupResource;
      }
      
      private static void EventBroker\_LookupResource(object sender, EventArgs e)
      {
          Console.Write("ResourceBroker::EventBroker\_LookupResource");
      }
      

      }

      However the output is not what I expected: the LookupResource event never gets called. That's understandable, since the ResourceBroker never gets referred in the executing code, it will not get instantiated. But what should I do to get the 'correct' output? Creating a NoOp method in the ResourceBroker, and calling it in the Main function, works but it seems to me like an ugly hack. Am I missing something in the language keywords/features? Thanks in advance

      Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása

      C Offline
      C Offline
      carbon_golem
      wrote on last edited by
      #2

      Hmmmm... This is a bit of a tricky situation. Usually when I find myself in this situation, it means it's time to do a redesign. As a suggestion, you may want to make a BrokerManager that does all of the initialization for you, or pick an appropriate Broker class and have it take an instance of the other as a constructor argument. You can limit the instances that way, and you may be able to do away with the singleton pattern. Hope that helps somewhat. Scott P

      "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups