tags:

views:

98

answers:

3

I would like to know where is the best place to set my db object with my model.

Should I hard coded it since my model should be designed for one project, so i set it inside my constructor or wherever i do initialization ? or Should I pass my db object to my constructor when instancing my object ?

What is the best way, i mean from experimented users, and efficient that'll give me more confort to use ?

A: 

I would not hard code it, even if the code is never used for another project simply moving from a test database to a live database may require locating and changing the code in the model class. That would be far better placed in some kind of configuration file.

Personally, I would have the db object defined in whatever you use as a bootstrap - and then have the model(s) use that single object.

Tim Lytle
+1  A: 

For testability, you should pass it into the constructor rather than hard coding it. This helps you to write unit test because you can mock your DB object.

oykuo
+2  A: 
jakemcgraw