Generic abstract base class for singleton (C #) - cannot create an instance of private Lazy

Currently, I have a collection of 6 or 7 singletones, all of which do almost the same thing (see the method Forin the example below), but with a different internal database query and returning a collection of different objects (so the analysis of the database results is different in each singlet )

Therefore, using this question as my base, I tried to create an abstract base class in C # for these singletones.

There are similar questions on SO, but none of them implements Lazywhich I want.

While I have it

public abstract class SingletonBase<T> where T : class, new()
{
    private static Lazy<SingletonBase<T>> _lazy;
    private static readonly object _lock = new object();

    public static SingletonBase<T> Instance
    {
        get
        {
            if (_lazy != null && _lazy.IsValueCreated)
            {
                return _lazy.Value;
            }

            lock (_lock)
            {
                if (_lazy != null && _lazy.IsValueCreated)
                {
                    return _lazy.Value;
                }

                *****  this is the problem line  *****
                _lazy = new Lazy<SingletonBase<T>>(new T());
            }

            return _lazy.Value;
        }
    }

    public abstract IEnumerable<T> For(string systemLangCode);
}

However, the problem occurs in the line

_lazy = new Lazy<SingletonBase<T>>(new T());

Visual Studio tells me: "Cannot resolve Lazy <T> constructor."

, Lazy<SingletonBase<T>>, ?

+4
2

, , , , . .

.

public abstract class SingletonBase<S, T> where S : class, new()
{
    protected static readonly Dictionary<string, List<T>> Dictionary = new Dictionary<string, List<T>>();
    private static readonly S _instance = new S();
    private static readonly object _lock = new object();

    public static S Instance
    {
        get
        {
            lock (_lock)
            {
                return _instance;
            }
        }
    }

    public IEnumerable<T> For(string systemLangCode)
    {
        systemLangCode = systemLangCode.ToLower();

        if (!Dictionary.ContainsKey(systemLangCode))
        {
            PopulateDictionary(systemLangCode);
        }

        return Dictionary.FirstOrDefault(d => d.Key == systemLangCode).Value;
    }

    protected abstract void PopulateDictionary(string systemLangCode);
}

, , , S, T. , , , , systemLangCode , List<T>, Dictionary systemLangCode .

, , .

.

public sealed class Languages : SingletonBase<Languages, Language>
{
    protected override void PopulateDictionary(string systemLangCode)
    {
        var languages = new List<Language>();

        // some DB stuff

        Dictionary.Add(systemLangCode, languages);
    }
}

, , . , singleton, , , , , ..

:

var langsDe = Languages.Instance.For("de");
var langsEn = Languages.Instance.For("en");

var langsDe = Languages.Instance.For("de");

Dictionary.

+1

?

.

, _lazy Lazy<SingletonBase<T>>. , T .

- , SingletonBase<T> -derived ( SingletonBase<T> ). , . , getter .

?
.

-, , Lazy<T>? Lazy<T>:

var singleton = new Lazy<YourClass>(true);

var singleton = new Lazy<YourClass>(SomeMethodThatCreatesYourClass, true);

UPD.

:

  • ;
  • T .

. factory ( , , ):

abstract class Factory<T>
{
    public abstract IEnumerable<T> For(string systemLangCode);
}

, :

class Factory1 : Factory<YouClass1> {}
class Factory2 : Factory<YouClass2> {}
// etc

, , , Lazy<T>, :

static class Singletons
{
    private static readonly Lazy<Factory1> factory1;
    private static readonly Lazy<Factory2> factory2;

    static Singletons()
    {
        factory1 = new Lazy<Factory1>(true); // or Lazy<Factory1>(() => /* */, true)
        factory2 = new Lazy<Factory2>(true); // or Lazy<Factory2>(() => /* */, true)
    }

    public Factory1 Factory1
    {
        get { return factory1.Value; }
    }
    public Factory2 Factory2
    {
        get { return factory2.Value; }
    }
    // etc
}

:

Singletons.Factory1.For("ab-CD")
Singletons.Factory2.For("ef-GH")

Lazy<T> - . true - . , , .

0

All Articles