views:

72

answers:

3

I'm using Castle ActiveRecord, but wrapping it in my own persistence layer, because I want to hide this fact from application code. However, my entities all inherit from ActiveRecordBase<T>, so my choice of ORM is leaking to the application. What I especially don't like is the slew of methods this exposes off my entities.

How can I use ActiveRecord without inheriting from ActiveRecordBase?

I know some part of the abstraction will leak because my entities are decorated with ActiveRecord attributes, however I don't consider this part meaningful.

+2  A: 

You can use composition instead of inheritance - your business object basically wraps your active record object and delegates persistence responsibilities to it.

Dzmitry Huba
So you suggest I define, for every model entity, another one that just contains it? This seems a bit cumbersome ... or perhaps I didn't get what you're trying to say. Can you supply a code sample?
ripper234
@ripper234 » You could do that, but a simpler implementation is just to use an inner class.
John Feminella
Sorry, without a code sample I don't understand your answer.
ripper234
A: 

If you use just plain NHibernate you dont have to use decorators, etc you just need to roll up your own hbm.xml files which are very simple and there are plenty of tools out there to help.

Note, whilst this does not answer your question directly I put it out there because using your own NHibernate abstraction and learning NH well is a great skill and once you invest the time you will never use AR again (Well, unless its a small project you want to roll out quickly).

I have my own custom NH abstraction layer, you can find some code samples etc on my blog here: http://www.picnet.com.au/blogs/Guido/post/2009/07/08/Code-Generated-DAL-%28Data-Access-Layer%29-using-ORM-Article-1.aspx

Whilst this is probably over the top it does show you what a good understanding of NH can allow you to do in the future.

Guido

gatapia
I do want to use ActiveRecord (and FluentNH) to simplify matters, I just don't want this to leak so severely to the application.
ripper234
+3  A: 

When using Castle ActiveRecord your entities do not have to inherit from the base class. You can use ActiveRecordMediator<T> instead. Perhaps this article from the manual will help.

Krzysztof Koźmic
As usual, rftm :)
ripper234