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
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.
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.
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 ...
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With