gpt4 book ai didi

c# - MongoDB 的单例

转载 作者:可可西里 更新时间:2023-11-01 10:41:06 25 4
gpt4 key购买 nike

我应该为 MongoDB 使用单例模式吗?我目前正在使用 Photon Engine 和 MongoDB 为 turnbase 游戏构建游戏服务器。

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 ?

一般来说,您应该在需要时使用模式。 当你有时间的时候,或者它看起来很酷或者它不会伤害。

那么问问自己,您需要一个单例吗?我的猜测是没有。这也可以是您碰巧只有一个实例的普通类。

需要存储库模式吗?我的猜测也不会。描述说:

Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

只有你能决定。再次重申,在需要时实现不要当您喜欢或其他人认为它很酷时。

关于c# - MongoDB 的单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42717054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com