Fluent Interfaces in C# .NET with Expression Builder Method Chaining and RPG Games

Introduction

Domain specific language (DSL) is a method of programming where variables, functions, and classes are descriptive of business-domain definitions and methodology. It allows software developers to more easily communicate with business matter experts and non-technical users, even to the point of allowing non-developers to manipulate programming logic and customize operation. Domain specific languages can be categorized into two areas, including Internal and External DSLs. Internal DSLs include programming logic and code, designed to incorporate business domain-specific terminology. External DSLs include separate scripts, text files, and other configuration files programmed in domain-specific keywords, and used to drive the software application.

In this tutorial, we’ll create a simple RPG role-playing game. We’ll create a character, assign his attribute statistics including strength, agility, intelligence, and class. We’ll then put him to battle against an endless barrage of enemies and see how long he lasts! We’ll focus on an internal domain specific language for our design. In particular, we’ll implement a fluent interface design, using the popular Expression Builder pattern with method chaining and progressive interfaces. The Expression Builder pattern is a popular design used in internal domain specific languages and it will allow us to create a fluent implementation of the Character class, leaving an expressive implementation of heroes and enemies in our programming logic.

A Hero Without a Cause

Since we’ll be creating a simple role-playing RPG game, we’ll need to first implement a basic character class. Before we get to the details of the class, assume it will contain various attributes for name, class, age, hit points, strength, agility, intelligence, gold, and possibly more. Instantiating the Character in a traditional fashion might be done using the following code:

Picking and Poking the Hero

1
2
3
4
5
6
7
8
9
10
11
12
13
//
// Instantiating a character by populating variable fields.
//
Character hero = new Character();
hero.Name = "King";
hero.Class = ClassType.Fighter;
hero.Age = 49;
hero.HP = 50;
hero.Strength = 17;
hero.Agility = 12;
hero.Intelligence = 15;
hero.Gold = 9999999;

In the above block of code, we’ve instantiated a Character class by using the empty constructor. We’ve then populated the Character by assigning values to each public field of the class. While the code is clear and cut, it’s not exactly expressive. We’re executing several assignment statements in no particular order. The logic flow is not well defined. From a programmer’s perspective, the above code would be perfectly suitable. However, from a business user perspective, perhaps we could define a more descriptive interface.

Breezing Through the Hero

1
2
3
4
5
//
// Instantiating a character through a constructor
//
Character hero = new Character("King", ClassType.Fighter, 49, 50, 17, 12, 15, 9999999);

We’ve trimmed the code down to a single line, instantiating the constructor of the Character class. This code is short and to-the-point. We can also gain any needed information on parameters through intellisense or comments on the class constructor. However, the code can still be more expressive. From a business user perspective, the code may even be un-readable.

A Fluent Hero

1
2
3
4
5
6
7
8
9
10
11
12
//
// Instantiating a character through a fluent interface.
//
characterBuilder.Create("King")
.As(ClassType.Fighter)
.WithAge(49)
.HP(50)
.Strength(17)
.Agility(12)
.Intelligence(15)
.Gold(9999999);

In the above code, we’ve defined a domain-specific fluent interface. The interface is a set of construction methods, each one returning itself (or the next-step interface, in the case of progressive interfaces), allowing us to construct a new character. The above code may be longer; it’s certainly not as short as the one-line implementation, but it is more expressive. In particular, non-technical business users may be able to better determine the functionality for the character and possibly even define their own.

Implementing a fluent interface is a relatively straight-forward task that can be done through the use of method chaining. Method chaining is simply a set of assignment methods that return itself (ie., this). The result from each method can then call the next assignment method, and so-on. To guide the user and enforce rules of construction (such as, the Class can only be assigned once, followed by attributes), we utilize progressive interfaces. Where the method would return “this”, we instead return an interface for the next step in line.

The Character Class Defined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Character
{
/// <summary>/// Enum for Character class type.
///</summary>
public enum ClassType
{
Fighter,
Mage,
Cleric,
Rouge
};

public string Name;
public ClassType Class;
public int Age;
public int Strength;
public int Agility;
public int Intelligence;
public int Gold;
public int HP;
public int MaxHP;
public bool IsAlive
{
get
{
return HP > 0;
}
}

public Character()
{
}

public Character(string name)
{
Name = name;
}

public Character(string name, ClassType classType, int age,int hp, int strength, int agility, int intelligence, int gold)
{
Name = name;
Class = classType;
Age = age;
MaxHP = hp;
HP = hp;
Strength = strength;
Agility = agility;
Intelligence = intelligence;
Gold = gold;
}

In the above code, we’ve defined the basic Character class, used in the beginning examples. The class consists of properties and attributes to utilize the character in our role playing game. The long constructor (allowing assignment of all properties in the constructor) is provided for non-fluent design. Notice, the class itself does not contain method chaining fluent methods. We’ll define the fluent implementation in a separate builder class, implementing the Expression Builder pattern. This allows us to separate our fluent-specific code from our class implementation. Developers who prefer not to use a fluent interface may then instantiate the Character class directly, while those who prefer the fluent interface can utilize the expression builder class.

The Expression Builder with Method Chaining: No Strings Attached

In our first implementation of an expression builder for a fluent interface of the Character class, we’ll implement the builder with no strings attached. That is, we won’t enforce rules for the order of assignment. While the code will be simpler, it does leave the builder class open to misuse. We can implement the basic expression builder with method chaining in C# .NET as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class CharacterBuilder
{
private Character _character;

public CharacterBuilder Create(string name)
{
_character = new Character(name);
return this;
}

public CharacterBuilder As(Character.ClassType classType)
{
_character.Class = classType;
return this;
}

public CharacterBuilder WithAge(int age)
{
_character.Age = age;
return this;
}

public CharacterBuilder Str(int strength)
{
_character.Strength = strength;
return this;
}

public CharacterBuilder Agi(int agility)
{
_character.Agility = agility;
return this;
}

public CharacterBuilder Int(int intelligence)
{
_character.Intelligence = intelligence;
return this;
}

public CharacterBuilder HP(int hp)
{
_character.HP = hp;
_character.MaxHP = hp;
return this;
}

public CharacterBuilder Gold(int gold)
{
_character.Gold = gold;
return this;
}

public Character Value()
{
return _character;
}
}

Notice in the above code, we’ve implemented the Expression Builder pattern using method chaining. The class itself constructs a Character for us. We may then call any of the assignment methods to populate the fields and attributes of the Character class. Each method returns a copy of itself, allowing us to chain the assignment methods, one after the other, thus implementing our fluent interface in C# .NET. We can then finally call the Value() method to obtain the completed Character class.

The Expression Builder with Progressive Interfaces: Locked-Down, and Ready to Go

While the above expression builder implements a fluent interface in C#, we can better guide the user when constructing the Character class by enforcing certain rules and order-of-events. In particular, after calling the Create() method, we can guide the user to assign Class property, using the method name “As”, a descriptive term for assigning the type of Character. After populating the Class, we can guide the user to assign an Age, followed by assigning the other attribute values.

This type of expression builder fluent interface is using Progressive Interfaces, which are designed to incrementally guide the construction of a fluent entity. Using this method of enforcing rules with progressive interfaces, we can force properties to be constructed while leaving others optional. We can also enforce the order by which properties are assigned (similar to a parameterized constructor), while allowing other properties to be assigned in any order.

We can update our Expression Builder with method chaining to use progressive interfaces in C# .NET as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class CharacterBuilder : ICharacterBuilderClassType,ICharacterBuilderAge, ICharacterBuilderStats
{
private Character _character;

public ICharacterBuilderClassType Create(string name)
{
_character = new Character(name);
return this;
}

public ICharacterBuilderAge As(Character.ClassType classType)
{
_character.Class = classType;
return this;
}

public ICharacterBuilderStats WithAge(int age)
{
_character.Age = age;
return this;
}

public ICharacterBuilderStats Str(int strength)
{
_character.Strength = strength;
return this;
}

public ICharacterBuilderStats Agi(int agility)
{
_character.Agility = agility;
return this;
}

public ICharacterBuilderStats Int(int intelligence)
{
_character.Intelligence = intelligence;
return this;
}

public ICharacterBuilderStats HP(int hp)
{
_character.HP = hp;
_character.MaxHP = hp;
return this;
}

public ICharacterBuilderStats Gold(int gold)
{
_character.Gold = gold;
return this;
}

public Character Value()
{
return _character;
}
}

In the above code, we’ve changed the return type from each of the assignment methods. Instead of returning “this”, which would give full control to assign any other method by the user, we return a copy of the specific interface. The interface will specify which methods may be called next. The interfaces can be defined as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public interface ICharacterBuilderClassType
{
ICharacterBuilderAge As(Character.ClassType classType);
}

public interface ICharacterBuilderAge
{
ICharacterBuilderStats WithAge(int age);
}

public interface ICharacterBuilderStats
{
ICharacterBuilderStats HP(int hp);
ICharacterBuilderStats Str(int strength);
ICharacterBuilderStats Agi(int agility);
ICharacterBuilderStats Int(int intelligence);
ICharacterBuilderStats Gold(int gold);
}

Each interface contains the next set of available assignment methods. The user will only be able to call the defined set of methods from within the fluent interface. Our expression builder implements each of the interfaces and returns them accordingly.

Our Mighty Hero Goes to Battle

With our expression builder defined, using method chaining and progressive interfaces in C# .NET, we can now utilize the fluent interface for the Character expression builder to create a hero and an endless supply of enemies. We can then automate the creation to create our simple role-playing game and watch them battle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
static void Main(string[] args)
{
Random ran = new Random((int)DateTime.Now.Ticks);
CharacterBuilder builder = new CharacterBuilder();

// Build our Character with the expression builder.
builder.Create("Valient")
.As(Character.ClassType.Fighter)
.WithAge(22)
.HP(20)
.Str(18)
.Agi(14)
.Int(12);

// Get our Character.
Character hero = builder.Value();

// Put our hero to battle against endless enemies, // and see how long he survives!
while (hero.IsAlive)
{
// Build an enemy with the expression builder.
builder.Create(CommonHelper.GenerateRandomName())
.As((Character.ClassType)ran.Next(4))
.WithAge(ran.Next(12, 200))
.HP(ran.Next(5, 12))
.Str(ran.Next(21))
.Agi(ran.Next(21))
.Int(ran.Next(21))
.Gold(ran.Next(50));

// Get our enemy.
Character enemy = builder.Value();

// Display start of battle.
CommonHelper.DisplayStartOfBattle(hero, enemy);

// Battle time!
CommonHelper.Battle(hero, enemy);
}

Console.ReadKey();
}

Notice in the above code, we’ve utilized the fluent interface to create a hero Character class. The code follows the internal domain specific language and is fully readable by both developer and business user (if there were one). We also create an enemy Character to fight our hero. The enemy is constructed in a similar fashion, using the fluent interface with method chaining in C# .NET. It’s interesting to note that we could take this a step further and implement an external domain-specific language, by providing a text file script for defining enemies. The script could utilize its own language for assigning the attributes. Our program would read the text file, parse the commands, and pass the data into the fluent interface to construct the entities. The fluent interface certainly opens the door to expressive design.

Program Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
= Starting Battle =  
Name: Valient
Class: Fighter
HP: 20/20
Age: 22
Str 18 / Agi 14 / Int 12
Gold: 0

vs.

Name: Mozriicous
Class: Mage
HP: 11/11
Age: 97
Str 12 / Agi 17 / Int 17
Gold: 19

An enemy approaches>

Valient (20/20) / Mozriicous (11/11)
Valient incinerates Mozriicous for 3 damage!
Mozriicous incinerates Valient for 2 damage!
>
Valient (18/20) / Mozriicous (8/11)
Valient slashes Mozriicous for 6 damage!
Mozriicous slashes Valient for 5 damage!
>
Valient (13/20) / Mozriicous (2/11)
Valient poisons Mozriicous for 3 damage!
Mozriicous impales Valient for 2 damage!
>
Our hero survies to fight another battle! Won 19 gold!
>

Download @ GitHub

You can download the project source code on GitHub by visiting the project home page.

Conclusion

Traditional programming code is often designed to be read and understood by programmers. However, as technology expands, code may be exposed to business users and non-technical audiences. By implementing fluent interfaces with expression builders, method chaining, and progressive interfaces, we can develop expressive code that enhances readability and is potentially easier to communicate to non-technical subject matter experts. Fluent interfaces can be a powerful tool to enhance understanding of code for both software developers and business domain experts.

About the Author

This article was written by Kory Becker, software developer and architect, skilled in a range of technologies, including web application development, machine learning, artificial intelligence, and data science.

Share