MongoDB类具有参数,但未配置参数

问题描述 投票:1回答:1

我有一个类,该类具有一些我想使用mongodb存储的只读属性。

User.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:类Box.Domain.User的Creator映射具有3个参数,但未配置任何参数。。我真的不明白这是什么意思。

NB: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();
        }
    }
mongodb mongodb-.net-driver mongodb-csharp-2.0
1个回答
0
投票

好像您的寄存器类映射中的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));
            });
© www.soinside.com 2019 - 2024. All rights reserved.