Transactional support for objects

D

Dathan

I'm migrating a forms project from using Visual Studio's designer-
generated data binding objects (dataset, strongly typed data tables,
etc.) to binding to objects that encapsulate my application's business
logic. I'd like to provide support for transactions on these
objects. I've found the IEditableObject interface, which (correct me
if I'm wrong) provides the standard interface for objects that support
transactions. Now I'm wondering how best to implement this
internally. Is there a framework class for providing this sort of
support? If not, is there a "standard" way to implement this
functionality?

Let's say one of my objects is person. Here's more or less what I've
got so far:

public class Person
{
internal Hashtable original;
internal Hashtable current;
public Person()
{
this.original = new Hashtable();
this.original.Add("FirstName", null);
this.original.add("LastName", null);

this.current = this.original.Clone();
}

public string FirstName
{
get
{
return this.current["FirstName"];
}
set
{
this.current["FirstName"] = value;
}
}

public string LastName
{
get
{
return this.current["LastName"];
}
set
{
this.current["LastName"] = value;
}
}

public void Reset()
{
// reset current state to original state
this.current = this.original.Clone();
}
}

The original state is initialized by an adapter class that
deserializes a Person instance from my data store. Assuming the
values stored in the hashtable are immutable, initializing the current
member using original.Clone() should be fine, right? But more
importantly, is this the "right" way to provide this sort of
functionality?
 
P

Peter Morris

It will work for simple things, but when you have a composite pattern of
objects you might end up doing something like this

MyParentObject.MultiRoleProperty.Add(new MyChildObject());

when you undo the changes to the MyChildObject instance it needs to be "un
created" and in addition removed from MyParentObject's MultiRoleProperty.
When you have a multi-multi association that is navigable in both directions
you need to update both ends

{Start}
Pete.FoodLiked.Add(Pizza);
//or the equivalent
Pizza.LikedBy(Pete);
{Undo}


I'm just pointing out that it can be more complicated when it comes to
associations and multi-multi associations. I handled this myself in my own
OPF that I wrote for my employer for the compact framework, but in desktop /
web apps it's all implemented for my in the commercial framework I use so I
don't need to worry about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top