hello sorry my english is not good,I hope you can understand me.. I'm using this DbEntities and other classes everywhere
How do I use this classes global? I don't want to create continuously instance, example Can I do this with single pattern? or global Constructor ? or What should I do?
你好抱歉我的英语不好,我希望你能理解我..我正在使用这个DbEntities和其他类到处如何使用这个全局类?我不想连续创建实例,例子我可以使用单一模式吗?还是全局构造函数?或者我该怎么办?
thank you
谢谢
namespace Crewnetix.Entity.Concrete
{
public class Crew
{
DbEntities _DbEntities = new DbEntities (); >> object context db entities
CertificateItem _Certificate = new CertificateItem ();
WorldProcess _WorldProcess = new WorldProcess ();
List<CertificateItem> _Certificates = new List<CertificateItem>();
CountryCity.CountryItem _Country = new CountryCity.CountryItem();
}
}
namespace Docnetix.Entity.Concrete
{
public class Document
{
DbEntities _DbEntities = new DbEntities ();
CertificateItem _Certificate = new CertificateItem ();
WorldProcess _WorldProcess = new WorldProcess ();
List<CertificateItem> _Certificates = new List<CertificateItem>();
CountryCity.CountryItem _Country = new CountryCity.CountryItem();
}
namespace accountingnetix.Entity.Concrete
{
public class accounting
{
DbEntities _DbEntities = new DbEntities ();
CertificateItem _Certificate = new CertificateItem ();
WorldProcess _WorldProcess = new WorldProcess ();
List<CertificateItem> _Certificates = new List<CertificateItem>();
CountryCity.CountryItem _Country = new CountryCity.CountryItem();
}
}
example I want to it..I thought it
例子我想要它......我想到了
namespace Global
{
public static class GlobalInstance
{
public static object GetCreateInstance(object class); >> class name
{
...code
return newInstance;
}
}
}
}
namespace crewnetix
{
public class crew
{
object instance = GetCreateInstance(CertificateItem) >> class name
}
}
}
2 个解决方案
#1
2
Singleton Is perfect for your goal:
Singleton非常适合您的目标:
//1) make your constructor private
//2) add this code:
private static Your_Class_Name _Instance;
public static Your_Class_Name Instance
{
get
{
if (_Instance == null)
_Instance = new Your_Class_Name();
return _Instance;
}
}
#2
1
You could make the classes static? and mark each member you also would like to be global / static , e.g:
你可以让这些类是静态的吗?并标记每个成员您也希望是全局/静态的,例如:
public static class A
{
public static string GetSomething()
{
// get something and return it
}
public static string AProperty
{
// getter and setter
}
}
using the class;
使用班级;
A.GetSomething()
var a = A.AProperty;
you would not need to create an instance.
你不需要创建一个实例。
#1
2
Singleton Is perfect for your goal:
Singleton非常适合您的目标:
//1) make your constructor private
//2) add this code:
private static Your_Class_Name _Instance;
public static Your_Class_Name Instance
{
get
{
if (_Instance == null)
_Instance = new Your_Class_Name();
return _Instance;
}
}
#2
1
You could make the classes static? and mark each member you also would like to be global / static , e.g:
你可以让这些类是静态的吗?并标记每个成员您也希望是全局/静态的,例如:
public static class A
{
public static string GetSomething()
{
// get something and return it
}
public static string AProperty
{
// getter and setter
}
}
using the class;
使用班级;
A.GetSomething()
var a = A.AProperty;
you would not need to create an instance.
你不需要创建一个实例。