Re: "how to call the underlying ORM when I need to create a new object"
Have you looked into the idea of AGGREGATE ROOT and the REPOSITORY patterns - they might be helpful.
As a rough summary:
AGGREGATE ROOT is an 'entity' that has a globally unique id within the system. Most of the time, these are the only objects that your application will need to be grabbing 'by id'. They are found through a REPOSITORY which is generally initialized to know about the data layers at application start-up / bootstrap.
FACTORIES for AGGREGATE ROOTs are also generally initialized to know about the data layers at application start-up / bootstrap.
The REPOSITORY can then mediate the process of digging up the data for the object in any way it likes. REPOSITORY delegates to the datalayer/mapper to get the raw data and tends to delegate the actual job of object reconstitution over to a FACTORY (class /method) to do the building.The REPOSITORY then returns the newly reconstituted AGGREGATE ROOT object or (collection-of) to the client that invoked the REPOSITORY's find method - i.e the application. The REPOSITORY is the interface to retrieve and save AGGREGATE ROOTs and gives the behaviour of them being stored in-memory.
An application might use a FACTORY directly, however, to create a brand new AGGREGATE ROOT object.
The Factory of an AGGREGATE ROOT has good knowledge about the ORM/Mapper Layers and, upon creation of a brand new entity it might call upon the services of some kind of 'number sequence object' to get its unique id.
AGGREGATE ROOTs are generally the only kinds of domain object that you need to 'Find by Globally Known Id' because any other domain objects are either:
- throwaway value objects - like a money value, OR
- 'AGGREGATE ROOT dependent' entities. I.e. objects that have ids that are only unique within the context of an AGGREGATE ROOT which implies that
- the AGGREGATE ROOT should really be the only thing that needs to find it/use it - internally
- they can be found by using the ORM Mapper of the AGGREGATE ROOT
- their ids can be created by the AGGREGATE ROOT that defines its context/scope
Further Reading:
See Domain Driven Design, Eric Evans.
Aggregate Root:
http://books.google.co.uk/books?id=7dlaMs0SECsC&lpg=PP1&dq=domain%20driven%20design&pg=PA147#v=onepage&q=&f=false
Repositroies:
http://books.google.co.uk/books?id=7dlaMs0SECsC&lpg=PP1&dq=domain%20driven%20design&pg=PA147#v=onepage&q=&f=false