Problem:
Class1.foo1 relies on Class2.foo2, but it can't be guaranteed they'll compile in an order that will allow class2.foo2 to have been initialized when referenced by Class1.foo1.
class Class1
{
static Foo foo1 = new Foo("arg1", Class2.foo2);
}
class Class2
{
static Foo foo2 = new Foo("arg2");
}
Since Class2.foo2 has not be initialized when Class1.foo1 is created, the 'Class2.foo2' argument passed is simply null.
My Question:
Is there a known pattern to handle this sort of reliance scenario that doesn't require verbose initializers to be written for every field? I can throw as much code into the Foo class as needed to make it work because it only runs once at the beginning of the program, I'd just really like to keep it hidden away, because a great number of these fields need to be written.
What I've Read:
I've come across a few threads talking about lazy initialization which almost sounds appropriate in that it does something the first time an object is needed, and it's field specific, but it results in unwieldy code:
static Foo _otherFoo;
static Foo otherFoo
{
get
{
if (_otherFoo == null)
return new Foo("arg2");
else return _otherFoo;
}
}
I'd like to save my fellow developers from carpal tunnel. It wouldn't be a problem if all this bloat could be hidden behind a constructor:
public Foo(params Foo[] dependencies)
{
if(anyItem in dependencies) == null
anyItem = new Foo("Params are defined per-instance in the initializer. What goes here?");
}
But I can't know Class2.foo2's initialization parameters. Since this only runs once at startup, I decided to look into reflection as an option, but from what I've seen, reflection can't help you discern anything about a field's initializer, you can only get its value after it's been initialized.
It looks like the order of class initialization is determined by the order fields are accessed:
Is the order of static class initialization in C# deterministic?
So I ran some tests to see how this plays out, and it doesn't seem to halt initialization of fields in order to intialize their dependencies i.e. Class1.foo1's constructor must fully complete before Class2.foo2 gets initialized, which means Class2.foo2 will remain null.
My Question (again):
Is there a known pattern to handle this sort of reliance scenario that doesn't require verbose initializers to be written for every field?