添加参数,或创建新方法?
假设我有一个长期建立的存储库,如下所示:
interface IDonutRepository
{
public IEnumerable<Donut> GetDonuts();
}
它已经存在很长时间了,并且 GetDonuts
方法按照它的说明执行操作。然后有一天,我需要添加一个新屏幕来显示数据库中的所有甜甜圈,结果发现该方法有一个隐藏功能 - 它过滤掉所有 stale = true
的甜甜圈。但在我的新屏幕上,我想显示所有这些,甚至是陈旧的!这里最好的方法是什么?
假设这个方法在各处使用,并且默认行为需要保持不变,那么最好添加一个名为 GetAllDonuts
的新方法,该方法不执行过滤,或者我应该只是这样做将 onlyFresh
参数添加到 GetDonuts
方法中?
我猜这只是判断,但我想知道是否有更多明智的答案?
Let's say I have a long established repository like this:
interface IDonutRepository
{
public IEnumerable<Donut> GetDonuts();
}
It's been around for ages, and the GetDonuts
method does what it says. Then one day I need to add a new screen that shows all the donuts in the database, and it turns out that the method has a hidden feature - it filters out all donuts where stale = true
. But on my new screen, I want to show all of them, even the stale ones! What is the best approach here?
Assuming that this method is used all over the place, and the default behaviour needs to stay the same, is it best to add a new method called GetAllDonuts
that doesn't do the filtering, or should I just add a onlyFresh
parameter onto the GetDonuts
method?
I'm guessing its just down to judgement, but I'm wondering if there are any more informed answers out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将重载该方法,创建一个采用
showStale
参数的新重载,然后修改旧方法以使用新重载,为参数值传递false
。界面如下所示:
或者,如果您使用 .NET 4.0,则可以使用可选参数:
I would overload the method creating a new overload that takes the
showStale
parameter and then modify the old method to use the new overload passingfalse
for the parameter value.The interface would look like:
Or if you're using .NET 4.0, you can use an optional parameter:
为什么不使用可选参数?这样您就不会破坏现有代码:
实现:
Why not use an optional parameter? This way you don't break existing code:
Implementation:
在某种程度上,这确实取决于个人喜好...
如果您有能力更改 API,我会(个人)以一种明显不返回所有 Donut 的方式重命名当前方法 实例。我的期望是存储库的 GetDonuts 方法将获取所有甜甜圈。这可以通过参数或不同的名称来完成,具体由您自行决定。
话虽这么说,如果保持兼容性至关重要,那么采用额外参数的方法重载可能是前进的最佳选择。 (这很大程度上取决于这个 API 的使用者和地点......)
This really comes down to personal preference, to some extent...
If you have the ability to change the API, I would (personally) rename the current method in a way that makes it obvious that it is not returning all
Donut
instances. My expectation would be that a repository'sGetDonuts
method would get all of the donuts. This could be doing via a parameter, or a different name, at your discretion.That being said, a method overload taking the extra parameter is probably the best option moving forward, if keeping compatibility is critical. (This depends a lot on who and where this API is used...)
根据情况,人们可能会考虑引入一种用于访问甜甜圈的属性。
如果您使用像 Entity Framework 或 NHibernate 这样精通 Linq 的 ORM,则实现此接口相当容易。
旧的 GetDonuts 方法可以重命名为 GetFreshDonuts(),或者您可以将对它的调用重构为以下形式:
Depending on the circumstancs, one might consider introducing a property for accessing the donuts.
It's fairly easy to implement this interface if you're using a Linq-savvy ORM like Entity Framework or NHibernate.
The old GetDonuts method could be renamed GetFreshDonuts(), or you could refactor calls to it into the form:
http://www.martinfowler.com/ieeeSoftware/published.pdf
http://www.martinfowler.com/ieeeSoftware/published.pdf