Given the classes Company, Employee, and Car what is the preferred practice for methods to retrieve Cars associated with Company or Employee?
Employee.GetCars(params...)
Company.GetCars(params...)
Or:
Cars.GetByEmployee(params...)
Cars.GetByCompany(params...)
The first approach is the one I have generally used and always seemed the most intuitive to me. But after seeing a large code-base that used the second approach I have to admit that it’s growing on me. The two things I really like about the second approach are:
- It groups all
Carrelated code together into one file, making the code more modular and easier to maintain. - There is an intuitive logic to having any method with a return value of
Car(or more likeList<Car>in this case) grouped into theCarclass.
Is there a best practice that covers this?
I would use the first approach in the entity classes. There should be no params to these methods as they only return all associations.
The second approach which involves some simple business logic should be placed in a helper class or maybe the CarDAO, if you have one.