ibanore
ibanore

Reputation: 1500

Accessing the database through one Database class, tight coupling? Breaks SRP?

I am developing some software with a few people and from the completed class diagrams there is one Database class and for example the Order class has two constructors, one which has no arguments and one which excepts an id. It also has a save() method so going by those class features I am assuming if you supply an id in the constructor the class will use the Database class and populate the objects properties and also there is no place to inject this Database class in the constructor or in a setter method so I presume they want to use a Singleton.

I want to know if my arguments are valid before I say it to them so here they are:

Would they be valid arguments and are there any more flaws doing it this way? If my points are valid would it be worth saying it to them?

Thanks.

Upvotes: 1

Views: 141

Answers (1)

Alexander Torstling
Alexander Torstling

Reputation: 18898

This is a well-known pattern called active record. It is commonly employed in several large frameworks such as Ruby on Rails. It does have the downsides that you mention, and I think that you should highlight the potential problem, but not without having any alternatives to discuss.

One common alternative is to have a service façade which saves you objects - a set of DAOs. With this pattern you make accessing the database more explicit and less convenient, but IMO you decrease DB coupling in your main app. This is better in a SRP perspective, as you mention, which amongst others makes testing much easier.

Upvotes: 1

Related Questions