2012-01-25 3 views
9

シングルトンパターンとMEFに関する質問があります。 MEFプラグインを実装するのが初めてで、答えが見つかりませんでした。C#シングルトンパターンとMEF

MEF実装プラグインを使用してクラスのインスタンスを1つだけ提供できますか?

私の古いクラスは、このようなものです:


    #region Singleton 
    /// 
    /// This class provide a generic and thread-safe interface for Singleton classes. 
    /// 
    /// The specialized singleton which is derived 
    /// from SingletonBase<T> 
    public abstract class Base where T : Base 
    { 
    /* the lock object */ 
    private static object _lock = new object(); 

    /* the static instance */ 
    private static T _instance = null; 
    /// 
    /// Get the unique instance of . 
    /// This property is thread-safe! 
    /// 
    public static T Instance 
    { 
     get 
     { 
     if (_instance == null) 
     { 
      lock (_lock) 
      { 
      if (_instance == null) 
      { 
       /* Create a object without to use new (where you need a public ctor) */ 
       object obj = FormatterServices.GetUninitializedObject(typeof(T)); 
       if (obj != null) // just 4 safety, but i think obj == null shouldn't be possible 
       { 
       /* an extra test of the correct type is redundant, 
       * because we have an uninitialised object of type == typeof(T) */ 
       _instance = obj as T; 
       _instance.Init(); // now the singleton will be initialized 
       } 
      } 
      } 
     } 
     else 
     { 
      _instance.Refresh(); // has only effect if overridden in sub class 
     } 
     return _instance; 
     } 
    } 


    /// 
    /// Called while instantiation of singleton sub-class. 
    /// This could be used to set some default stuff in the singleton. 
    /// 
    protected virtual void Init() 
    { } 

    /// 
    /// If overridden this will called on every request of the Instance but 
    /// the instance was already created. Refresh will not called during 
    /// the first instantiation, for this will call Init. 
    /// 
    protected virtual void Refresh() 
    { } 
    } 
    #endregion 

    #region class 
    public class xy : Base 
    { 
    private bool run; 

    public xy() 
    { 
     this.run = false; 
    } 

    public bool isRunning() 
    { 
     return this.run; 
    } 

    public void start() 
    { 
     // Do some stuff 
     this.run = true; 
    } 
    } 
    #endregion 

は、誰かが私の例を提供することはできますか?

+1

[MEFはシングルトンパターンに何らかの価値を貸していますか?](http://stackoverflow.com/questions/4484619/does-mef-lend-any-value-to-the-singleton-pattern) – IAbstract

+0

申し訳ありませんが、質問には関係ありませんが、 'Lazy 'を不必要に再実装しているようです。余計なコード行! – binki

答えて

18

はい、そうすることが可能です。

デフォルトでは、MEFはインポートを満たすときに常にクラスの同じインスタンスを返します。技術的には、シングルトンにしたい場合は何もする必要はありません。これはMEFが共有作成ポリシーと呼ぶものです。

あなたの輸入は同じインスタンスから来るしたくない場合は、あなたが同様にあなたの属性のいずれかで、それを指定する必要があります。

[Import(RequiredCreationPolicy = CreationPolicy.NonShared)] 
public MyClass : IMyInterface 

それともそれは非共有インスタンスを作成しますので、あなた自身のCompositionContainerを上書きすることができますデフォルトではあなたはまた、明示的に共有の作成方針(シングルトン)をすることを指定することができます

注:

[Import(RequiredCreationPolicy = CreationPolicy.Shared)] 
public MyClass : IMyInterface 
{ 
    public MyClass() { } // you can have a public ctor, no need to implement the singleton pattern 
} 

しかし、共有(シングルトン)はすでにデフォルト値であるとして、それは必要ありません。

ここに私が話したことを説明しているhttp://mef.codeplex.com/wikipage?title=Parts%20LifetimeのMEFのドキュメントへのリンクがあります。また、「MEF作成ポリシー」を検索することで、件名に関するブログを見つけることもできます。

+0

ありがとう!できます! – subprime

+0

GEFは、MEFがCreationPolicyに関して動作する方法については絶対に正しいです。私が指摘したいのは、シングルトンパターンで得られる1インスタンス保証は、誰かがまだMEF以外の別のインスタンスを新しくできるので、厳密にここで実施されるわけではないということです。 – MatrixManAtYrService

+0

@MatrixManAtYrService良い点! – Gilles

1

非常に悪い練習です!静的コンストラクタを使用します。 は、作成する各Tため、CLRは新しいTのための静的なデータがコピーされますので、予めご了承ください正確に一度

を実行することが保証されています。

したがって、使用するTタイプごとにシングルトンを作成しています。

シングルトンのための最善の方法は次のようなものです:

public class Logger { 
    public static readonly Logger Instace; 

    static Logger() { 
     Instace = new Logger(); 
    } 
} 
+0

こんにちは、非常にquikly答えをありがとうが、私はMEFでそれを使用することができますか? – subprime

+0

これはMEFで使用できます。通常のように、MEF FIRSTが静的コンストラクタが呼び出されたクラスを初期化するときと同じように、すべてをMEFします。 –

関連する問題