ORM have a session (or whatever the name is) to load entities. The session can indeed be injected by a DI framework.
So in your example, with Hibernate
you could have something like:
class Service {
Session session; // could be injected by DI
...
Person p = (Person) session.load( typeof(Person), 50 );
}
The entity mapping in the ORM is independent of the IoC configuration.
Entities in an ORM hold data in the simplest way. You usually don't need to have an interface for your entity.
public class Person
{
Integer id;
String name;
// getter and setter for the values
}
The mapping can be done in various way. The idea is that you map the properties in the class to columns in the database. Here would be an excerpt of the configuration with the hibernate
.
<class name="Person"
table="PERSON" >
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="name" column="NAME" />
...
</class>
This were just the general ideas (sorry if there are mistakes). The details will depend on the IoC/DI you use, as well as the ORM, of course.
EDIT
Here is what I meant in my comment:
abstract class Person {
abstract Presenter GetPresenter();
...
}
class Person1 : Person {
Presenter GetPresenter() { return new PresenterForPerson1() }
...
}
// other subclasses
You don't use IoC to load entities, you use the session/repository.
Person person = session.Load( typeof(Person), 50 );
ORM support polymoprhic entities. So the person
object that is loaded can have concrete type Person1, Person2
etc.
To obtain the presenter
for the person
, you use
Presenter presenter = person.GetPresenter();
That's plain OO.
For testing, you could subclass PersonX
with PersonXTest
and override GetPresenter
to return mock presenter. Then you change the config of the ORM to user PersonXTest
instead of PersonX
for testing. No need of DI then.