"Global" Dataset - Singleton

H

Hans Greif

Hallo,

hello, i try to implement a global dataset - singleton based.

On www.bakterienforum.de i found a singleton implementation:

sealed class SingletonCounter
{
public int Counter = 0;

private SingletonCounter(){}
public static readonly SingletonCounter Instance = new
SingletonCounter();
}

Can i use this for implementing the global dataset and how can i do this?

Thank you in advance.

hans
 
S

Simon Tamman

I usually recommend the following article on the Singleton pattern.

http://www.yoda.arachsys.com/csharp/singleton.html

However I don't think you are looking for a Singleton, you just want a
static dataset, right?
So you just need to declare:

public class SomeClass
{
private static DataSet globalDataSet = new DataSet();

public static DataSet GlobalDataSet
{
// TODO: Implement locking if using multiple threads
get{return globalDataSet;}
}
}

Then you can get access to the dataset by stating:

SomeClass.GlobalDataSet

However, a static dataset sounds absolutely hideous to me and I think you
might be misunderstanding the concepts of OOP if you are looking to use one.
Singleton items are usually for objects or services that you only ever want
1 of in existence, for example an object that handles the database or
possibly uses a lot of resources (i'm sure there are loads of other good
reasons too).
Datasets are usually just currency that move between architectural tiers
there is hardly ever reason to make static ones.
Could I ask what exactly do you need this static dataset for?

HTH

Simon
 
M

Max Fox

Am Sat, 09 Sep 2006 21:02:55 GMT schrieb Simon Tamman:

well, thank you for your answer.
I want to access a dataset threadsfae from different forms, so i though
maybe the singleton approach will help.

If the singleton nor the static is right, what would you suggest. It should
be threadsafe for sure. Btw, the dataset should be inside the application,
no external database and so on.

Thank you in advance

hans
 
G

Guest

I assume their is a potential need to synchronise access to the dataset as
some threads are going to change values inside it. What exactly are the
threads doing to the dataset and how up to date do they all need to be about
changes. The answers to these questions will determine the complexity of the
synchronisation code.

Ciaran O'Donnell
 
M

Mark Rae

Am Sat, 09 Sep 2006 21:02:55 GMT schrieb Simon Tamman:

well, thank you for your answer.
I want to access a dataset threadsfae from different forms, so i though
maybe the singleton approach will help.

If the singleton nor the static is right, what would you suggest. It
should
be threadsafe for sure. Btw, the dataset should be inside the application,
no external database and so on.

In which case, I'm a little puzzled...

You want a static dataset, but you don't populate it initially from a
database...?

So where does the data come from?

What sort of data is it?

What do you subsequently use it for?
 
M

Max Fox

Am Sun, 10 Sep 2006 07:54:11 +0100 schrieb Mark Rae:
In which case, I'm a little puzzled...

You want a static dataset, but you don't populate it initially from a
database...?

So where does the data come from?

What sort of data is it?

What do you subsequently use it for?

Well, i do have an initially dataset which is empty and is filled during
runtime with datas from a file. The Programm is something like a data
converter.
So what i have right now is a Dataset inside Form1 which contains the data.
But i want to access the data also outside form1, from soem different
classes. The one way is to declare the dataset as static, but that not
good. So i'm looking for an implementation of the dataset which controls
the access to it and is threadsafe.

Am i completely wrong with my thoughts?

Assuming someone wants to access the data within a dataset from different
classes, do i have to pass the dataset each time as a parameter, is that
what OOP means?

Thank you in advance

Hans
 
C

Cor Ligthert [MVP]

Hans,

Just open in version 2003 a component and drag a dataset on that.

In version 2005 you can click on top of Visual Studio to make your dataset.

That both creates a dataset class.

Creating a dataset static, is a nice example how in my idea static would not
be used.

However that is just my opinion,

Cor
 
M

Mark Rae

The one way is to declare the dataset as static, but that not good.

Why not?
So i'm looking for an implementation of the dataset which controls
the access to it and is threadsafe.

Am i completely wrong with my thoughts?

It's difficult to say until you actually tell us what this dataset will
subsequently be used for.

Are you using it as the datasource for bound controls?

Why does it have to be a dataset? Why not a generic?
Assuming someone wants to access the data within a dataset from different
classes, do i have to pass the dataset each time as a parameter, is that
what OOP means?

Nothing to do with object-orient(at)ed programming per se... I have a
feeling getting yourself bogged down in nomenclature that you don't really
understand rather than taking a step back, focussing on the problem, and
coming up with a solution.

You mention that the dataset is created when Form1 is loaded - does that
mean that currently the dataset is created EVERY TIME Form1 is loaded? Is
that the problem?
 
H

Hans Greif

Am Sat, 9 Sep 2006 22:38:38 +0200 schrieb Hans Greif:
Hallo,

hello, i try to implement a global dataset - singleton based.

On www.bakterienforum.de i found a singleton implementation:

sealed class SingletonCounter
{
public int Counter = 0;

private SingletonCounter(){}
public static readonly SingletonCounter Instance = new
SingletonCounter();
}

Can i use this for implementing the global dataset and how can i do this?

Thank you in advance.

hans
Well, i got it. thank you
 

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