There are no problems that cannot be solved by adding more abstraction. (*)
The pattern that you repeat over and over is a lazy loading pattern. This pattern is very capturing in the type, and in fact it was in version 4 of the structure. The documentation is here:
http://msdn.microsoft.com/en-us/library/dd642331.aspx
Then you can do something like:
public void Foo(AnotherType theObj) { var shared = new Lazy<SomeType>(()=>LoadShared()); theObj.LoadThing += () => shared.Value.Thing; theObj.LoadOtherThing += () => shared.Value.OtherThing;
And you go there. At the first call to shared.Value
value is loaded; Each subsequent period of time uses the cached value. Additional bonus: it is even thread safe if access to common values ββwill be available on several threads. (For details on what guarantees we provide regarding thread safety, see the documentation).
(*) Except, of course, for the problem: "I have too much abstraction."
source share