Applied WPF 4 In Context – p5

I’m finally done with Chapter 6. It took longer than I thought (read: vacation break).  If you are struggling with Applied WPF 4 in Context by Raffaele Garofalo, below I have a list of things that might you get through Chapter 6.

Note: since it’s been a while I’m having trouble remembering all the things I did. If I missed something just ask in the comments and I’ll try to help.

The one item I struggled with the most was the test code. I just couldn’t get some of the tests to pass.  In UnitOfWorkFixture.cs, these methods had to have changes:

  • public void CanDeleteAnExistingObject()
    public void CanModifyExistingObject()
    public void CanModifyADetachedObject()

I also made similar changes to the RoleRepositoryTestFixture.cs for these methods:

    • public void CanDeleteAnExistingRole()
      public void CanUpdateAnExistingRole()

I’ll show you how I made the test pass for one method, the logic is the same for all. Here is my exact code for CanUpdateAnExistingRole:

// TDD
public void CanUpdateAnExistingRole()
        // add the entity
        var repository = new RoleRepository(uow);
        var role = new Role
                  Name = "Administrators",
                  Description = "Administrator Group."

        Assert.IsTrue(role.Id > 0); //works to here, gets new ID.
        uow.Session.Detach(role);//need to detach


        // modify the entity
        uow.Attach(role);//need to attach
        role.Name = "PowerUsers"; //change

        // try get it
        var expectedRole = uow.Session
                              .Where(r => r.Id == role.Id)
        Assert.AreNotEqual(expectedRole.Name, "Administrators");
        Assert.AreEqual(expectedRole.Name, "PowerUsers");
   catch (Exception exception)

So what I did is add the Detach and Attach statements to the existing code in the book. Without these, the test method could only add a role but not update it. It would try to update, and the context believed it was in “Modified” status, but the update never made it to the database. I still don’t fully understand why – if you do, please explain in the comments. :-)

Btw, if you do this make sure you add the Attach method before you attempt to update the role.Name property. (Otherwise the property isn’t tracked by the entity framework yet and therefore the change tracking is not “on”. )

If you repeat this same technique for the rest of the methods listed then they will end up passing. See below – both test sets now pass.

UnitOfWorkFixture Test Results

UnitOfWorkFixture Test Results


RoleRepositoryTestFixture Test Results

RoleRepositoryTestFixture.cs Test Results

2 thoughts on “Applied WPF 4 In Context – p5

  1. Well done !
    I didn’t find solution to pass the previous UnityOfworkMarkDeleted and UnityOfWorkMarkDirty tests. If you’ve an idea, please tell me even if I won’t use that way to communicate with the db.

    Best regards.


    • I gave up at some point on this book. It was too frustrating because too many items where missing. I couldn’t get past Chp 8.
      Sorry Franck, I deleted everything when the MS Build conference pretty much killed WPF.

Leave a Reply

Your email address will not be published. Required fields are marked *