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
[TestMethod]
public void CanUpdateAnExistingRole()
{
   try
   {
        // add the entity
        uow.StartTransaction();
        var repository = new RoleRepository(uow);
        var role = new Role
              {
                  Name = "Administrators",
                  Description = "Administrator Group."
              };
        repository.Insert(role);
        uow.CommitTransaction();

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

        uow.StartTransaction();

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

        // try get it
        var expectedRole = uow.Session
                              .CreateObjectSet<Role>()
                              .Where(r => r.Id == role.Id)
                              .FirstOrDefault();
        Assert.AreNotEqual(expectedRole.Name, "Administrators");
        Assert.AreEqual(expectedRole.Name, "PowerUsers");
   }
   catch (Exception exception)
   {
        Assert.Fail(exception.ToString());
   }
}

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

Applied WPF 4 In Context – p4

So I’m still working on getting through Chapter 6 – almost there. Right now I made it to Page 151, the start of the Repository pattern.  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.

  1. Ignore the top of page 140 where it says run the test. It won’t work because IUnitOfWork is not fully implemented yet.
  2. In IUnitOfWork, change void BeginTransaction(); to be void StartTransaction(); Raffaele switches to using StartTransaction at the top of  page 141 but he doesn’t update the interface.
  3. In IUnitOfWork, change the method definition by adding the where T: class like so:
  4. void MarkDirty<T>(T entity) where T : class;
    void MarkNew<T>(T entity) where T : class;
    void MarkDeleted<T>(T entity) where T : class;
  5. On pages 143-150, Raffaele discusses the Add, Update and Remove Objects, the trouble is that he skips the MarkDeleted implementation. Look at page 145, he jumps straight to the test method. As a result I created my own:
  6. public void MarkDeleted<T>(T entity) where T : class
    {
         session.DeleteObject(entity);
    }

Screen shot of VS 2010:

Chapter 6 - Test Complete

Chapter 6 - Test Complete

 

Applied WPF 4 in Context – P3

So last night I managed to get Rudi’s codeproject example to work inside of the TimeTracker application.

According to Raffaele’s post here (see his comment on 6/20/11) we should be getting *full* source code sometime today. I checked the Apress site earlier but there wasn’t any new code.

Here’s a screenshot of the control in VS 2010 – I think it’s close enough to Figure 5-11 to continue moving on.

Schedule Control

Rudi's Schedule Control in VS2010

More Applied WPF 4 in Context

If you are reading Applied WPF 4 in Context…

Two issues…

a) page 108 cites: http://www.rudigrobler.net/blog/timeslotpanel
This is wrong, the right address for Rudi’s control is http://www.rudigrobler.net/timeslotpanel

b) The code is apparently on codeplex at http://wpfschedule.codeplex.com however this is incorrect. The project does not appear to have any relation with Rudi.

 

Update:

It appears that Figure 5-11, page 109, is from Rudi’s CodeProject article. Go to Rudi’s page (above) and hit Part 1 of the article. Downloading and compiling the article code gave me something that is pretty close to the Figure. Now I just need to shoehorn it into Raffaele’s TimeTracker example.

Hope that helps.

If you are reading Applied WPF 4 In Context…

Right now I’ve made it through to Chapter 3 but I found Chapter 2 quite challenging because some of the Figures do not match the associated text. Below is my version (as big as I could make it) of Figure 2-6 from the book. It is how I understand it to look, and I believe, it is what Raffaele Garofalo is attempting to communicate. I have submitted several erratas in regards to Chapter 2 but until those are sorted out I hope I am helping other readers.

Things to note:

Chapter 2 code is not included the source code download I grabbed from Apress on 6/27/2011.

I used Apress.TimeTracker like the text suggests and not Apress.TimeTracking like the figure(s) in the book contain.

Under the 3rdParty Folder, WPFToolKit.Extended.dll is actually not from Microsoft but rather an individual effort from codeplex – here.  In Figure 2-7 of the book Raffaele shows the extended toolkit added to the project and not the WPF Toolkit provided by Microsoft which he suggests he is using on page 26. In the text on page 39 it only describes it as “WPFToolkit” and so I have assumed the he meant the Extended version given the Figure and it’s placement in the 3rdParty folder.

I also used Lib as a folder name and not Lib and Resources as in the Figures in the book do since the icons are placed in Apress.TimeTracker/Resources/Images as per Figure 2-8 on page 40.

 

Applied WPF in Context Fig 2-6

Applied WPF in Context Fig 2-6