2

I am writing a program which will manipulate certain objects, let's call them "Words". Each word is an instance of one of several classes. There can be lots and lots of these at a time.

Each of these Word objects needs access to a dictionary, stored in an XML file. I don't feel like each Word object should be loading the XML file separately. The Word objects should be able to access some global pool of program data.

What's the best way to deal with this? Should I have a class called ProgramData which contains the XML document and gets passed to every Word object when they are created? This won't cause multiple instances of the XML file to be loaded into memory, will it? Can I do what I want to do without passing ProgramData to every new object?

2 Answers 2

2

You should load the XML file into one instance of ProgramData, and then pass that instance into each Word instance (maybe in the constructor). You could also make a static property in the Word class that you set before you start instantiating Words, but be sure to use locking for thread safety.

Your alternative is the Singleton pattern, but trust me, you don't want to go down that road.

Edit: Just to be clearer, this is the first option (the one I'd use):

public class Word
{
   private ProgramData _Data
   public Word(ProgramData data)
   {
      _Data = data;
   }

   public void MethodThatUsesData
   {
      // _Data.TryGetValue()
   }
}

// in your main method or initialization routine:
ProgramData data = MethodThatLoadsData();
Word w = new Word(data);
Sign up to request clarification or add additional context in comments.

4 Comments

Why is there a need for locking? OP's code is single threaded, he can simply set the static property before using it. Besides, locking is probably above OP's level right now.
I'm making no assumptions on single-threadedness, nor the OPs technical ability to use proper locking constructs. If it will be a set-and-forget, locking may not be required, but it's something for him/her to consider.
Fair enough. To rephrase.. my previous comment was mostly to notify the OP that setting the static property first is enough, and if he does so, he does not need to worry about thread safety or locking. Not trying to knock it, and +1 for your reply. :)
Good point for him to consider, though, and I could have been more clear on why/when locking was required. Even still, I'd probably avoid static properties altogether in favor of injecting it into the constructor (for example, what happens when he needs to use 2 separate ProgramData dictionaries). But, just gave him 3 popular options to consider.
1

Your ProgramData class can have a static variable for the XmlDocument and thus you won't have to pass the variable through a constructor; your other class can just reference ProgramData.YourVariable.

To answer your other question, no, this approach will not cause multiple instances of the XML file to be loaded into memory, and nor will drharris's approach.

Also, keep in mind that this information applies to all object oriented programming languages (at least those that I'm aware of), not just C#. If you load the XML document into memory once in a method called myMethod(), and myMethod() is only called once... guess what, that XML document will only get loaded into memory once. Only things that you coded to happen, or that another dev coded (and you called either directly or indirectly) will happen. There is no magic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.