BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

ORMs And Persistence Ignorance

by Roopesh Shenoy on Aug 29, 2013 |

Julie Lerman recently noted that Entity Framework can work with classes having private constructors and private property setters.

In the test published by Julie, you can see that Entity Framework (EF) is able to populate a property with private setters and invoke a private constructor without much difficulty. You can see how EF does this by referring the EntityProxyFactory and related classes, but in a gist, it’s a combination of reflection and IL Emits. NHibernate goes one step further and even supports accessing private fields (already being discussed for EF as well). However both currently require you to have a default constructor (even if it is non-public).

This general direction is good because, as Julie mentions, it promotes persistence ignorance

Architecturally, both Entity Framework and NHibernate support using patterns that help achieve persistence ignorance –

Entity framework has the DBSet and the DBContext respectively, whereas NHibernate has QueryOver api and the SessionContext. On the other hand, to support lazy loading, they also currently follow the virtual proxy pattern which goes against this principle (needs properties to be marked as virtual).

Persistence Ignorance is generally regarded as a good practice; business rules can be isolated from persistence logic, unlike, say when using Active Record pattern. This keeps the overall design simpler and more easily testable.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT