Implementation factory using a function

The implementation factory of the GetService() method can be used to determine the type of InventoryCommand class to be returned. For this example, a new static method is created in the InventoryCommand class:

public static Func<IServiceProvider, Func<string, InventoryCommand>> GetInventoryCommand =>                                                                             provider => input =>{    switch (input.ToLower())    {        case "q":        case "quit":            return new QuitCommand(provider.GetService<IUserInterface>());        case "a":        case "addinventory":            return new AddInventoryCommand(provider.GetService<IUserInterface>(), provider.GetService<IInventoryWriteContext>());        case "g":        case "getinventory": return new GetInventoryCommand(provider.GetService<IUserInterface>(), provider.GetService<IInventoryReadContext>()); ...

Get Hands-On Design Patterns with C# and .NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.