Wednesday, March 16, 2011

Class usage in ORM based projects

This question is about "best use" scenarios in projects with ORMs like NHibernate, Subsonic, Linq2SQL, etc...

All of these tools generate basic entity classes, some with attributes, others without. Do people use these classes as their business classes? Or is there wholesale copying of data from ORM generated classes to manually created business classes?

Thanks.

From stackoverflow
  • I tend to work the opposite way. I create the business objects the way I need them, and create NHibernate mappings from my objects to data. You can have NHibernate generate a schema for you based on your mappings, or you can create your own schema, and create the mappings to go between the two. Linq2Sql and Entity Framework do not support this. I can't speak to Subsonic on this point.

    I usually create my business classes, and get the application at least partially running without any database at all. This way I can develop a better understanding of what it is the application is supposed to do, and how it should behave before making the decision of how to persist the objects.

    Justice : This is the way to go. In fact, the whole point of OOP and ORM is so that you are free to work with business objects (the domain model)! You can use tools like NHibernate to persist your business objects transparently, but your application code does not need to deal at all with persistence.
  • There are a couple of solutions for all of the tools mentioned, the answer is it depends on the scope of your project.

    Here's a similar question about LINQ to SQL that I answered a little while ago.

    Hope that helps!

  • I normally use the entities directly in the business and presentation layers. The data layer defines the entity, the business layer manipulates the entity or queries a list of entities, and the presentation layer displays the entities.

    I think that creating separate business objects and copying data between the two would be a lot of unneeded overhead. But if you find that you have to do this, I'd recommend just wrapping the entities instead of copying data back and forth. You can hide the entity and use Properties to expose members and alter behavior.

  • SubSonic and Linq2Sql are one-to-one orm mapper. Consider the situation where the datbase is normalized. For example an employee info is broken down in 3 different tables but in your domain model you'd want only one object Employee represent the info. This is where SubSonic and Linq2Sql fail. NHibernate allows you to map your domain object to multiple tables. Also you'd want to stay away from auto generated code. NHibernate allows you to define your own POCO (Plain old C# object) domain and has different ways that allows us to map that to table(s) in database

    Ahmad : Correct Shiraz. To isolate yourself from one-to-one mapping, youl would have to use SQL and abstract yourself at that level.

0 comments:

Post a Comment