tags:

views:

162

answers:

7

I'm looking over .NET ORM implementations, and I have a major burning question - are there any .NET ORM implemenations that don't require public properties for every field in the database? When I see examples like this, a little bell goes off in my head. I firmly believe in encapsulation, and being forced to open the kimono of my objects just to make them work nicely with persistence frameworks gives me the heebie-jeebies. Is this sort of accessibility required in all ORMs out there? If not, please point me to examples of those that don't need it!

A: 

Check out Scott Gu's nerd dinner tutorial. LINQ and MVC lets you create a business layer that wraps the DB objects in public and private properties.

http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx

Mike Robinson
LINQ isn't exactly an ORM - but I'll go check out the link just in case it's got useful techniques.
Harper Shelby
He meant LINQ to SQL specifically, not LINQ in general.
Lucas
A: 

You mean you would like to map private or protected members to the database, rather than having them all public? One ORM I have used that can do that is Gentle.NET. It is stable but not really getting many updates these days.

codeulike
That's exactly the need - like I mentioned, I have a very strong belief in encapsulation, but sometimes there are private fields that should be stored.
Harper Shelby
+6  A: 

NHibernate supports mutiple access strategies for class members. Public properties are a de-facto default but you can tell NHibernate to access the fields of your class directly.

e.g.

<property name="CustomerName" access="field.camelcase-underscore" />

will instruct NHibernate to use a field in your class declared like this:

protected string _customerName;
Steve Willcock
I was going to answer exactly the same thing. :)NHibernate also does not require you to have a public default constructor. If you don't want to have a public default constructor, you can simply make it private. (You have to define one, so that NHibernate can create an instance of your class, but it can perfectly be private).
Frederik Gheysels
So NHibernate can actually deal with private/protected members, and doesn't require the public access? Excellent! Time to really dig in then. Thanks.
Harper Shelby
+1  A: 

The Entity Framework supports that, too. You can define the access modifier per property and independently for the getter and the setter.

Daniel Brückner
+1  A: 

Mindscape LightSpeed binds only to fields - only add properties for fields you wish to expose (this is the only way it works for the express reason of encapsulation). Of course the tooling will default to providing properties but it can all be turned off.

Mindscape LightSpeed O/R Mapper

traskjd
A: 

LINQ to SQL can bind to private fields but it does not however fully support a private parameterless constructor.

Lucas
I think my issue is bigger than that - because unless there's a good domain reason for it, I don't want to write *any* parameterless constructor.
Harper Shelby
Hmm, Harper asked for ORMs that don't require all properties to be public, I give a valid and well-known example, and I get -1. Any reason?
Lucas
A: 

Harper - I felt pretty much the same way about Linq-To-SQL until I realized that it's all about how you think about the ORM... it helps to think of Linq2SQL as nothing more than a "strongly typed ADO.NET provider"...

If you just want a "quick look" at a couple of fields, then LINQ projections will return anonymous types to you that only contain the fields you're interrested in. This means that the size of the entity class is irrelevant to the network and memory performance of youre query.

On the far other side of the spectrum, DataContexts (or UnitOfWork in other ORMs) are typically something that you use and dispose of as quickly as possible. It follows, then, that the entities they generate should be limited in their role to merely being "data holders". Put differently, if you want to construct complex or long living objects, design them seperately from the LINQ entities (i.e. different classes), and just use L2S to populate/instansiate those objects.

Mark