Question

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

Was it helpful?

Solution

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top