Generics with singleton

G

Guest

Hi

Have a singleton class like this.

public sealed class ProductAdapter
{
private static readonly ProductAdapter instance = new
ProductAdapter();
private ProductAdapter(){}

public static ProductAdapter Instance
{
get
{
return instance;
}
}

public int Save()
{
return 0;
}
}

But there will be many more xxxAdapter classes so I wonder how I can use
generics to have a abstract AdapterBase class, like below, but as a
singleton.

public abstract class AdapterBase<T>
{
public abstract int Save(T t);

public abstract void Update(T t);

public abstract void Delete(int id);

public abstract void DeleteAll(int id);

public abstract T GetObject();

public abstract List<T> GetCollection();
}
 
J

Joanna Carter [TeamB]

"Senna" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Have a singleton class like this.
|
| public sealed class ProductAdapter
| {
| private static readonly ProductAdapter instance = new
| ProductAdapter();
| private ProductAdapter(){}
|
| public static ProductAdapter Instance
| {
| get
| {
| return instance;
| }
| }
|
| public int Save()
| {
| return 0;
| }
| }
|
| But there will be many more xxxAdapter classes so I wonder how I can use
| generics to have a abstract AdapterBase class, like below, but as a
| singleton.

I would use something like the following (not tested)

public class Adapter<T>
{
private static readonly Adapter<T> instance = new Adapter();

private Adapter(){}

public static Adapter<T> Instance
{
get
{
return instance;
}
}

public abstract int Save()
{
return 0;
}
}

Joanna
 
G

Guest

Hi Joanna

Thanks for helping. I'm on my way to a birthday party so I have to test your
code and respond to it later. :)

Cheers
 
G

Guest

Hi again

Get this error.

Error 1 c:\...\ProductAdapter.cs(61): error CS0513: 'Adapter<T>.Save()' is
abstract but it is contained in nonabstract class 'Adapter<T>'
 
J

Joanna Carter [TeamB]

"Senna" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Get this error.
|
| Error 1 c:\...\ProductAdapter.cs(61): error CS0513: 'Adapter<T>.Save()' is
| abstract but it is contained in nonabstract class 'Adapter<T>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

Joanna
 
J

Jon Skeet [C# MVP]

Joanna Carter said:
"Senna" <[email protected]> a écrit dans le message de
(e-mail address removed)...

| Get this error.
|
| Error 1 c:\...\ProductAdapter.cs(61): error CS0513: 'Adapter<T>.Save()'is
| abstract but it is contained in nonabstract class 'Adapter<T>'

Sorry, try this :

public virtual int Save()
{
return 0;
}

And the other change needed (when I tested it earlier :) is in the
field initialization:

private static readonly Adapter<T> instance = new Adapter<T>();

(The second <T> was missing before.)
 
G

Guest

Hi

Have been looking around and found this article.
http://www.codeproject.com/csharp/GenericSingleton.asp#xx1183285xx
And after some work I came up with this(below). It looks ok as far as I can
see, can you find anything wrong with it?

public abstract class AdapterBase<T>
{
public abstract int Save(T t);

public abstract void Update(T t);

public abstract void Delete(int id);

public abstract void DeleteAll(int id);

public abstract T GetObject();

public abstract List<T> GetCollection();
}

public class ProductAdapter : AdapterBase<ProductAdapter>
{
...
}

public class AdapterProvider<T> where T : new()
{
private AdapterProvider()
{
}

public static T Instance
{
get
{
return AdapterFactory.instance;
}
}

class AdapterFactory
{
static AdapterFactory()
{
}

internal static readonly T instance = new T();
}
}

public static class Adapter
{
public static ProductAdapter Product
{
get
{
return AdapterProvider<ProductAdapter>.Instance;
}
}
}
 
G

Guest

And to call this I would use:
ProductAdapter pa = Adapter.Product;
Response.Write(pa.Count.ToString());
 
G

Guest

Didn't actually test the code. The errors in it are...

There is also a Product class:
public class Product
{
public int Save()
{
return Adapter.Product.Save(this);
}
}

The ProductAdapter is of type Product:
public class ProductAdapter : AdapterBase<Product>
{...}

And to call it:
Product p = new Product();
Response.Write(p.Save().ToString());

So, those this setup look ok?
 
J

Joanna Carter [TeamB]

"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de (e-mail address removed)...

And the other change needed (when I tested it earlier :) is in the
field initialization:

private static readonly Adapter<T> instance = new Adapter<T>();

(The second <T> was missing before.)

Yup, well caught; I was dashing out of the door when I wrote the sample
without even using VS :)

Joanna
 
J

Jon Skeet [C# MVP]

Joanna Carter said:
Yup, well caught; I was dashing out of the door when I wrote the sample
without even using VS :)

I don't usually use VS for newsgroup programs :)

(That's not to say I always remember to test compile it either though -
not by a long chalk!)
 

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