OO data model - can I ignore the constraints of a Relation Mapper object?

I am just starting to model data for a new C # project that needs to be resilient.

It looks like the most natural OO model would have many nested .Net generics. Lists of objects, and these objects will also contain lists of other common types, etc., nested at least three levels deep.

Ideally I would just like to create an OO style data model and let the object relational mapping tool handle persistence - for me, OO design is much simpler than relational design.

But my initial research on ORMs shows that they don't necessarily work this way. It's not clear to me yet if I can create any arbitrarily complex object model and expect the ORM to continue "automatically". (I asked this question yesterday).

It seems to me that I might have to restrict my OO data model to constructs that I know the ORM can handle, which seems like a big limitation, especially when I haven't solved the ORM yet.

I want to keep the design of the project moving forward and don't want to get bogged down in ORM research right now.

My instinct is to simply design my object model around what I know I can do in C # in the way that feels most natural. This will help me better understand what is really needed for the application. It is my hope that I can then redefine the constraint-based ORM design if necessary.

Is this a good approach, or am I setting myself up for the pain world? Is it better to deal with the well-known ORM contraindications up front and the dumb object model around those constraints?

Edit: Undesirable, Stefan Steinegger lists most of these limitations that NHibernate places on OO code. Could anyone else provide a similar list for other ORMs - Subsonic in particular?

+2


source to share


3 answers


To decide how easy it is to map a model to a relational database is still heavily dependent on the ORM that will be used.

I have experience with NHibernate. This is the most flexible and non-intrusive ORM I've seen so far (although I haven't seen many of them). So I can talk about NHibernate, even if you don't use it, it will give you some idea of ​​what you should expect.

We develop the class model almost freely. There are more restrictions on implementation details:



  • you need a default constructor (can be private) Collections
  • must be of type IList<T>

    , ICollection<T>

    , IDictionary<K, V>

    , the Array, several other interfaces or no common instances.
  • everything must be virtual to make lazy loading possible. (optional but recommended)
  • each object (not value-type) needs a database primary key property and a database version property for optimistic locking (optional but recommended)

You must be careful about having value type classes (which do not have their own identity and are processed "by value") that are polymorphic. Probably, it needs, must be, become objects (needing identification).

I don't remember any other constraints we had in NHibernate regarding the class model.

+2


source


I don't know much about other ORMs, but you should definitely go this route with NHibernate. It is intended to be used in exactly the same way: first create your DDD style domain, later take care of the database and persistence related stuff. It is possible that some (minor) refactorings become necessary with the introduction of NH, but they don't really matter in my experience if you have a properly designed domain model, and they certainly aren't painful.



+1


source


I think the real question is how can I develop my OO application and still store my data (objects) in relational storage, regardless of the ORM or ORM. You should be fine when your approach is tightly coupled with your object model until you reach a real volume where impedance mismatch could harm you. It is still impossible to back up a relational database system without thinking about the relational model of the system, OO or not.

Also, the only classes in your object model that need to be stubbed out are those that are persistent. All others can be as complex and complex as you wish.

+1


source







All Articles