Class usable from all other class

G

Geoffrey

Hello,

Form my software, I create a class to manage configuration.
Configuration parameters are retrieved from wml file, ini file, registry,
....

For this, I have a constructor that create a dataset with all my parameters,
and I have a method "getParameter" that return a value of a parameter ...
This class can do other think like modify parameter, display a usable list,
....

When I have a lot of class on my project, I must use an instance of my
configuration class in each of the class that would use it.

So, I have lot of instance of the same class ...

In c#, it's not possible to declare a global instance of this class.
And want to have a constructor (so impossible to have statict method) to
retrieve all parameters.
I don't want to have a static method that allways read configuration files
to retrieve a parameter.


What is the best design to do that ?
Allways create new instance on each class ?

Thx
 
F

Frans Bouma [C# MVP]

Geoffrey said:
Hello,

Form my software, I create a class to manage configuration.
Configuration parameters are retrieved from wml file, ini file,
registry, ...

For this, I have a constructor that create a dataset with all my
parameters, and I have a method "getParameter" that return a value of
a parameter ... This class can do other think like modify parameter,
display a usable list, ...

When I have a lot of class on my project, I must use an instance of my
configuration class in each of the class that would use it.

So, I have lot of instance of the same class ...

In c#, it's not possible to declare a global instance of this class.
And want to have a constructor (so impossible to have statict method)
to retrieve all parameters.
I don't want to have a static method that allways read configuration
files to retrieve a parameter.


What is the best design to do that ?
Allways create new instance on each class ?

first, create a singleton for your configuration class. This should be
done by a thread-safe variant of which you can find more info of here:
http://www.yoda.arachsys.com/csharp/singleton.html

second, inside the single instance of your configuration class, you
have to make sure just 1 thread can do things inside your singleton
class if you manipulate data. So you have to surround these code parts
with lock statements:

lock(this)
{
// critical code which has to be executed by 1 thread at a time
}

good luck.

Frans

--
 
M

Marc Gravell

It depends on whether anything (fields etc) ever changes between instances
of your class in the current implementation.

If so (i.e. via different calling parameters etc each instance has different
params), then you are possibly limited to your current approach.

If not, then you could either expose a singleton instance on a static
property, or you could static class/methods (which to me sounds more likely
as the right solution).

And yes, I know you said you can't use static methods, but you didn't say
why. For example, it this is because different params access different
config files, you could use a static (private) hashtable (or Dictionary in
2.0) to cache the contents of multiple between calls (differentiating on one
of the params).

Any more info on what your need to do? Or a cut-down version of what you
have and where it is limited?

Marc
 
G

Geoffrey

I'm beginner in c#.

I think I dont completly understand the use of static.

On my constructor, I read some files, registry, .. and construct a dataset
with all parameters. (I use a dataset because my class is a WinForm and can
display the list of params ...). But for storing parameters in memory, I can
use what you would.

I say that I can't use static method because with static method you don't
store any instance of class, so you don't have permanent cache of
parameters, and I must re-read all config files, ... for each call to
getparam.

But, if i good understand, I can have a static field on my class, on first
call of getparam, I fill my dataset, and on other calls, my dataset is still
in memory
it's just ?
 
M

Marc Gravell

Under 2.0 you can actually have a static constructor that is (given a few
caveats) run automatically, which initialises the static fields.

I can't remember the technical details, but if I have the following, the
static constructor will run *before* the GetValue() method call.
Unfortunately, static ctors must be parameterless (since you don't invoke
them directly), so no way to add extra options here.

public static class MyClass {
private SomeType someField;
static MyClass() {
someField = new SomeType(); // pre-populate the data
}
public static GetValue(string setting) {
return someField[setting];
}
}

static void Main() {
Console.WriteLine(MyClass.GetValue("abc"));
}


The static ctor could read all the values into properties, or just prepare
the config object, or whatever.
If you need to access multiple items (as needed), I would (as mentioned) use
a static Dictionary - something like:

public static class MyClass {
private Dictionary<string, SomeType> data;
static MyClass() {
data = new Dictionary<string, SomeType>();
}
public static GetValue(string file, string setting) {
SomeType obj;
lock(data) { // lock while checking/adding
if(!data.TryGetValue(file, out obj)) {
obj = new MyClass(data); // init
data.Add(file, obj)
}
}
// continue outside of lock
return obj[setting];
}
}

static void Main() {
Console.WriteLine(MyClass.GetValue("MainConfigFile","abc"));
}


Does that help?

Marc
 
M

Marc Gravell

Sorry - the fields should be marked static also, and the new MyClass()
should probably take file, not data, as the param

Marc
 
L

Lucky

hi,
Making Singleton class is good idea. but letting it hold all the
data for different Configuration is not good idea from my point of
view. it is good if more than one class is using the same data very
frequently but what if only one class needs a perticular configuration
details? you have not mentioned here that so i'm assuming that some of
classes only required perticular config info that will not be used by
other classes, in this case letting the object holds all the config
data is not good idea.

instead of this, if you allow the class which ask for the configuration
to hold data through out the life of class or application, will be good
and also it will make your config data object light weight.

there might be situation when some of the classes will not be used
different config details so such details has to be release in order to
free memory.

i think on it. and guys do let me know if you have comment on this. i
really like to have some.
 
G

Geoffrey

I make try with a singleton.

From my point of view, it's better that my class hold all the parameters.
Ther is only 10 or 20 parameters (a big maximum is 100 string parameters).
From the memory point of view, it's not a problem to hold that. For my
performance, it's better to keep on memory.
 
G

Guest

Geoffrey said:
I think I dont completly understand the use of static.

On my constructor, I read some files, registry, .. and construct a dataset
with all parameters. (I use a dataset because my class is a WinForm and can
display the list of params ...). But for storing parameters in memory, I can
use what you would.

I say that I can't use static method because with static method you don't
store any instance of class, so you don't have permanent cache of
parameters, and I must re-read all config files, ... for each call to
getparam.

Yes you can. You can't declare a class as static, but you can have a static
constructor and static variables.

This is valid in 1.1 (typing into the message window, not a compiler so
typos possible)

public class MyClass
{
public static int i;

static MyClass()
{
i = 5;
}
}


From somewhere else in your code you can then do

int someInt = MyClass.i;

You should of course usestatic properties instead of public variables but
this was just to be a quick example.
 
M

Marc Gravell

For completeness: in 2.0 you can mark the class as static - the compiler
then enforces this (no instance members, including bespoke ctors), and
doesn't generate a default instance ctor

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top