java - Data Source Patterns - Where's to put table's level methods? -
at company work models based on "active record pattern", model methods related operations of single record database, example:
class user { int id; string name; public boolean find (id); // sql / orm return object based on user's id; public boolean save (); // sql / orm save / update user public boolean delete (); // sql / orm delete user. } // create user "john" set objuser = new user (); objuser.name = "john"; objuser.save ();
my question in relation database entity "user", have methods table level , not record, such method "getallactiveusers", returns me query object of active users. type of situation ends in own activerecord model, in opinion not make sense .. me understand advisable / elegant treat type of situation? read gateway , repository patterns may useful this, wondering if has same trouble , how did solve ..
thank you!!
i'm not big fan of active record pattern think consistent way of using add getallactiveusers user class:
user.getallactiveusers();
on other hand reccommend use repository pattern in following cases:
- you want take advantage of unit testing.
- you access data source many locations , want abract complexity.
- you want centralized place handle caching.
- you want clear separation of concerns.
- you want apply domain model simplify complex business logic.
Comments
Post a Comment