D
Dave A
Has anyone written or using an identity mapper design pattern in an
enterprise framework?
The identity mapper pattern is described in Martin Fowler's Patterns of
Enterprise Application Architecture. It means that subsequent loads of the
same object from the database will result in the same object in memory.
Album album1 = Album.Read(10);
Album album2 = Album.Read(10);
Assert.AreSame(album1, album2);
There are some caveats; there is one identity mapper per transaction not per
appdomain and if there is no transaction then a separate identity mapper is
created for that too. Also, when there are no more references to that
object in play then the identity mapper needs to remove the object from its
own internal store. If it did not do this then it means that the identity
mapper is nothing more than a cache that would not be aware of changes to
the underlying data in the db.
This is where my question lies. How can you work out when an object falls
out of scope to notify the identity mapper that the object count has reduced
by one? You could implement IDisposable but then you would need to call
'using' everywhere - not pretty. You could hook into the destructor but then
the behaviour of your code would rely on the scheduling of the garbage
collector - even worse.
You need to ensure that if the following function is called twice in
succession then it actually hits the database twice. If the Album's
destructor was left to notify the identity mapper of album1 falling out of
scope then this notification would not be sent until way after the Function1
was called for the second time.
void Function1()
{
Album album1 = Album.Read(10);
}
Any ideas?
Regards
Dave A
enterprise framework?
The identity mapper pattern is described in Martin Fowler's Patterns of
Enterprise Application Architecture. It means that subsequent loads of the
same object from the database will result in the same object in memory.
Album album1 = Album.Read(10);
Album album2 = Album.Read(10);
Assert.AreSame(album1, album2);
There are some caveats; there is one identity mapper per transaction not per
appdomain and if there is no transaction then a separate identity mapper is
created for that too. Also, when there are no more references to that
object in play then the identity mapper needs to remove the object from its
own internal store. If it did not do this then it means that the identity
mapper is nothing more than a cache that would not be aware of changes to
the underlying data in the db.
This is where my question lies. How can you work out when an object falls
out of scope to notify the identity mapper that the object count has reduced
by one? You could implement IDisposable but then you would need to call
'using' everywhere - not pretty. You could hook into the destructor but then
the behaviour of your code would rely on the scheduling of the garbage
collector - even worse.
You need to ensure that if the following function is called twice in
succession then it actually hits the database twice. If the Album's
destructor was left to notify the identity mapper of album1 falling out of
scope then this notification would not be sent until way after the Function1
was called for the second time.
void Function1()
{
Album album1 = Album.Read(10);
}
Any ideas?
Regards
Dave A