开发者

Stored IEnumerable dissapears on restart debug, but recreating IObjectServer and IObjectClient in one unit test is ok

I'm storing an IUser object in a Db4o database as follows (client is an IObjectClient):

public Guid AddUser(IUser user)
{
    lock (userLock)
    {
        user.Id = Guid.NewGuid();
        client.Store(user);
        client.Commit();
    }
    return user.Id;
}

The implementation of IUser that is used here has the field that is discribed in IUser IEnumerable<Roles> Roles { get;} implemented with a backing field IList<Roles> = new List<Roles>()

When I run the following UnitTest everything works fine:

[TestMethod]
public void UserStaysInRoleAfterServerRestart()
{
    string filePath = IOHelper.MakeAbsolute(ConfigurationManager.AppSettings["userRepository.path.db4o"] + "1");
    IUser expected = GenerateTestUser();
    expected.AddRole(Roles.Anonymous);
    IObjectServer userServer1 = Db4oFactory.OpenServer(filePath, 0);
    IUserRepository repo = new UserRepository(userServer1);
    repo.AddUser(expected);
    userServer1.Close();

    IObjectServer userServer2 = Db4oFactory.OpenServer(filePath, 0);
    IUserRepository rep开发者_StackOverflow中文版o2 = new UserRepository(userServer2);
    IUser actual = repo2.GetUser(expected.Id);
    Assert.IsTrue(actual.IsInRole(Roles.Anonymous));
}

When I use the same methods (AddUser and GetUser) in my website, separated by a "restart debugging" however, the GetUser() returns the IUser correctly, but with the Roles.Count() == 0


One issue could be the update-deph. In your test, you use a new created IUser and use .AddRole() on it. So everything works fine. However, as soon as you call .AddRole() on an existing IUser the update-deph is relevant.
By default only the object passed to IObjectContainer.Store() are updated. All the object referenced are not updated. Since a list is also an object, it isn't updated. Therefore you have this possibilities:

  • Increase the update-deph in the configuration. Either globaly, for some classes or with the cascade-update-flag
  • Store the list explicit with IObjectContainer.Store()
  • Or use Transparent Persistance

If that isn't the issue, tell me and I'll remove this answer.

By the way. I the db4o-version you're using (7.4) has a suboptimal Guid-Handling. Maybe you should consider the newer version, 7.12.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜