A static riddle (or: how to avoid lazy instantiation?)
-
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
-
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
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