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
189
votes
10 answers

PHP method chaining or fluent interface?

I am using PHP 5 and I've heard of a new featured in the object-oriented approach, called 'method chaining'. What is it exactly? How do I implement it?
Sanjay Khatri
  • 4,153
  • 7
  • 38
  • 42
188
votes
20 answers

Method chaining - why is it a good practice, or not?

Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to…
Ilari Kajaste
  • 3,207
  • 2
  • 23
  • 25
66
votes
15 answers

Entity Framework Code First Fluent Api: Adding Indexes to columns

I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects. As an example lets say we have this employee class: public class Employee { public int EmployeeID { get; set; } public string EmployeeCode { get; set; } …
65
votes
7 answers

Fluent interfaces and inheritance in C#

I'll show a problem by example. There is a base class with fluent interface: class FluentPerson { private string _FirstName = String.Empty; private string _LastName = String.Empty; public FluentPerson WithFirstName(string firstName) …
bniwredyc
  • 8,649
  • 1
  • 39
  • 52
59
votes
4 answers

Conditional Builder Method Chaining Fluent Interface

I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level =…
Ken Burkhardt
  • 3,528
  • 6
  • 33
  • 45
57
votes
15 answers

Can you monkey patch methods on core types in Python?

Ruby can add methods to the Number class and other core types to get effects like this: 1.should_equal(1) But it seems like Python cannot do this. Is this true? And if so, why? Does it have something to do with the fact that type can't be…
airportyh
  • 21,948
  • 13
  • 58
  • 72
51
votes
5 answers

How to subclass str in Python

I am trying to subclass str object, and add couple of methods to it. My main purpose is to learn how to do it. Where I am stuck is, am I supposed to subclass string in a metaclass, and create my class with that meta, or subclass str directly? And…
yasar
  • 13,158
  • 28
  • 95
  • 160
48
votes
5 answers

How do I map a char property using the Entity Framework 4.1 "code only" fluent API?

I have an object that has a char property: public class Product { public char Code { get; set; } } Entity Framework doesn't seem to be able to map chars (this field is missing from the database when I create the database…
dommer
  • 19,610
  • 14
  • 75
  • 137
43
votes
8 answers

Tips for writing fluent interfaces in C# 3

I'm after some good tips for fluent interfaces in C#. I'm just learning about it myself but keen to hear what others think outside of the articles I am reading. In particular I'm after: when is fluent too much? are there any fluent patterns? what…
Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70
41
votes
6 answers

Creating Unique Index with Entity Framework 6.1 fluent API

I have a column "Name" that must be unqiue. No foreign key or anything like that. EF 6.1 finally supports creating such indexes via Annotations. That has been discussed already on SO. But it seems it can only be done via annotations in the classes.…
John
  • 3,591
  • 8
  • 44
  • 72
37
votes
5 answers

EF6.0 "The relationship could not be changed because one or more of the foreign-key properties is non-nullable"

If I try to delete a "child" row I always get an exception. Here is a snipset: using (var context = new CompanyContext()) { ItemType itemType = context.ItemTypes.FirstOrDefault(i => i.Name == "ServerType"); ItemTypeItem itemTypeItem =…
Max
  • 1,289
  • 3
  • 26
  • 50
34
votes
3 answers

Can I have an abstract builder class in java with method chaining without doing unsafe operations?

I'm trying to have an abstract base class for some builder classes so I can easily reuse code between the Builder implementations. I want my builders to support method chaining therefore a method has to return "this" instance of the most specific…
Patrick Huy
  • 975
  • 1
  • 8
  • 19
34
votes
4 answers

Python equivalents to LINQ

In C#, with LINQ, if I have en enumeration enumerable, I can do: // a: Does the enumerable contain an item that satisfies the lambda? bool contains = enumerable.Any(lambda); // b: How many items satisfy the lambda? int count =…
33
votes
8 answers

Fluent Interfaces - Method Chaining

Method chaining is the only way I know to build fluent interfaces. Here's an example in C#: John john = new JohnBuilder() .AddSmartCode("c#") .WithfluentInterface("Please") .ButHow("Dunno"); Assert.IsNotNull(john); [Test] public…
solrevdev
  • 8,863
  • 11
  • 41
  • 49
33
votes
5 answers

Fluent API with inheritance and generics

I'm writing a fluent API to configure and instantiate a series of "message" objects. I have a hierarchy of message types. To be able to access method of subclasses when using the fluent API, I used generics to parametrize the subclasses and make all…
Boj
  • 3,923
  • 3
  • 22
  • 39
1
2 3
29 30