c# separate class for storage

2019-07-24 17:44发布

I use more than one class and I need a... lets say Global storage for all the class and method. Is it the right way to create a static class for storage?

public static class Storage
    {
        public static string filePath { get; set; }
    }

Or is there other ways to do it?

标签: c# class storage
5条回答
萌系小妹纸
2楼-- · 2019-07-24 18:05

Applying Singleton to your original class:

public class Storage
{
   private static Storage instance;
   private Storage() {}
   public static Storage Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Storage();
         }
         return instance;
      }
   }
   public string FilePath { get; set; }
}

usage:

string filePath = Storage.Instance.FilePath;
查看更多
beautiful°
3楼-- · 2019-07-24 18:13

You should have a look at the repository pattern:

http://martinfowler.com/eaaCatalog/repository.html

One way of implementing this pattern is through the use of ORM's s.a. NHibernate:

https://web.archive.org/web/20110503184234/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/10/08/the-repository-pattern.aspx

查看更多
可以哭但决不认输i
4楼-- · 2019-07-24 18:15

I love seeing that implementation of singleton in C#.

public class Singleton
{
    public static readonly Singleton instance;

    static Singleton()
    {
        instance = new Singleton();
    }

    private Singleton()
    {
       //constructor...
    }
}

C# guarantees that your instance wont be overriden and your static constructor guarantees that you WILL have your static property instantiated before the first time it's used.

Bonus: It's threadsafe as per language design for static constructors, no double checked locking :).

查看更多
狗以群分
5楼-- · 2019-07-24 18:17

If you really need to make your example a singleton then here is how you do it.

public class StorageSingleton
{
    private static readonly StorageSingleton instance;

    static StorageSingleton() { 
         instance = new Singleton();
    }
    // Mark constructor as private as no one can create it but itself.
    private StorageSingleton() 
    { 
        // For constructing
    }

    // The only way to access the created instance.
    public static StorageSingleton Instance
    {
        get 
        {
            return instance;
        }
    }        

    // Note that this will be null when the instance if not set to
    // something in the constructor.
    public string FilePath { get; set; }
}

The way to call and set the singleton is the following:

// Is this is the first time you call "Instance" then it will create itself
var storage = StorageSingleton.Instance;

if (storage.FilePath == null)
{
    storage.FilePath = "myfile.txt";
}

Alternatively you can add into the constructor the following to avoid null reference exception:

// Mark constructor as private as no one can create it but itself.
private StorageSingleton() 
{ 
    FilePath = string.Empty; 
}

Word of warning; making anything global or singleton will break your code in the long run. Later on you really should be checking out the repository pattern.

查看更多
闹够了就滚
6楼-- · 2019-07-24 18:23

You could consider using the Singleton design pattern: Implementing Singleton in c#

eg.

using System;
public class Singleton
{
   private static Singleton instance;
   private Singleton() {}
   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
查看更多
登录 后发表回答