Encapsulate multiple retrieval methods for a class

I’m doing a little bit of cleanup and I’m trying to gather all the spread SQL queries done to an object into a single place. I have a class whose responsibility is to present a CRUD interface to the developer, and to encapsulate the results coming from selection queries into objects, that at the same time can be updated or deleted.

So far so good. However, I noticed that across the site we have 115 different places where an object retrieval is done. I managed to find out that there are a total of 17 cases which, with a little bit of witchcraft and making a couple parameters optional, could be reduced to 9.

9 retrieval methods feels too much to me for a class, especially if it already has many more methods to do the CRUD.

I thought of using a mixin (more specifically in PHP, a trait) to keep all the methods together and “in the class”, although separate enough, to keep the main class as clean as possible (with just the most complete retrieval method, whose all the other methods would depend on), although since mixins are used to encapsulate and reuse across many classes, I wonder if there’s a better way to do this without making the class too crowded.

For the record, this class is not an ORM itself, although it uses one internally to do all the CRUD tasks. Instead, the methods are called like get_by_id, reset_timer, add_feature, and so.

I think you might be over complicating this. There is nothing wrong with creating another class just for data retrieval. The main CRUD class could delegate the query logic to the data retrieval class.

Composition is your friend.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *