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

StackOverflow https://stackoverflow.com/questions/2063775

  •  20-09-2019
  •  | 
  •  

Question

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 repo2 = 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

Was it helpful?

Solution

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:

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top