我刚刚开始玩MongoDB(C#),并尝试从实体框架移植一个存储库.我正在使用官方的C#驱动程序1.0.现在我做了这样的事情:
internal class MongoContext { public MongoContext(string constring) { MongoServer server = MongoServer.Create(constring); this.myDB = server.GetDatabase("MyDB"); BsonClassMap.RegisterClassMap<VoyageNumber>(cm => { cm.MapField<string>(p => p.Id); }); BsonClassMap.RegisterClassMap<Schedule>(cm => { cm.MapField<DateTime>(p => p.EndDate); cm.MapField<DateTime>(p => p.StartDate); }); BsonClassMap.RegisterClassMap<Voyage>(cm => { cm.MapIdField<VoyageNumber>(p => p.VoyageNumber); cm.MapField<Schedule>(p => p.Schedule); }); } private MongoDatabase myDB; public MongoDatabase MyDB { get { return this.myDB; } } }
我会继续执行这样的Repository:
public class MongoVoyageRepository : IVoyageRepository { private readonly MongoContext context; public MongoVoyageRepository(string constring) { this.context = new MongoContext(constring); } public void Store(Domain.Model.Voyages.Voyage voyage) { MongoCollection<Voyage> mongoVoyages = context.MyDB.GetCollection<Voyage>("Voyages"); //store logic... } }
现在我想知道,如果在性能方面实例化这样的“上下文”是一个很好的决定.将BsonClass地图放在那里是否有意义?
谢谢您的意见.
解决方法@H_404_13@
我想这是在创建存储库类时注册类映射是没有意义的.由于MongoDB C#驱动程序管理与MongoDB内部的连接,在我看来,最好在应用程序启动之前创建MongoServer并注册类映射一次,然后再使用它.
我正在使用单例来创建MongoServer只有一次
public class MongoRead : MongoBase
{
public MongoRead(MongoServer server)
: base(server)
{
}
public override MongoDatabase Database
{
get { return Server.GetDatabase("myDb"); }
}
public MongoCollection Logs
{
get { return Database.GetCollection("logs"); }
}
private static MongoRead _instance = null;
public static MongoRead Instance
{
get
{
if (_instance == null)
{
_instance = RegisterMongoDb();
}
return _instance;
}
}
private static MongoRead RegisterMongoDb()
{
var readServer = MongoServer.Create(connectionString);
var read = new MongoRead(readServer);
var myConventions = new ConventionProfile();
myConventions.SetIdMemberConvention(new NoDefaultPropertyIdConvention());
BsonClassMap.RegisterConventions(myConventions,t => true);
return read;
}
}
所以你也可以在你的存储库中使用上面的类:
public class MongoVoyageRepository : IVoyageRepository
{
private readonly MongoRead context
{
get { return MongoRead.Instance; }
};
public MongoVoyageRepository()
{
}
public void Store(Domain.Model.Voyages.Voyage voyage)
{
MongoCollection<Voyage> mongoVoyages =
context.Database.GetCollection<Voyage>("Voyages");
//store logic...
}
}
我正在使用单例来创建MongoServer只有一次
public class MongoRead : MongoBase { public MongoRead(MongoServer server) : base(server) { } public override MongoDatabase Database { get { return Server.GetDatabase("myDb"); } } public MongoCollection Logs { get { return Database.GetCollection("logs"); } } private static MongoRead _instance = null; public static MongoRead Instance { get { if (_instance == null) { _instance = RegisterMongoDb(); } return _instance; } } private static MongoRead RegisterMongoDb() { var readServer = MongoServer.Create(connectionString); var read = new MongoRead(readServer); var myConventions = new ConventionProfile(); myConventions.SetIdMemberConvention(new NoDefaultPropertyIdConvention()); BsonClassMap.RegisterConventions(myConventions,t => true); return read; } }
所以你也可以在你的存储库中使用上面的类:
public class MongoVoyageRepository : IVoyageRepository { private readonly MongoRead context { get { return MongoRead.Instance; } }; public MongoVoyageRepository() { } public void Store(Domain.Model.Voyages.Voyage voyage) { MongoCollection<Voyage> mongoVoyages = context.Database.GetCollection<Voyage>("Voyages"); //store logic... } }