gpt4 book ai didi

MongoDB 类有参数但没有配置

转载 作者:行者123 更新时间:2023-12-04 10:26:56 26 4
gpt4 key购买 nike

我有一个类,其中包含一些我想使用 mongodb 存储的只读属性。

用户.cs

public class User: ValueObject<User>
{
public UserId Id { get; }
public string Firstname { get; }
public string Lastname { get; }

public User(UserId id, string firstname, string lastname)
{
Id = new UserId(id.Id);
Firstname = firstname;
Lastname = lastname;
}

public User(string id, string firstname, string lastname)
{
Id = new UserId(id);
Firstname = firstname;
Lastname = lastname;
}

protected override bool MembersEquals(User other)
{
return Id == other.Id && Firstname == other.Firstname && Lastname == other.Lastname;
}

protected override int MembersHashCode()
{
return Id.GetHashCode() ^ Firstname.GetHashCode() ^ Lastname.GetHashCode();
}
}

类图注册:

BsonClassMap.RegisterClassMap<User>(cm =>
{
cm.AutoMap();
cm.MapProperty(user => user.Id);
cm.MapProperty(user => user.Firstname);
cm.MapProperty(user => user.Lastname);
cm.MapCreator(user => new User(user.Id, user.Firstname, user.Lastname));
});

此类存储为子文档。当我尝试存储整个文档时,mongodb 驱动程序告诉我 MongoDB.Bson.BsonSerializationException: Creator map for class Box.Domain.User 有 3 个参数,但没有配置。。我真的不明白这是什么意思。

注意:UserId 类有一个注册的序列化器

public class UserIdBsonSerializer : SerializerBase<UserId>
{
public override UserId Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var currentBsonType = context.Reader.GetCurrentBsonType();
return currentBsonType switch
{
BsonType.String => new UserId(context.Reader.ReadString()),
_ => throw new NotSupportedException($"Cannot deserialize {currentBsonType} to an UserId")
};
}

public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, UserId value)
{
context.Writer.WriteString(value.Id);
}
}

编辑

ValueObject.cs

public abstract class ValueObject<T> : IEquatable<T>
where T : ValueObject<T>
{
// Verify the value object members equality.
protected abstract bool MembersEquals(T other);

// Generate a hash code depending on the value object members values.
protected abstract int MembersHashCode();

#region Equality
public bool Equals([AllowNull] T other)
{
if (ReferenceEquals(other, null))
return false;

if (ReferenceEquals(this, other))
return true;

return MembersEquals(other);
}

public override bool Equals(object obj)
{
var other = obj as T;

return Equals(other);
}

public static bool operator ==(ValueObject<T> lhs, ValueObject<T> rhs)
{
if (ReferenceEquals(lhs, null) && ReferenceEquals(rhs, null))
return true;

if (ReferenceEquals(lhs, null))
return false;

return lhs.Equals(rhs);
}

public static bool operator !=(ValueObject<T> lhs, ValueObject<T> rhs) => !(lhs == rhs);
#endregion

public override int GetHashCode()
{
return MembersHashCode();
}
}

UserId.cs

public class UserId: ValueObject<UserId>
{
public string Id { get; }

public UserId(string id)
{
if (id.Length < 5)
throw new ArgumentException($"user id must be 5 characters length. {id}");

// TODO: Add more validation rules for user id

Id = id;
}
protected override bool MembersEquals(UserId other)
{
return Id == other.Id;
}

protected override int MembersHashCode()
{
return Id.GetHashCode();
}
}

最佳答案

看起来您的注册类 map 中的 cm.AutoMap(); 在添加您自己的之前创建了 2 个创建者。

如果您删除它,它将开始工作。

BsonClassMap.RegisterClassMap<User>(cm =>
{
cm.MapProperty(user => user.Id);
cm.MapProperty(user => user.Firstname);
cm.MapProperty(user => user.Lastname);
cm.MapCreator(user => new User(user.Id, user.Firstname, user.Lastname));
});

另一种方法是从默认约定包中删除 ImmutableTypeClassMapConvention

ConventionRegistry.Remove("__defaults__");
var pack = new ConventionPack();
var defaultConventions = DefaultConventionPack.Instance.Conventions;
pack.AddRange(defaultConventions.Except(
defaultConventions.OfType<ImmutableTypeClassMapConvention>()
));
ConventionRegistry.Register(
"__defaults__",
pack,
t => true);

关于MongoDB 类有参数但没有配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60602105/

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