پشتیبانی توکار از ایجاد کلاسهای Singleton از دات نت 4 به بعد
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۸/۰۲ ۱۲:۴۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class WrongSingleton
{
static WrongSingleton _instance;
WrongSingleton()
{
}
public static WrongSingleton Instance
{
get { return _instance ?? (_instance = new WrongSingleton()); }
}
} public class Container
{
// ...
}
public static class ObjectFactory
{
private static readonly Lazy<Container> _containerBuilder =
new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);
public static Container Container
{
get { return _containerBuilder.Value; }
}
private static Container defaultContainer()
{
return new Container();
}
} public sealed class LazySingleton
{
private static readonly Lazy<LazySingleton> _instance =
new Lazy<LazySingleton>(() => new LazySingleton(), LazyThreadSafetyMode.ExecutionAndPublication);
private LazySingleton()
{
}
public static LazySingleton Instance
{
get { return _instance.Value; }
}
} .....
private int _myVar;
public int MyProperty
{
get { return _myVar; }
set { _myVar = value; }
}
..... LazySingleton.Instance.MyProperty = 1000; OR var foo = LazySingleton.Instance.MyProperty;