views:

30

answers:

1

In my framewrok I have an ITransaction interface.
It implements some basic operations like Commit() and Rollback(), it is being used for flat files and other data sources.
However NHibernate has an ITransaction interface as well.
It is not the same as my interface since there are some database specific methods but there are similarities.
My problem is that I want a NHibernate transaction to be considered like my own transaction so I'll be able to switch implementations between database and flat files more easily.
How do I combine both interfaces so NHibernate will still be able to accept my NHibernate transaction objects and my framework will be able to accept my NHibernate transaction objects because they belong to MyFramework.ITransaction?

+1  A: 

Do you have a common base class, which could implement both NHibernate.ITransaction, and MyFramework.ITransaction? I don't know that I would create a common base class for the sole reason of implementing both these interfaces, but if you already have a common base class for other reasons, why not?

MyFramework.ITransaction could inherit from NHIbernate.ITransaction, ensuring that all implementors of MyFramework.ITransaction would also be implementors if NHibernate.ITransaction.

Finally, you could implement an Adapter pattern, with an ITransactionWraper, and 2 implementors (MyFrameworkTransactionWrapper and NHibernateTransactionWrapper). All your code would be written to the ITransactionWrapper interface, and its implementors would delegate calls to the correct ITransaction. Like most Adapter pattern implementations, you would likely use a Factory or AbstractFactory pattern to create the correct wrapper at run-time, as dictated by configuration.

mikemanne
MyFramework.ITransaction cannot have the same interface as NHibernate.ITransaction since NHibernate.ITransaction has DB specific methods. How would you implement an Adapter pattern for interfaces?
the_drow
My answer contains the basics of the Adapter pattern (see ITransactionWrapper), but I'll try to expand a little: assume NHibernate.ITransaction defines the SaveToDB() function, and your ITransaction interface defines the SaveToFile(). You want all your code to simply call Save(). Your ITransactionWrapper would define Save(). Your MyFrameworkWrapper would implement Save() as a call to its wrapped object SaveToFile(). Your NHibernateWrapper would implement Save() as a call to its wrapped object SaveToDB(). Your calling code never has to know which object is wrapped. It just calls Save().
mikemanne
mikemanne
NHibernate has a behaviour **that doesn't exist** for other transactions.
the_drow
I've dealt with that situation in a few different ways. Does the behavior represent a logical abstraction that would make sense to include on your ITransactionWrapper? For example, if NHibernate has an InitializeDB() function, maybe your ITransactionWrapper should define an Initialize() function. Also, consider whether that function needs to be called by your "outside" code at all. Maybe it isn't exposed on your outer interface, but is called by your wrapper class as part of certain other implementations.
mikemanne