IEditableObject: a useful interface

I really like this interface, so I decided to dedicate a post to this beauty. As you can read in my previous post I am using dynamic proxy to apply this behavior dynamically.

Let me show you my love with some tests:

private const string title = "The dark side of the moon";
private const string newTitle = "Dark side of the moon";

[Test]
public void transient_entity_should_commitchanges_after_endedit()
{
    var album = container.Resolve<Album>();
    
    album.Title = title;

    ((IEditableObject)album).BeginEdit();
    album.Title = newTitle;
    ((IEditableObject)album).EndEdit();

    album.Title.Should().Be.EqualTo(newTitle);
}

[Test]
public void transient_entity_should_rollback_after_canceledit()
{
    var album = container.Resolve<Album>();

    album.Title = title;

    ((IEditableObject)album).BeginEdit();
    album.Title = newTitle;
    ((IEditableObject)album).CancelEdit();

    album.Title.Should().Be.EqualTo(title);
}

Could we add this behavior to nhibernate entities? Yes, I’m working on uNhAddIns.WPF for that matter. But let me show you two simple test:

[Test]
public void session_should_be_dirty_after_commitchanges()
{
    var id = CreateNewAlbum();
    using (ISession session = sessions.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        var album = session.Get<Album>(id);
        ((IEditableObject)album).BeginEdit();
        album.Title = "Dark side of the moon";
        ((IEditableObject)album).EndEdit();

        session.IsDirty().Should().Be.True();

        tx.Commit();
    }
}

[Test]
public void session_shouldnot_be_dirty_after_cancelchanges()
{
    var id = CreateNewAlbum();
    using (ISession session = sessions.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        var album = session.Get<Album>(id);
        ((IEditableObject)album).BeginEdit();
        album.Title = "Dark side of the moon";
        ((IEditableObject)album).CancelEdit();

        session.IsDirty().Should().Be.False();

        tx.Commit();
    }
}

As you can see when we cancel the edition the entity restore his values and the current session should not be dirty.

What do you think about this one?

    var id = CreateNewAlbum();
    using (ISession session = sessions.OpenSession())
    using (ITransaction tx = session.BeginTransaction())
    {
        var album = session.Get<Album>(id);
        ((IEditableObject)album).BeginEdit();
        album.AddTrack(new Track("On the Run"));
        ((IEditableObject)album).CancelEdit();

        album.Tracks.Any(t => t.Name == "On the Run")
                    .Should().Be.False();

        session.IsDirty().Should().Be.False();

        tx.Commit();
    }

Well this is not yet implemented, but CancelEdit will raise cancel’s within the full object graph. In this case Tracks use a collection type that implements IEditableObject.

I mix this concept whit CpBT (from uNhAddIns) and I did manage to resolve an interface like this with a few  databindings.

 window

Thoughts?


blog comments powered by Disqus
  • Categories

  • Archives