471,350 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Class usable from all other class

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
Jan 16 '06 #1
9 1290
Geoffrey wrote:
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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 16 '06 #2
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
Jan 16 '06 #3
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 ?

"Marc Gravell" <mg******@rm.com> a écrit dans le message de
news:eh**************@TK2MSFTNGP12.phx.gbl...
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

Jan 16 '06 #4
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
Jan 16 '06 #5
Sorry - the fields should be marked static also, and the new MyClass()
should probably take file, not data, as the param

Marc
Jan 16 '06 #6
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.

Jan 16 '06 #7
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.
"Lucky" <tu************@gmail.com> a écrit dans le message de
news:11*********************@g43g2000cwa.googlegro ups.com...
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.

Jan 16 '06 #8


"Geoffrey" wrote:
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.
Jan 16 '06 #9
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
Jan 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Bezalel Bareli | last post: by
6 posts views Thread by Max | last post: by
1 post views Thread by Rob Andrew | last post: by
21 posts views Thread by phpCodeHead | last post: by
12 posts views Thread by Shraddha | last post: by
1 post views Thread by chris.bahns | last post: by
4 posts views Thread by dascandy | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.