/*
Below is a stripped-down version of the class.
It is named singlestonstuff.cs
The usage pattern is:
1. Callers try to get the single instance of the class.
2. The class sets its DataSet property to new.
3. Caller makes multiple method calls to add info to the
DataSet property.
4. Caller requests the entire populated DataSet.
5. Caller disconnects.
I need to guarantee that every time (1) happens,
(2) also happens. Right now, (1) can happen and then
the caller can exit (from a crash, etc), and then when
a new caller appears they will get the same DataSet
populated by a earlier caller.
Is it possible for you to show how a delegate (with
events) can be hooked into this so the DataSet is
always given fresh for each caller?
Right now my workaround is for each caller to
get an instance of the class and then touch the
DsClear() method. I want DsClear to happen
internally with this class instead - can you
please show how this can be done with an event?
This compiles with
"csc /target:library singletonstuff.cs"
Thanks.
*/
using System;
using System.Data;
using System.Collections;
public sealed class DataSetStuffer
{
// Thread-safe usage without using locks
static readonly DataSetStuffer dssInstance =
new DataSetStuffer();
// Explicit static constructor tells compiler
// not to mark type as BeforeFieldInit
// Thanks to Jon at
http://www.yoda.arachsys.com/
static DataSetStuffer()
{
}
DataSetStuffer()
{
PrepDs();
}
public static DataSetStuffer GetInstance()
{
return dssInstance;
}
private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
DataSet outSet = _dataSet.Copy();
DsClear();
return outSet;
}
}
private void PrepDs()
{
string callerName =
new System.Diagnostics.StackFrame(3).GetMethod().Name;
DataTable dataTable = new DataTable();
dataTable.Columns.Add( "Something" ,
typeof(System.String) );
_dataSet.Tables.Add( dataTable );
}
public void TakeInfo( string clientInfo )
{
_dataSet.Tables[0].Rows.Add
( new Object[] { clientInfo } );
}
public void DsClear()
{
_dataSet = new DataSet();
PrepDs();
}
}