MongoDb C#普通属性自动转换为版本化属性
我有这个版本化属性的实现MongoDb C#普通属性自动转换为版本化属性,mongodb,mongodb-.net-driver,postsharp,Mongodb,Mongodb .net Driver,Postsharp,我有这个版本化属性的实现 public class VersionedProperty<T>: Dictionary<int, T>, IParentEntityTracker //where T:IComparable<T> { [BsonIgnore] public IVersionableEntity ParentEntity { get; set; } public VersionedProperty() {
public class VersionedProperty<T>: Dictionary<int, T>, IParentEntityTracker //where T:IComparable<T>
{
[BsonIgnore]
public IVersionableEntity ParentEntity { get; set; }
public VersionedProperty()
{
}
public VersionedProperty(IVersionableEntity parentEntity)
{
ParentEntity = parentEntity;
}
public T Value
{
set
{
var curVal = Value;
if (EqualityComparer<T>.Default.Equals(curVal, value))
return;
ParentEntity.AtLeastOneVersionedPropertyModified = true;
this[ParentEntity.Version + 1] = value;
}
get
{
var key = ParentEntity.Version + (ParentEntity.AtLeastOneVersionedPropertyModified ? 1 : 0);
var keys = Keys.Where(k => k<=key).ToList();
if (!keys.Any())
return default(T);
var max = keys.Max();
T res;
TryGetValue(max, out res);
return res;
}
}
}
public class Product
{
public VersionedProperty<Decimal> Price{get;set;}
}
过了一段时间,我意识到我犯了错误,我应该使用版本属性
public class VersionedProperty<T>: Dictionary<int, T>, IParentEntityTracker //where T:IComparable<T>
{
[BsonIgnore]
public IVersionableEntity ParentEntity { get; set; }
public VersionedProperty()
{
}
public VersionedProperty(IVersionableEntity parentEntity)
{
ParentEntity = parentEntity;
}
public T Value
{
set
{
var curVal = Value;
if (EqualityComparer<T>.Default.Equals(curVal, value))
return;
ParentEntity.AtLeastOneVersionedPropertyModified = true;
this[ParentEntity.Version + 1] = value;
}
get
{
var key = ParentEntity.Version + (ParentEntity.AtLeastOneVersionedPropertyModified ? 1 : 0);
var keys = Keys.Where(k => k<=key).ToList();
if (!keys.Any())
return default(T);
var max = keys.Max();
T res;
TryGetValue(max, out res);
return res;
}
}
}
public class Product
{
public VersionedProperty<Decimal> Price{get;set;}
}
公共类产品
{
公共版本属性价格{get;set;}
}
我希望将现有文档中的旧值自动反序列化到此版本化属性中,以避免对集合编写更新查询。
是否可能以某种方式干扰反序列化过程
这可能会有所帮助:我使用postsharp为AutoProperty创建版本化属性的空实例。我的postsharp特性还将父实体的引用分配给新创建的版本化属性的空实例的ParentEntity属性。您看过这些文档了吗:如果数据集很小,我仍然建议您修复数据。谢谢您的回复。是的,我不喜欢指定我的序列化程序。我想要简单的解决方案。是的,我可以修复数据,但是如果可以用一种简单的方式修复,我不希望写更新查询,因为这可能会对数据造成错误。我知道的唯一合理的方法是在我包含的链接中。在转换所有数据之前,它始终需要自定义序列化程序的额外开销。