Managing Related Collections

Posts   
 
    
Posts: 98
Joined: 09-Feb-2005
# Posted on: 26-Jan-2006 02:45:35   

I am using the adapter model.

Suppose I have the typical User, UserRole, Role model, where UserRole is a m:n join table. Further, in my generated UserEntity class, I have the UserRoleCollection and the readonly m:n RoleCollection. My question is: What is the best way to keep these collections in sync? Specifically, assume I do the following:

  1. Fetch a UserEntity, prefetching both collections.
  2. Add an UserRole to the join collection
  3. Save the UserEntity recursively << ========================>> Now, the RoleCollection of the UserEntity is out of sync. Trying to simply refetch the RoleCollection doesn't work. Because it is read only, I can't do anything to the collection. So the question is, what is the least amount of work I can do to get this read-only collection in sync? At the moment, I'm setting it to null, and refetching, which can't be the best way.

Thought? Thanks all! Appreciate the input as always. (I've included the test case below, which presupposes a _roles entity that contains a collection of all of the available roles. The function: TestCollections_AfterSave_Reset_AfterBothFetch(); simply creates a user, adds the UserRole items, saves the user and the userroles. Then it sets the user to null, and refetches it, along with both collections.)


        [Test]
        public void TestRemoveItemsFromCollection()
        {
            DataAccessAdapter adapter = new DataAccessAdapter();

            /// Save the entity, and then refetch both collections
            TestCollections_AfterSave_Reset_AfterBothFetch();

            // Remove item from join collection doesn't remove the item from the m:n collection
            UserRoleEntity userRoleToDelete = (UserRoleEntity) _userAll.UserRoleCollection[2];
            _userAll.UserRoleCollection.RemoveAt(2);
            adapter.DeleteEntity(userRoleToDelete);

            Assert.AreEqual(_userAll.UserRoleCollection.Count, _roles.Count-1);
            Assert.AreEqual(_userAll.RoleCollectionViaUserRole.Count, _roles.Count);

            // Save the collection, and refetch both collections
            adapter.SaveEntity(_userAll, true, true);
            Assert.AreEqual(_userAll.UserRoleCollection.Count, _roles.Count-1);
            Assert.AreEqual(_userAll.RoleCollectionViaUserRole.Count, _roles.Count);

            // Now refetch both collections, and see if the other data is overwritten or not.
            PrefetchPath2 prefetchWithBoth = new PrefetchPath2((int)EntityType.UserEntity);
            prefetchWithBoth.Add(UserEntity.PrefetchPathRoleCollectionViaUserRole);
            prefetchWithBoth.Add(UserEntity.PrefetchPathUserRoleCollection);
            adapter.FetchEntity(_userAll, prefetchWithBoth);

            Assert.AreEqual(_userAll.UserRoleCollection.Count, _roles.Count-1);
            Assert.AreEqual(_userAll.RoleCollectionViaUserRole.Count, _roles.Count);

            // Now refetch both collections, and see if the other data is overwritten or not.
            
            adapter.FetchEntity(_userAll, prefetchWithBoth);
            Assert.AreEqual(_userAll.UserRoleCollection.Count, _roles.Count-1);
            Assert.AreEqual(_userAll.RoleCollectionViaUserRole.Count, _roles.Count-1);
        }

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Jan-2006 15:21:08   

why a refetch is not the best way?!! remember that a refetch is the only way to get values set by the database like Identity fields and default values.

Posts: 98
Joined: 09-Feb-2005
# Posted on: 26-Jan-2006 18:45:25   

Walaa wrote:

why a refetch is not the best way?!! remember that a refetch is the only way to get values set by the database like Identity fields and default values.

Thanks for the reply Walaa. I don't need to refetch the entire UserEntity, because I JUST saved it. The crux of the problem is that the save process doesn't update the the m:n Role collection. I basically just need to know what code to execute to rebuild the User's Role collection so that it remains in sync with the User's UserRole collection.

Hope that makes sense. Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Jan-2006 06:56:57   

You may fetch the UserRoles of that particular User alone in an Entity Collection.

I can't think of something else if you don't want prefetchPaths.