我对 ObjectId 表示有一个小问题。这是示例代码:
public class EntityWithObjectIdRepresentation
{
public string Id { get; set; }
public string Name { get; set; }
}
[Test]
public void ObjectIdRepresentationTest()
{
BsonClassMap.RegisterClassMap<EntityWithObjectIdRepresentation>(cm =>
{
cm.AutoMap();
cm.GetMemberMap(x => x.Id).SetRepresentation(BsonType.ObjectId);
});
var col = db.GetCollection("test");
var entity = new EntityWithObjectIdRepresentation();
col.Insert(entity);
Assert.IsNotNullOrEmpty(entity.Id); // Ok, Id is generated automatically
var res = col.FindOneByIdAs<EntityWithObjectIdRepresentation>(entity.Id);
Assert.IsNotNull(res); // Fails here
}
上面的代码适用于
var res = col.FindOneByIdAs<EntityWithObjectIdRepresentation>(ObjectId.Parse(entity.Id));
但是我想要的是在通用存储库类中抽象这些东西,所以一般我不知道这个Id是否必须转换为ObjectId。我可以从 BsonClassMap 中检索此类信息吗?
以下代码也可以工作,但由于 LINQ 表达式转换,根据基准,它几乎慢了 15 倍:
var res = col.AsQueryable().FirstOrDefault(x => x.Id.Equals(id));
好的,我将包含项目中的实际代码:
public class MongoDbRepository<T, T2> : IRepository<T, T2>
where T : IEntity<T2> // T - Type of entity, T2 - Type of Id field
{
protected readonly MongoCollection<T> Collection;
public MongoDbRepository(MongoDatabase db, string collectionName = null)
{
MongoDbRepositoryConfigurator.EnsureConfigured(db); // Calls BsonClassMap.RegisterClassMap, creates indexes if needed
if (string.IsNullOrEmpty(collectionName))
{
collectionName = typeof(T).Name;
}
Collection = db.GetCollection<T>(collectionName);
}
public T GetById(T2 id)
{
using (Profiler.StepFormat("MongoDB: {0}.GetById", Collection.Name))
{
// TODO Use FindOneByIdAs<T>
return Collection.AsQueryable().FirstOrDefault(x => x.Id.Equals(id));
}
}
// some more methods here ...
}
// ...
var repo = new MongoDbRepository<SomeEntity,string>(); // Actually it's injected via DI container
string id = "510a9fe8c87067106c1979de";
// ...
var entity = repo.GetById(id);