Questions tagged [fluent-interface]

Refers to a practice of coding object-oriented APIs with the aim of improving readability of the interface, normally implemented using method chaining. The phrase was first coined by Eric Evans and Martin Fowler.

Martin Fowler's explanation on what he considers to be the proper way to design fluent interfaces can be found at http://www.martinfowler.com/bliki/FluentInterface.html.

Related concepts:

437 questions
8
votes
3 answers

Fluent methods for data class in kotlin

We are familiar with fluent interfaces for calling methods in java and other programming languages. For eg: Picasso.with(this).load(url).into(imageView); This is made possible by setter methods what return object of desired type. public Picasso…
erluxman
  • 18,155
  • 20
  • 92
  • 126
8
votes
5 answers

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

See below codes : new ConditionCreator() .Add() .Or() .Add() .And() .Add() I want to create a Fluent Interface for that But I need, after Add() method developer see Only Or() or And() and after one…
HamedFathi
  • 3,667
  • 5
  • 32
  • 72
8
votes
3 answers

PHP OOP: Chainable objects?

I have tried to find a good introduction on chainable OOP objects in PHP, but without any good result yet. How can something like this be done? $this->className->add('1','value'); $this->className->type('string'); $this->classname->doStuff(); Or…
Industrial
  • 41,400
  • 69
  • 194
  • 289
8
votes
2 answers

Fluent interfaces in C#

I have a question with fluent interfaces. We have some objects that are used as parameter objects for a SQL interface, here's an example: using (DatabaseCommand cmd = conn.CreateCommand( "SELECT A, B, C FROM tablename WHERE ID = :ID", …
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
8
votes
4 answers

Shortcut in Fluent API to set multiple properties as required

Here is what I am currently doing: modelBuilder.Entity().Property(e => e.Name).IsRequired(); modelBuilder.Entity().Property(e => e.UPC).IsRequired(); modelBuilder.Entity().Property(e =>…
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
8
votes
6 answers

Persisting complex test data

We are using builder pattern to generate test data. These domain objects have relations between them. Our functional tests require these objects to be persisted. Think about this model: If I want a plain instance of C I do aNew().c().build() If I…
nimcap
  • 10,062
  • 15
  • 61
  • 69
7
votes
1 answer

Specifying Foreign Key Entity Framework Code First, Fluent Api

I have a question about defining Foreign Key in EF Code First Fluent API. I have a scenario like this: Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). Code: public class Person { public…
7
votes
5 answers

Are fluent interfaces a violation of the Command Query Separation Principle?

I started writing a fluent interface and took a look at an older piece Martin Fowler wrote on fluent interfaces (which I didn't realize he and Eric Evans coined the term). In the piece, Martin mentions that setters usually return an instance of the…
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
7
votes
3 answers

How can I combine fluent interfaces with a functional style in Scala?

I've been reading about the OO 'fluent interface' approach in Java, JavaScript and Scala and I like the look of it, but have been struggling to see how to reconcile it with a more type-based/functional approach in Scala. To give a very specific…
Alex Dean
  • 15,575
  • 13
  • 63
  • 74
7
votes
1 answer

Entity Framework Code First - Foreign Key to non primary key field

I have two tables that look like this: dbo.ReviewType ReviewTypeId INT PRIMARY KEY ShortName CHAR(1) - Unique Index Description dbo.Review ReviewId INT PRIMARY KEY ReviewType_ShortName CHAR(1) - FK to ReviewType ... A Review…
Dismissile
  • 32,564
  • 38
  • 174
  • 263
7
votes
5 answers

C# Building Fluent API for method invocations

What do I have to do to say that InvokeMethod can invoke a method and when using special options like Repeat it shall exexute after the Repeat. My problem for now is that the method will already exexute before it knows that it has to be called 100…
Rookian
  • 19,841
  • 28
  • 110
  • 180
7
votes
4 answers

Effects of method chaining

I know the benefits of chaining within PHP but lets say we have this following situation $Mail = new MailClass("mail") ->SetFrom("X") ->SetTo("X") ->SetSubject("X") ->AddRecipient("X") ->AddRecipient("X") …
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
7
votes
4 answers

Assert that value is equal to any of a collection of expected values

Does NUnit provide a constraint to find whether the actual value is the element of a given enumerable or array, in other words, that it is equal to any of multiple expected values? Something like: Assert.That(actual, Is.EqualToAnyOf(new[] { 1, 2, 3…
chiccodoro
  • 14,407
  • 19
  • 87
  • 130
7
votes
4 answers

method names with fluent interface

I have a Permissions class in Java with methods in fluent style like this: somePermissions.setRead(true).setWrite(false).setExecute(true) The question is, whether I should name these methods set{Property} or only {property}. The latter would look…
deamon
  • 89,107
  • 111
  • 320
  • 448
7
votes
4 answers

Fluently setting C# properties and chaining methods

I'm using .NET 3.5. We have some complex third-party classes which are automatically generated and out of my control, but which we must work with for testing purposes. I see my team doing a lot of deeply-nested property getting/setting in our test…
John Feminella
  • 303,634
  • 46
  • 339
  • 357