我有以下界面:
public interface IChartDefinitionBasic
{
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
string Id { get; }
IMetricDefinition MetricDefinition { get; }
}
...以及用于测试目的的简单实现:
class DummyDefinitionBasic : IChartDefinitionBasic
{
internal DummyDefinitionBasic(string metricName)
{
MetricDefinition = new DummyMetricDefinition(metricName);
Id = ObjectId.GenerateNewId().ToString();
}
[BsonIgnoreIfDefault]
[BsonRepresentation(BsonType.ObjectId)]
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
public string Id { get; private set; }
public IMetricDefinition MetricDefinition { get; private set; }
}
下面的测试方法在到达_service.Delete(_dto)
行时出现异常(“无法确定c => c.Id的序列化信息”):
[TestClass]
public class MongoChartServiceTest
{
private readonly MongoChartService _service = new MongoChartService();
private readonly IChartDefinitionBasic _dto = DummyDefinitionGenerator.GetDummyDefinitionBasic();
[TestMethod, TestCategory("MongoService")]
public void MongoChartService_CRD_ExecutesSuccessfully()
{
_service.Create(_dto);
IChartDefinitionBasic dto = null;
while (dto == null)
{
var dtos = _service.GetAllCharts();
dto = dtos.SingleOrDefault(d => d.Id == _dto.Id);
}
_service.Delete(_dto);
while (dto != null)
{
var dtos = _service.GetAllCharts();
dto = dtos.SingleOrDefault(d => d.Id == _dto.Id);
}
}
}
删除方法实现如下:
public async void Delete(IChartDefinitionBasic instance)
{
var builder = new FilterDefinitionBuilder<IChartDefinitionBasic>();
var filter = builder.Eq(c => c.Id, instance.Id);
await _collection.DeleteOneAsync(filter);
}
我发现了这个link,但它似乎没有帮助。而不是c =&gt; c.Id,我取代了“Id”,但现在测试从未退出;无论如何,我更喜欢使用Expression,因为它更干净(我暂时停留在C#5.0上)。有人可以解释为什么这不起作用,有什么必要修复它?