RavenDb Strange行为与具有相等成员的对象

时间:2012-01-29 22:56:29

标签: ravendb

当我试图将对象列表存储到ravendb时,我得到了一个奇怪的bug(在我的代码中)的底部。问题是要存储的对象具有resharper生成的相等成员。有问题的对象如下(注意我已经注释了平等成员以解决问题) -

//[DataContract(Namespace = "")]
//[KnownType(typeof(IApplicationEntity))]
public class ApplicationEntity: IApplicationEntity
{

    public ApplicationEntity()
    {

    }
    public ApplicationEntity(string processName)
    {
        ProcessName = processName;
        Id = "Processes/" + ProcessName;
    }

    public ApplicationEntity(string key, string processName)
    {
        ProcessName = processName;
        Key = key;
        Id = string.Format("Processes/{0}_{1}", Key, ProcessName);

    }


    //[DataMember]
    public string Id { get; set; }
    //[DataMember]
    public string Key { get; set; }
    //[DataMember]
    public string ProcessName { get; set; }
    //[DataMember]
    public string ProcessDescription { get; set; }
    /// <summary>
    /// used to generate sequential unique activity ID generation only.
    /// </summary>
    //[DataMember]
    public string ActivityCount { get; set; }

    //public bool Equals(ApplicationEntity other)
    //{
    //    if (ReferenceEquals(null, other)) return false;
    //    if (ReferenceEquals(this, other)) return true;
    //    return Equals(other.ProcessName, ProcessName);
    //}

    //public override bool Equals(object obj)
    //{
    //    if (ReferenceEquals(null, obj)) return false;
    //    if (ReferenceEquals(this, obj)) return true;
    //    if (obj.GetType() != typeof (ApplicationEntity)) return false;
    //    return Equals((ApplicationEntity) obj);
    //}

    //public override int GetHashCode()
    //{
    //    return (ProcessName != null ? ProcessName.GetHashCode() : 0);
    //}
}

现在,如果我使用相等的成员实现存储对象,那么以下代码会产生奇怪的结果 -

int count = 0;
        using (var session = _store.OpenSession(_databaseName))
        {


            foreach (var applicationEntity in _listOfApplications)
            {
                var entity = new ApplicationEntity(count.ToString(), applicationEntity.ProcessName);


                //ravenRepositoryCachable.Add(entity);
                session.Store(entity);


                count++;


            }

            session.SaveChanges();
        }

奇怪的行为是我希望Key字段增加到400,因为列表有400个成员,但是存储的前10个对象的Key是正确的,即0到9.但是第11个从0开始等等。

但是如果我关闭相等的成员(如上面的代码片段)那么这个问题就会消失。

此外,如果我一次添加一个对象而不是批处理,问题就会消失 -

int count = 0;
        foreach (var applicationEntity in _listOfApplications)
        {
            using (var session = _store.OpenSession(_databaseName))
            {
                var entity = new ApplicationEntity(count.ToString(), applicationEntity.ProcessName);


                //ravenRepositoryCachable.Add(entity);
                session.Store(entity);
                session.SaveChanges();

                count++;

            }
        }

我知道我已经解决了这个问题,但我不明白这里发生了什么以及为什么只有关键领域受到影响?这是一个未定义的行为,我担心代码应该在生产中部署!很明显,平等成员不是定义的。我需要深究它,这是一个错误吗?

1 个答案:

答案 0 :(得分:0)

不,我认为RavenDB中没有这样的错误。相反,我猜你的代码还有另一个问题,因为这个测试(基于你上面的例子)对我有用:

public class EqualityMembersInSessionCache
{
    public class ApplicationEntity
    {
        public ApplicationEntity(string key, string processName)
        {
            ProcessName = processName;
            Key = key;
            Id = string.Format("Processes/{0}_{1}", Key, ProcessName);
        }


        public string Id { get; set; }
        public string Key { get; set; }
        public string ProcessName { get; set; }


        public bool Equals(ApplicationEntity other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.ProcessName, ProcessName);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(ApplicationEntity)) return false;
            return Equals((ApplicationEntity)obj);
        }

        public override int GetHashCode()
        {
            return (ProcessName != null ? ProcessName.GetHashCode() : 0);
        }
    }

    [Fact]
    public void Has_correct_behaviour()
    {
        var randomNames = new List<string>();
        for (int c = 1; c < 100; c++)
        {
            randomNames.Add(string.Format("test{0}", c));
        }

        using (var store = new EmbeddableDocumentStore { RunInMemory = true })
        {
            int count = 0;
            using (var session = store.OpenSession())
            {
                foreach (var name in randomNames)
                {
                    session.Store(new ApplicationEntity(count.ToString(), name));
                    count++;
                }
                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                var results = session.Query<ApplicationEntity>()
                    .Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
                    .ToList();

                Assert.NotEmpty(results);
                Assert.Equal(99, results.Count);
                for (int c = 0; c < 99; c++)
                {
                    Assert.Equal(c, int.Parse(results[c].Key));
                }
            }
        }
    }
}

如果您希望我们对此进行进一步调查,请提供失败的测试。