473,499 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lazy Load base types w/ EF Table Per Type?

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
Aug 15 '08 #1
2 2515
Michael Bray wrote:
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?
No O/R mapper which does its job properly would allow that, directly,
as in: load an entity and fetch only the derived entity's fields. This
is because the derived entity's fields are ALSO the fields from Person,
so in general: Employee's fields are Person's fields + the fields only
defined in Employee.

THe thing is that a field defined in Employee, e.g. BadgeNo, has no
meaning outside the context of its associated Person fields.

So even though you could for example define a query like:
var q = from e in ctx.Employee
select new Employee() { BadgeNo=e.BadgeNo,
/* other fields only defined in Employee */ };

It makes no sense not to join with person even though there aren't any
person fields involved.

The above query already gives you a possibility of how to solve it with
an O/R mapper which allows multiple entities mapped onto the same table
(e.g. our LLBLGen Pro does that). You map a second Employee Entity onto
the Employee table, but this time, you dont make it a subtype of person.

Then your query becomes:
var q = from e in ctx.EmployeeNotSubtype
select new Employee() { BadgeNo=e.BadgeNo,
/* other fields only defined in Employee */ };

where EmployeeNotSubtype is the second mapped entity. Not sure if EF
supports this, I do recall it doesn't do this in the designer, but I'm
not sure, so you've to check.

Inheritance in the db isn't something which is free. THis is often
overlooked by POCO oriented developers who think that their class
hierarchies can be persisted without worries into databases using
relational schemas which have no clue what inheritance is.

As Nijssen/Halpin described in their NIAM books: flattening inheritance
hierarchies can be done in a couple of ways, but all have their side
effects and disadvantages. One therefore should choose the hierarchy
based on the nature of the data that's expected, which is against some
OO design principles perhaps, but so be it: in practise, applications
simply have to do what they're suppose to so if they're dogslow because
some OO guru thought it would be great to have deep inheritance
hierarchies with big queries as a result... that's not the way to go ;)

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.)
Isn't that a very complex way to solve a simple projection related
problem? :)

FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Aug 16 '08 #2
"Frans Bouma [C# MVP]" <pe******************@xs4all.nlwrote in
news:#s**************@TK2MSFTNGP04.phx.gbl:
The above query already gives you a possibility of how to solve
it with
an O/R mapper which allows multiple entities mapped onto the same
table (e.g. our LLBLGen Pro does that). You map a second Employee
Entity onto the Employee table, but this time, you dont make it a
subtype of person.

Then your query becomes:
var q = from e in ctx.EmployeeNotSubtype
select new Employee() { BadgeNo=e.BadgeNo,
/* other fields only defined in Employee */ };

where EmployeeNotSubtype is the second mapped entity. Not sure if EF
supports this, I do recall it doesn't do this in the designer, but I'm
not sure, so you've to check.
Yes exactly! After my post I came upon this exact solution as well. I
think I could get the designer to do this if I'm careful, but I'm having
some other wierd problems with it right now (I can't move my entities
around in the design space!?!). I know I had it working with two distinct
models. The obvious problem (whether in one model or split across two) is
that you have twice the work to maintain the model(s).
>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
Isn't that a very complex way to solve a simple projection
related
problem? :)
Probably, but it was fun to write... It turned out to be more of a
theoretical exercise for me, though, as it never saw any production use.
It actually worked quite well from a logic perspective, but I never even
got to the point of doing any performance testing, which I'm sure would
have suffered due to round trips.

-mdb
Aug 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
21625
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
4
3256
by: blackhawk | last post by:
I need to build a web page that has to potentially display a large amount of data in two grids on the same page. The HTML file with all of the formatting is about 7MB in size. This is too large...
10
2744
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
2
1256
by: Scott Ribe | last post by:
Here's my issue: Data comes in and is inserted into the database automatically, into a base table A. Later a human looks at the records, fills in some data, and now the records are assigned to...
22
8888
by: Brett Romero | last post by:
If my UI app uses three DLLs and two of those DLLs reference something named utilities.dll, does the UI app load utilities.dll twice or does the compiler recognize what is going on and load...
6
3067
by: acord | last post by:
Hi, In a html page, I don't know how to load in a php page from javascript. I've already defined a <div id="treemenu"> in the html page. The executed content from the php script is supposed...
0
2113
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
4
9564
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
6
3913
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.