Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Interfaces - the number of objects being created

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

like image 854
vdh_ant Avatar asked Jul 13 '09 07:07

vdh_ant


People also ask

What is the fluent user interface How does it work?

Fluent Ribbon User Interface The Fluent interface makes it easier to find powerful features by replacing menus and toolbars with a Ribbon that organizes and presents capabilities in a way that corresponds more directly to how people work.

What is a fluent interface in Javascript?

A fluent interface, coined by Eric Evans and Martin Fowler, is defined on Wikipedia as: An implementation of an object oriented API that aims to provide more readable code. A fluent interface is also referred to as a fluent API or method chaining, depending on the language community.

What is fluent design pattern?

Fluent Interface pattern provides easily readable flowing interface to code. Wikipedia says. In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). Programmatic ...

What is fluent interface in C#?

A fluent interface is an object-oriented API that depends largely on method chaining. The goal of a fluent interface is to reduce code complexity, make the code readable, and create a domain specific language (DSL). It is a type of method chaining in which the context is maintained using a chain.


1 Answers

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);
    }
}
like image 185
Lasse V. Karlsen Avatar answered Sep 22 '22 14:09

Lasse V. Karlsen