Pattern: FluentInterface
Fluent interface allows you do have method chaining.
Purpose of .NET implementation
The methods of a service collection instance is called multiple times( e.g. for .AddSingleton).
You want to ensure that the programmer can write code that is easy to write, easy to read, and easy to maintain.
So from each method you return the instance of the service collection, so you can chain the methods.
Example in .NET :
FluentInterface
FluentInterface example for Pattern FluentInterface
using Microsoft.Extensions.DependencyInjection;
using System.Data;
using System.Data.Common;
namespace FluentInterface;
internal static class FluentInterfaceDemo
{
public static ServiceCollection AddServices(this ServiceCollection sc)
{
//just for demo, does not make sense
sc
.AddSingleton<IComparable>((sp) =>
{
//does not matter
return 1970;
})
.AddSingleton<IComparable<Int32>>((sp) =>
{
//does not matter
return 16;
});
//this way you can chain the calls , making a fluent interface
return sc;
}
}
See Source Code for Microsoft implementation of FluentInterface
SourceCode Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingletonLearn More
WikipediaHomework
Implement a class person that you can see the first name and last name as fluent interface.