Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question regarding fluent interface in C#

I have the following class:

public class Fluently
{
  public Fluently Is(string lhs)
  {
    return this;
  }
  public Fluently Does(string lhs)
  {
    return this;
  }
  public Fluently EqualTo(string rhs)
  {
    return this;
  }
  public Fluently LessThan(string rhs)
  {
    return this;
  }
  public Fluently GreaterThan(string rhs)
  {
    return this;
  }
}

In English grammar you can’t have “is something equal to something” or “does something greater than something” so I don’t want Is.EqualTo and Does.GreaterThan to be possible. Is there any way to restrict it?

var f = new Fluently();
f.Is("a").GreaterThan("b");
f.Is("a").EqualTo("b");        //grammatically incorrect in English
f.Does("a").GreaterThan("b");
f.Does("a").EqualTo("b");      //grammatically incorrect in English

Thank you!

like image 997
Jeff Avatar asked Aug 20 '09 07:08

Jeff


People also ask

Why are the linq extension methods designed based on the principle of fluent API?

In LINQ, the 'fluent' method syntax flows logically and intuitively, and allows them to be combined simply, because each method returns the appropriate type of object for the next. Can this fluent technique be extended as an API style to make it easier to develop C# team-based applications for enterprises?

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.

What is a fluent interface Python?

Fluent interfaces help the user of your API to work with an object and it's methods in a more concise manner and can therefore make your API simpler and more desirable to use. Assume a basic Task object, which might look something like this: class Task(object): def name(self, name): self.


1 Answers

To enforce that type of thing, you'll need multiple types (to restrict what is available from which context) - or at the least a few interfaces:

public class Fluently : IFluentlyDoes, IFluentlyIs
{
    public IFluentlyIs Is(string lhs)
    {
        return this;
    }
    public IFluentlyDoes Does(string lhs)
    {
        return this;
    }
    Fluently IFluentlyDoes.EqualTo(string rhs)
    {
        return this;
    }
    Fluently IFluentlyIs.LessThan(string rhs)
    {
        return this;
    }
    Fluently IFluentlyIs.GreaterThan(string rhs)
    {
        return this;
    }
}
public interface IFluentlyIs
{
    Fluently LessThan(string rhs);
    Fluently GreaterThan(string rhs);
}
public interface IFluentlyDoes
{    // grammar not included - this is just for illustration!
    Fluently EqualTo(string rhs);
}
like image 147
Marc Gravell Avatar answered Sep 20 '22 15:09

Marc Gravell