M
Michael Bray
With the recent release of EF I've decided to dig into it a bit more than I
did before... the question I'm specifically interested in, but haven't
been able to find a resource to answer it is... when using Table-per-Type
with inherited entities, is it possible to lazy load the base classes?
For example, if I have an entity called "Person" and an entity called
"Employee" and (obviously) Employee inherits from Person, is it possible to
have EF only load the properties associated w/ Employee, and not perform
the join to the Person table until necessary?
Obviously this would result in an object that was only half-loaded. A year
or two back, I wrote my own ORM that actually did this - it worked by
dynamically building a new proxy object that derived from the type desired
(using Emit) and overriding the properties of the base class (which had to
be declared virtual), so that when the user requested a property on the
base class, it went back to the database to retrieve those properties. (As
an aside, the really neat thing about this was that it worked even when
inspecting the properties via the VS Debugger - that is, viewing the object
in the debugger, it always appeared as if the entire object was loaded - it
was possible but quite difficult to actually see the object in an unloaded
state.)
Obviously there is a performance hit on the back-end when you actually do
access the properties of the base class due to the extra round-trip to the
data store, but it was optional, and could be specified to either perform
the lazy load or not perform the lazy load. It was under the control of
the programmer based on what he intended to do with the object.
Looking at the generated code for what I have seen so far with EF, it
doesn't seem that it doesn't support this concept, but I figured I'd take a
stab and see what this community has to say...
-mdb
did before... the question I'm specifically interested in, but haven't
been able to find a resource to answer it is... when using Table-per-Type
with inherited entities, is it possible to lazy load the base classes?
For example, if I have an entity called "Person" and an entity called
"Employee" and (obviously) Employee inherits from Person, is it possible to
have EF only load the properties associated w/ Employee, and not perform
the join to the Person table until necessary?
Obviously this would result in an object that was only half-loaded. A year
or two back, I wrote my own ORM that actually did this - it worked by
dynamically building a new proxy object that derived from the type desired
(using Emit) and overriding the properties of the base class (which had to
be declared virtual), so that when the user requested a property on the
base class, it went back to the database to retrieve those properties. (As
an aside, the really neat thing about this was that it worked even when
inspecting the properties via the VS Debugger - that is, viewing the object
in the debugger, it always appeared as if the entire object was loaded - it
was possible but quite difficult to actually see the object in an unloaded
state.)
Obviously there is a performance hit on the back-end when you actually do
access the properties of the base class due to the extra round-trip to the
data store, but it was optional, and could be specified to either perform
the lazy load or not perform the lazy load. It was under the control of
the programmer based on what he intended to do with the object.
Looking at the generated code for what I have seen so far with EF, it
doesn't seem that it doesn't support this concept, but I figured I'd take a
stab and see what this community has to say...
-mdb