tags:

views:

59

answers:

3

If I put database queries in a class that is representing a real world object, does that violate the design rule that entities must not have access to data sources?

Here is an example

class User
{
  public function register
  {
  }

  private function save_user_data()
  {
    // database queries here, either in AR or ORM
  }
}
A: 

Personally I think that the responsibility of saving the class should be with itself, however the actual code to perform the save to the database should go into a class in a data access layer.

Russ Cam
A: 

Well quite clearly yes. But the real question is does this rule matter? Depends, I suppose.

Personally, I just use an ORM to generate all this stuff for me. LLBLGen.

Noon Silk
+2  A: 

To quote Steve McConnell (Code Complete [1,2]):

Sofware's Primary Technical Imperative is managing complexity.

If you're writing a large scale app, in the long run it will reduce complexity to abstract data access. If you're writing a small to medium size app, it may make more sense to do your data access within the object itself, so long as you are clear and consistent with it.

Reduce complexity in a way that makes sense.

Jason
+1 Sometimes, I really wish for a +5 :)
Aaron Digulla