2009-08-14 3 views
1

나는 하나 이상의 클래스를 사용하는데 ... 나는 모든 클래스와 메소드를위한 전역 저장소라고 말할 수있다. 저장 용 정적 클래스를 만드는 것이 올바른 방법입니까?저장을위한 C# 분리 된 클래스

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

다른 방법이 있습니까?

+0

저장하시는 것이 어떻습니까? –

+3

개체와 메서드에 액세스 할 수있는 전역 변수가 필요하다고 말합니까? 그렇다면 싱글 톤을 사용하여 조사 할 수 있습니다. –

+0

@Charlie Salts 감사합니다. –

답변

3

당신이 그것을 어떻게 당신이 정말로 여기에 다음 예 싱글을 할 필요가있다 합니다.

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; } 
} 

전화 싱글을 설정하는 방법은 다음과 같은 : 또는

// 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"; 
} 

당신이 null 참조 예외를 방지하기 위해 다음과 같은 생성자에 추가 할 수의

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

말씀을 경고; 글로벌 또는 싱글 톤을 만드는 것은 장기적으로 코드를 깨뜨릴 것입니다. 나중에 저장소 패턴을 확인해야합니다. 원래 클래스에 싱글을 적용

+0

감사합니다. 매우 자세한 예입니다. –

1

리포지토리 패턴을 살펴이 있어야합니다

이 패턴을 구현하는 http://martinfowler.com/eaaCatalog/repository.html

한 가지 방법은 ORM의 사용을 통해입니다 S.A. NHibernate에 :

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

+0

고마워요, 지금보고 있습니다. –

+0

그러나 당신의보기를 위해 나는 그것을 너무 많이 생각한다. 나는 그런 저장 장치가 전혀 필요 없다. –

3

당신은 단일 디자인 패턴을 사용하여 고려할 수 : Implementing Singleton in c#

예.

using System; 
public class Singleton 
{ 
    private static Singleton instance; 
    private Singleton() {} 
    public static Singleton Instance 
    { 
     get 
     { 
     if (instance == null) 
     { 
      instance = new Singleton(); 
     } 
     return instance; 
     } 
    } 
} 
0

:

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; } 
} 

사용 :

string filePath = Storage.Instance.FilePath; 
0

나는 C#에서 싱글 톤의 구현을 보는 것을 좋아한다. 당신이 당신의 정적 속성이 사용되는 처음 전에 인스턴스화해야합니다

인스턴스 늘 오버라이드 (override) 할 것을
public class Singleton 
{ 
    public static readonly Singleton instance; 

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

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

C#을 보장하고 정적 생성자 보장합니다.

보너스 : 정적 생성자에 대해 언어마다 쓰레드 세이프 (threadafe safe)이며, 이중 체크 잠금이 아닙니다.

관련 문제