tags:

views:

24

answers:

2

In all the examples I have seen, ORM's tend to be used directly or behind some kind of DAL repository (presumably so that they can be swapped out in the future).

I am no fan of direct ORM use as it will be hard to swap out, but i am equally no fan of losing the full domain change tracking it provides!

In the past I would have written a data mapper class (Fowler) for each object in my domain, but I have learnt through experience that this CRUD coding drains around 1/3 of my time.

I a realize that changing my data access strategy is rather unlikely (I have never had to do so before) but I am really concerned that by using an ORM directly I will be locking myself into using it until the end of time.

I have been thinking about wrapping the ORM (no decision on the ORM itself yet) in a generic ORM container and passing this around to finder classes for each of the domain objects. However, I am totally unsure what a generic ORM wrapper class would look like!

Has anyone got any real life advise here? Please don't feel it nessecary to sugar coat your answers!!

A: 

The repository has a number of functions:

  1. It allows for unit testing with a mock implementation
  2. It allows you to hide the full implementation of the ORM from the consumer, and implement security functions
  3. It provides a layer of abstraction for business logic (although some people use a separate service layer for this), and
  4. It allows you to change the ORM implementation, if necessary.

Another container to genericize your ORM feels like overengineering to me. As you pointed out, it is unlikely that you will ever change your underlying implementation, but if you do, your repositories seem like the sensible place to do it.

Robert Harvey
Thanks. The problem that I have with the repository solution is that I would want a single repository for each object in my domain. Meanwhile the ORM tool tracks changes across the entire domain. Would I share a single ORM context between repositories (in order to to be able to do a single "commit") or should I have a separate ORM context for each repository? If I did the latter (what I am used to) then I would potentially have to bridge between transactions (as opposed to a single transaction) and lose one of the main benefits of having an ORM in the first place.
Ben Hinman
You would instantiate an ORM context in each repository, on a "Unit of Work" basis. This gives you the transactional capabilities you need, without having to think about a "global, generic" object.
Robert Harvey
I was thinking about using the ORM context as the sole representative of the unit of work across multiple repositories... I don't understand how you would have a single transaction (by transaction I am specifically meaning a database transaction) by instantiating a new ORM context per repository? Thanks for the help thus far.
Ben Hinman
The ORM context is a fairly lightweight object in most ORM's IME, so keep it around long enough to encompass a single transaction, but no longer than that. Does that make sense? I don't think you need a repository for every object in your domain. Why would you do that? It just adds another unnecessary layer of abstraction to your already-existing objects. Make your repository the place where you accomplish your transactions, and use whatever objects you need in the ORM to do that, on a per-transaction basis.
Robert Harvey
Yep brilliant. Thanks for the help! I'm a bit more confident about my choices now.
Ben Hinman
A: 

To point you in the direction of someone much wiser than me on these matters, one of the issues with having a generic ORM wrapper as highlighted by Ayende in his blog post The false myth of encapsulating data access in the DAL is that different ORMs are inherently too different to encapsulate effectively, having different methods for transaction handling, etc.

And on top of that, there's really not much point in switching ORMs anyway - one of the main reasons for encapsulating the DAL in case of change was to cope with switching databases, but most modern ORMs are able to work with many different databases anyway.

Grant Crofton
I here ya. Abstracting data access as a whole (or not at all) seems to be the general thinking. That's probably the reason why I was having a hard time envisaging a decent wrapper!
Ben Hinman