Typed Dataset Factory for Singelton

G

Guest

I have a specific need to implement a singleton typed dataset and still use
drag and drop binding via project data source.
Specifically, I have a dll assembly that contains the typed dataset. When I
reference it in my WinForms app, I want all forms and user-controls
referencing the dataset type to point to only one instance at runtime.
Please no guesses! I need answers.
Thanks.
 
F

Fritz Francis

Jack said:
I have a specific need to implement a singleton typed dataset and still use
drag and drop binding via project data source.
Specifically, I have a dll assembly that contains the typed dataset. When
I
reference it in my WinForms app, I want all forms and user-controls
referencing the dataset type to point to only one instance at runtime.
Please no guesses! I need answers.
Thanks.

Are you using .NET 2.0 or .NET 1.1? Also are you asking how to implement a
singleton class or a factory class? One of the problems I have seen in .NET
2.0 is that you can not hide the public constructor that the designer
creates for the strongly-typed dataset which is a requirement to implement a
singleton class. Regardless here are the steps to create a singleton class.
(If you are using .NET 2.0, I suggest you do this in a partial class.)

1. IN .NET 1.1, hide the constructor that the designer creates by declaring
it as private.

2. Declare a private static (or shared) member variable.

3. Declare a public static method that returns the DataSet.

4. Implement th static method such that it checks to see if the static
member is null. If the static member is null, then set it to an instance of
the dataset. If it is not null, return the instance that was previously
created.

Here is the sample code.

Public Class MyDataSet

Private Shared m_ds As MyDataSet

Public Shared Function GetInstance As MyDataSet

If m_ds Is Nothing Then
m_ds = New MyDataSet
End If
Return m_ds

End Sub

End Class

You can still use drag and drop binding, but you will have to replace the
code where the designer instantiates the DataSet to call the GetInstance
method.

Regards,

Fritz
 
G

Guest

Fritz,

Thank you for the response, but this is not the answer I am looking for. Let
me clarify:
I am using .NET 2.0 with C#. I know how to implement Singleton and how to do
it for a typed dataset.
What I am looking for is a technique to overload the "new" operator or
provide a mechanism for the IDE to generate code for the factory method when
drag-and-drop usage of typed dataset tables are used.
Sure I can replace the IDE generated code with the factory method for
runtime, but this breaks form serialization at design time, and I would have
to replace the code every time I want to use the form designer.
 
F

Fritz Francis

Ok I see what you mean now. Unfortunately, that's the price you pay if you
use the designer and mix the data layer with the UI. I don't think it is
possible but if you find the answer I would be interested in knowing it as
well.

Regards,

Fritz
 

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