我有一个关于Singleton模式和MEF的问题。我是实施MEF插件的新手,我还没有找到答案。
是否可以通过MEF实现的插件只提供一个类的实例?
我以前的课是这样的:
#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
有人能给我举个例子吗?
是的,可以这样做。
默认情况下,MEF在填充导入时将始终返回类的同一实例。所以从技术上讲,如果你想让它成为一个单例,你不必做任何事情。这就是MEF所说的共享创造政策。
如果你不希望你的导入来自同一个实例,你需要指定它,或者在你的属性中同样指定:
[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
public MyClass : IMyInterface
或者,您可以覆盖自己的CompositionContainer,以便在默认情况下创建NonShared实例。
请注意,您还可以明确指定您想要共享创建策略(singletons):
[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public MyClass : IMyInterface
{
public MyClass() { } // you can have a public ctor, no need to implement the singleton pattern
}
但这并不是必须的,因为Shared(singleton)已经是默认值了。
以下是MEF文件的链接:http://mef.codeplex.com/wikipage?title=Parts%20Lifetime这就解释了我刚才所说的。你也可以通过搜索找到关于这个主题的博客:"MEF创建政策"。
非常糟糕的做法!使用静态构造函数:保证只执行一次
请注意,对于您创建的每个T
,CLR将复制新T
的静态数据。
因此,您正在为所使用的每个T
类型创建一个singleton。
单例的最佳方式是这样的:
public class Logger {
public static readonly Logger Instace;
static Logger() {
Instace = new Logger();
}
}