How to design a Fluent Interface?

Afraz Ali

I have been trying to design a fluent interface for one of my frameworks and it seems that I can't understand one piece of the puzzle. I understand that I can use interfaces and classes to kind of drive which methods I want to call. However consider the following scenario.

Lets Say I have a Person class and I want to be able to do something like

Person.WithName("Steve").WithAge(18).Save();

Simmilarly I also want the caller of my API to do something like

Person.WithName("Steve").Save();

or

Person.WithAge(18).Save();

But I don't want the user to call the save method alone like

Person.Save();

Now if I want to design such an API how can I implement it? If i return instance of Person class from WithName and WithAge methods then I have to put the Save method in the Person class as well which means user can call it directly.

Jay

As you indicated, you can use interfaces to control what is visible. Using explicit interface implementation lets you hide methods in some cases and expose them in others. It also lets you have more than one method with the same signature.

In this case, we have a private constructor, so the Person can only be created using one of the static entry points. Once we have either a name or an age, we return an instance of person, and it is valid to call WithName, WithAge or Save.

public class Person : IPersonBuilder
{
  private string _name;
  private int? _age;

  private Person() { }

  public static IPersonBuilder WithName(string name)
  {
    return ((IPersonBuilder)new Person()).WithName(name);
  }

  public static IPersonBuilder WithAge(int age)
  {
    return ((IPersonBuilder)new Person()).WithAge(age);
  }

  IPersonBuilder IPersonBuilder.WithName(string name)
  {
    _name = name;
    return this;
  }

  IPersonBuilder IPersonBuilder.WithAge(int age)
  {
    _age = age;
    return this;
  }

  public void Save()
  {
    // do save
  }
}

public interface IPersonBuilder
{
  IPersonBuilder WithName(string name);
  IPersonBuilder WithAge(int age);
  void Save();
}

If Person is a class with meaning beyond the fluent interface -- it is some kind of entity -- then I would create a single static entry point that returns a PersonBuilder object and move all of the rest of the fluent concerns out of Person.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Design Fluent Async Operations?

From Dev

Understanding of How to Create a Fluent Interface

From Dev

How to create a Fluent Interface with Generics

From Dev

How to properly design interface

From Dev

How create Fluent Interface in C# with some limitation for some methods?

From Dev

How can I DRY out this F# code? (Fluent Interface)

From Dev

How to handle a fluent interface where each step can be a terminal operation?

From Dev

Recursive Generic and Fluent Interface

From Dev

Fluent interface with inheritance in Delphi

From Dev

How strict should be interfaces (a good interface design)

From Dev

Fluent Design System CSS - improvement

From Dev

Fluent Design card lift effect

From Dev

Use Fluent Interface with less Code

From Dev

Use Fluent Interface with less Code

From Dev

Unit testing a fluent interface with Mockito

From Dev

Title Bar with Fluent Design System - UWP App

From Dev

How to avoid the fat/polluted interface in this scenario of C++ design - OOPS?

From Dev

how to design User Interface compatible with any screen size

From Dev

Interface Segregation Principle involving several classes - how to design properly?

From Dev

ModelBuilder fluent based on POCO Interface partcipation

From Dev

Fluent Interface to Build a Directed Cyclic Graph?

From Dev

Office 2016 Fluent User Interface Control Identifiers

From Dev

PHP OOP : Fluent interface and tree graphs

From Dev

Java - Using Generics to Create a Fluent Interface

From Dev

Office 2016 Fluent User Interface Control Identifiers

From Dev

Is it possible to detect if fluent interface was called for the first time?

From Dev

Strange interface design Java

From Dev

Strange interface design Java

From Dev

module interconnection interface design

Related Related

  1. 1

    How to Design Fluent Async Operations?

  2. 2

    Understanding of How to Create a Fluent Interface

  3. 3

    How to create a Fluent Interface with Generics

  4. 4

    How to properly design interface

  5. 5

    How create Fluent Interface in C# with some limitation for some methods?

  6. 6

    How can I DRY out this F# code? (Fluent Interface)

  7. 7

    How to handle a fluent interface where each step can be a terminal operation?

  8. 8

    Recursive Generic and Fluent Interface

  9. 9

    Fluent interface with inheritance in Delphi

  10. 10

    How strict should be interfaces (a good interface design)

  11. 11

    Fluent Design System CSS - improvement

  12. 12

    Fluent Design card lift effect

  13. 13

    Use Fluent Interface with less Code

  14. 14

    Use Fluent Interface with less Code

  15. 15

    Unit testing a fluent interface with Mockito

  16. 16

    Title Bar with Fluent Design System - UWP App

  17. 17

    How to avoid the fat/polluted interface in this scenario of C++ design - OOPS?

  18. 18

    how to design User Interface compatible with any screen size

  19. 19

    Interface Segregation Principle involving several classes - how to design properly?

  20. 20

    ModelBuilder fluent based on POCO Interface partcipation

  21. 21

    Fluent Interface to Build a Directed Cyclic Graph?

  22. 22

    Office 2016 Fluent User Interface Control Identifiers

  23. 23

    PHP OOP : Fluent interface and tree graphs

  24. 24

    Java - Using Generics to Create a Fluent Interface

  25. 25

    Office 2016 Fluent User Interface Control Identifiers

  26. 26

    Is it possible to detect if fluent interface was called for the first time?

  27. 27

    Strange interface design Java

  28. 28

    Strange interface design Java

  29. 29

    module interconnection interface design

HotTag

Archive