views:

117

answers:

1

Hi guys

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

+7  A: 

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);
    }
}
Lasse V. Karlsen
Hey cool I hadn't thought of that... Just one quick thing where we are returning a new SomeOtherObject object within the That method, couldn't we get Check to implement the ICheckAssertionForValueThat interface and just return this again? I guess it comes down to how much responsibility should go into a single class when designing a fluent interface. Maybe the answer is, if i wasn't doing it fluently would it all be in the same class... what do you think?
vdh_ant
Or am I worrying too much about creating new objects?? The only thing is that I will be using this interface a far bit (i.e. to check if params on different methods are null and if so throwing exceptions)
vdh_ant