Fluent Interfaces - 正在创建的对象数量

发布于 2024-07-26 18:18:23 字数 519 浏览 4 评论 0原文

我正在为我正在使用的一些简单的验证内容创建一些流畅的界面。 我注意到的一件事是我创建了很多不同的对象。

例如,给出以下语句:

Check.Assertion.ForValue.That(value, "value").IsNotNull() : void

Check.Assertion.ForArgument.That(value, "value").IsNotNull() : void

Validate.Assertion.ForDate.That("Test").IsNotNull() : bool

Validate.Assertion.ForNumeric.That("Test").IsNotNull() : bool

对于每个“.” (接受最后一个)我正在更新一个对象。 如果我在这里没有使用流畅的接口,我只会使用静态方法。

我想知道是否有人知道使用这么多实例对象(注意它们是非常小的对象)与使用静态方法相比,在哪里会注意到性能上的真正差异。

干杯 安东尼

I am in the process of creating some fluent interfaces for some simple validation stuff that I am playing around with. One thing that I have noticed is that I have a lot of different objects being created.

For instance given the below statements:

Check.Assertion.ForValue.That(value, "value").IsNotNull() : void

Check.Assertion.ForArgument.That(value, "value").IsNotNull() : void

Validate.Assertion.ForDate.That("Test").IsNotNull() : bool

Validate.Assertion.ForNumeric.That("Test").IsNotNull() : bool

for every '.' (accept for the last one) I am newing up a object. If I wasn't using a fluent interface here I would have just used static methods.

What I am wondering is if anyone knows where one would notice any real difference in performance when using this number of instance objects (note they are quite small objects) as appose to working with static methods.

Cheers
Anthony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心不设防 2024-08-02 18:18:23

请注意,您不一定需要到处构造新对象。 你可能只是通过接口来解决大部分流畅的接口,并且只是在一个对象上实现很多接口。 在这种情况下,您只需通过新接口返回 this 即可。

例子:

public interface ICheckAssertionForValue
{
    ICheckAssertionForValueThat That(Object value, String name);
}

public interface ICheckAssertion
{
    ICheckAssertionForValue ForValue { get; }
}

public class Check : ICheckAssertion
{
    public static ICheckAssertion Assertion
    {
        get { return new Check(); }
    }

    ICheckAssertionForValue ICheckAssertion.ForValue
    {
        get { return this; } // <-- no new object here
    }

    ICheckAssertionForValueThat ICheckAssertionForValue.That(Object value, String name)
    {
        return new SomeOtherObject(value, name);
    }
}

Note that you don't necessarily need to construct new objects all over the place. You might just solve most of the fluent interface through interfaces, and just implement lots of interfaces on one object. In that case you could just return this, just through a new interface.

Example:

public interface ICheckAssertionForValue
{
    ICheckAssertionForValueThat That(Object value, String name);
}

public interface ICheckAssertion
{
    ICheckAssertionForValue ForValue { get; }
}

public class Check : ICheckAssertion
{
    public static ICheckAssertion Assertion
    {
        get { return new Check(); }
    }

    ICheckAssertionForValue ICheckAssertion.ForValue
    {
        get { return this; } // <-- no new object here
    }

    ICheckAssertionForValueThat ICheckAssertionForValue.That(Object value, String name)
    {
        return new SomeOtherObject(value, name);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文