MongoDB 的单例
Singleton for MongoDB
我应该为 MongoDB 使用单例模式吗?我目前正在使用 Photon Engine 和 MongoDB.
为回合制游戏构建游戏服务器
public sealed class GSEntities
{
#region Fields
public IMongoClient Client;
public IMongoDatabase Database;
private static GSEntities _instance;
private static readonly Object sync = new object();
public static GSEntities Instance
{
get
{
if (_instance == null)
{
lock (sync)
{
if (_instance == null)
_instance = new GSEntities();
}
}
return _instance;
}
}
}
这很好,还是我应该使用其他模式,如 Repository?
感谢您的帮助! :)
It's good or should i use other pattern like Repository ?
一般来说,您应该在需要时使用模式。 不有空的话,要不然看起来很爽,要不就不疼了。
那么问问你自己,你需要一个单身人士吗?我的猜测是没有。这也可能是您碰巧只有一个实例的普通 class。
您需要存储库模式吗?我的猜测也不会。描述说:
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
只有你能决定。同样,在需要时实施,而不是当您喜欢或其他人认为它很酷时。
我应该为 MongoDB 使用单例模式吗?我目前正在使用 Photon Engine 和 MongoDB.
为回合制游戏构建游戏服务器public sealed class GSEntities
{
#region Fields
public IMongoClient Client;
public IMongoDatabase Database;
private static GSEntities _instance;
private static readonly Object sync = new object();
public static GSEntities Instance
{
get
{
if (_instance == null)
{
lock (sync)
{
if (_instance == null)
_instance = new GSEntities();
}
}
return _instance;
}
}
}
这很好,还是我应该使用其他模式,如 Repository?
感谢您的帮助! :)
It's good or should i use other pattern like Repository ?
一般来说,您应该在需要时使用模式。 不有空的话,要不然看起来很爽,要不就不疼了。
那么问问你自己,你需要一个单身人士吗?我的猜测是没有。这也可能是您碰巧只有一个实例的普通 class。
您需要存储库模式吗?我的猜测也不会。描述说:
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
只有你能决定。同样,在需要时实施,而不是当您喜欢或其他人认为它很酷时。