dimanche 2 juillet 2017

Why are static methods not available in Singleton Instance?

I Create Generic Type Class Of name SingletonGenerator<> For Implement Singelton Design Pattern So The above code is it the class:

SingletonGenerator.cs

   public class SingletonGenerator<T> where T : class, new()
   {
      private static readonly Lazy<T> _instance =
        new Lazy<T>(() => new T(), LazyThreadSafetyMode.ExecutionAndPublication);

    private SingletonGenerator()
    {
    }

    public static T Instance => _instance.Value;

    }

And Create Other Class For Get Instance:

AppDb.cs

    public class AppDbContext
    {
      public string Database { get; set; }

      private static string ConnectionString { get; set; }

      public static void Send()
      {

      }

      public void Go()
      {
      }

     }

In Program.cs

    class Program
    {
     static void Main(string[] args)
     {
         var context = SingletonGenerator<AppDbContext>.Instance;

         var database = context.Database; // is available 
         var connection = context.ConnectionString; //is not available

         context.Go(); // is available 
         context.Send(); // is not available
     }
    }

My Question is Why are static methods not available in Singleton Instance?

And My Code is Correct?

Aucun commentaire:

Enregistrer un commentaire