添加参数,或创建新方法?

发布于 2024-10-26 07:24:24 字数 499 浏览 4 评论 0原文

假设我有一个长期建立的存储库,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽手叙旧 2024-11-02 07:24:24

我将重载该方法,创建一个采用 showStale 参数的新重载,然后修改旧方法以使用新重载,为参数值传递 false

界面如下所示:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts();
    public IEnumerable<Donut> GetDonuts(bool showStale);
}

或者,如果您使用 .NET 4.0,则可以使用可选参数:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts(bool showStale = false);
}

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 passing false for the parameter value.

The interface would look like:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts();
    public IEnumerable<Donut> GetDonuts(bool showStale);
}

Or if you're using .NET 4.0, you can use an optional parameter:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts(bool showStale = false);
}
一场信仰旅途 2024-11-02 07:24:24

为什么不使用可选参数?这样您就不会破坏现有代码:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts(bool onlyFresh);
}

实现:

public IEnumerable<Donut> GetDonuts(bool onlyFresh = false)
{
    if (onlyFresh)
        // do stuff
    else
        // do other stuff
}

Why not use an optional parameter? This way you don't break existing code:

interface IDonutRepository
{
    public IEnumerable<Donut> GetDonuts(bool onlyFresh);
}

Implementation:

public IEnumerable<Donut> GetDonuts(bool onlyFresh = false)
{
    if (onlyFresh)
        // do stuff
    else
        // do other stuff
}
心欲静而疯不止 2024-11-02 07:24:24

在某种程度上,这确实取决于个人喜好...

如果您有能力更改 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's GetDonuts 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...)

清醇 2024-11-02 07:24:24

根据情况,人们可能会考虑引入一种用于访问甜甜圈的属性。

interface IDonutRepository
{
    IEnumerable<Donut> Donuts { get; }
    .. or ..
    IQueryable<Donut> Donuts { get; }
}

如果您使用像 Entity Framework 或 NHibernate 这样精通 Linq 的 ORM,则实现此接口相当容易。

旧的 GetDonuts 方法可以重命名为 GetFreshDonuts(),或者您可以将对它的调用重构为以下形式:

repository.Donuts.Where(x => !x.Stale)

Depending on the circumstancs, one might consider introducing a property for accessing the donuts.

interface IDonutRepository
{
    IEnumerable<Donut> Donuts { get; }
    .. or ..
    IQueryable<Donut> Donuts { get; }
}

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:

repository.Donuts.Where(x => !x.Stale)
久夏青 2024-11-02 07:24:24

软件的增长趋势之一
设计将界面与
执行。原理是关于
将模块分为公共模块和
私人部位,以便您可以更改
私处不协调
与其他模块。然而,有
进一步的区别——
公共和发布的接口。这
区别很重要,因为它
影响你的工作方式
界面。

http://www.martinfowler.com/ieeeSoftware/published.pdf

One of the growing trends in software
design is separating interface from
implementation. The principle is about
separating modules into public and
private parts so that you can change
the private part without coordinating
with other modules. However, there is
a further distinction—the one between
public and published interfaces. This
distinction is important because it
affects how you work with the
interface.

http://www.martinfowler.com/ieeeSoftware/published.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文