Geeks With Blogs
Greg Young Greg.ToString()

It is discussed heavily in P of EAA (Fowler) http://www.martinfowler.com/eaaCatalog/unitOfWork.html that there are 2 primary ways for implementing a unit of work, client registration and dirty data checks during commit. I have been using a third way for some time now which seems to alleviate the issue of both while also providing some useful by-products (such as transactional capability within the object graph itself and automated locking functionality at the instance level). I am unsure if my methodology is new or not (I highly doubt it) but I can find very little written about it, if it is new or if you think its worthwhile I would be happy to write up a paper on it if you know anywhere that would be intrested in housing it.

To start out, I created my own dynamic proxy generator in C# (it generates IL proxy shells which either call out a seperate proxy handler or can have method builders specify the methods for the proxied class). All of my domain objects are already factoried from various repositories so dealing with the proxied objects is quite easy. The proxies that I generate register the object as dirty/deleted/etc with my unit of work automatically (the client can declaratively state that a given method is non-destructive to bypass this). My unit of work is scoped at the thread level to allow for easy calling and transaction management, this greatly adds in removing complexity from the proxy itself and also helps me avoid having to pass the unit of work through out the system (I guess my goal is to use domain objects that don't look like domain objects but instead look like plain old objects) ... The really important thing here is that since the calls are happenning from my dynamically proxied object, there is no code to maintain, and nowhere a programmer can "forget" to put in a registration.
 
Once the unit of work gets the object, it calls back into the sender to access some functionalty defined in my DomainObjectBase, to be specific there is an interface which the domain base meets which defines that it is capable of returning and consuming a memento. Since the unit of work is holding the references to the original objects and can get mementos from these objects defining their states at various stages, it becomes a trivial matter to rollback in the graph a partially completed transaction. Locking can also be provided in the base or the units of work can share data. Because I am using mementos to track object state changes, I can derive from the mementos what has been changed in order to issue a dirty only update.
 
One of the places I have found huge gain using this methodology is when dealing with disconnected data, as far as my graph is concerned it can handle all of the disconnected changes and problems with committing its work due to the fact that it is capable of rolling back, using a recursive unit of work ( i.e. nested transaction) it is also capable of being defined if a transaction fails to not bubble the failure all the way out but to continue and simply report the failure.

If anyone is doing something similar please let me know or if you like to hear more let me know as I have considerred writing up an article including my proxy generator etc on this.

Greg

Posted on Tuesday, November 22, 2005 9:41 PM | Back to top


Comments on this post: Unit of Work

# re: Unit of Work
Requesting Gravatar...
Your approach looks very interesting. I like to read more on this.
Left by Szymon on Nov 23, 2005 1:06 PM

# re: Unit of Work
Requesting Gravatar...
This does sound like a clever approach. I would love to hear more. Have you encountered any performance problems with the dynamically generated proxies?
I would also like to hear more about how your use of momentos.
Left by Sounds like an interesting appro on Nov 23, 2005 4:25 PM

# re: Unit of Work
Requesting Gravatar...
dynamic proxies do add a bit of overhead to the layer but it is fairly minimal (simply a level of indirection, an extra method call in the chain will have a penalty but it will be very small) esp if you pre-generate your proxies into a dll.

Left by Greg Young on Nov 23, 2005 5:37 PM

Your comment:
 (will show your gravatar)


Copyright © Greg Young | Powered by: GeeksWithBlogs.net