OOP最佳实践:Employee.GetCars()vs Cars.GetByEmployee() [英] OOP best practice: Employee.GetCars() vs Cars.GetByEmployee()

查看:138
本文介绍了OOP最佳实践:Employee.GetCars()vs Cars.GetByEmployee()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定类公司员工汽车检索与公司或员工相关联的汽车的方法的首选方法是什么?

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...)

或者:

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:


  • 它分组所有 Car 相关的代码一起成为一个文件,使代码更加模块化,更易于维护。

  • 有一个直观的逻辑,使用返回值 Car (或更多类似于列表< Car> 在这种情况下)分组到 Car

  • It groups all Car related 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 like List<Car> in this case) grouped into the Car class.

有没有涵盖这个的最佳做法?

Is there a best practice that covers this?

推荐答案

我将在实体类中使用第一种方法。这些方法应该没有参数,因为它们只返回所有关联。
涉及一些简单的业务逻辑的第二种方法应该放置在一个帮助类中,或者可以放在CarDAO中,如果你有一个。

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.

这篇关于OOP最佳实践:Employee.GetCars()vs Cars.GetByEmployee()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆