I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject.
There is a reason virtual
is not the default for methods in C#. The objects in my domain model are not prepared to make promises about the behaviors of subclasses, except in very specific cases where I mark them as such. Put another way, for very few methods on my domain objects is it appropriate to add a hook for unspecified new functionality.
Yet NHibernate wants me to make everything virtual
, and Entity Framework wants me to make all entity references virtual
. I realize why they need it (to create proxy objects), and I realize it's actually a legitimate use of inheritance and virtual
---they actually are hooking in to my properties in order to add new functionality. But it grates on me that I have to annotate my domain model classes with something that is entirely about persistence, and not at all expressive of their actual contract to implementers and consumers.
As a smaller issue, which I realize I probably cannot do anything about, often it is expressive to annotate my classes with sealed
for all the usual reasons. This is a bit less grating though, since omitting an annotation from my domain objects for the purpose of persistence seems less bad than adding one.
It is frustrating that after several years reading books like Effective C# or blogs like those of Eric Lippert, which give great advice on how to design expressive and bulletproof C# objects, the need to use ORMs is making me throw much of that knowledge out of the window. I am hoping that someone here can point out where I am wrong, either in my grasp of their capabilities or in my thinking about domain modeling and the role of ORMs.
virtual
was default (for methods) and I can count the number of times I havesealed
a class on my fingers: If you subclass and break something, that is your fault :-) I've run into too much inflexible code (from other libraries) that I can't consume well and can't change. – Jewry