O'Reilly logo

Hands-On Design Patterns with C# and .NET Core by Jeffrey Chilberto, Gaurav Aroraa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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>()); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required