-
Notifications
You must be signed in to change notification settings - Fork 4.9k
C# SDK samples should all use AddBotRuntime() in startup #3287
Description
The AddBotRuntime() extension function has been recently added to the SDK. This function registers with the Dependency Injection framework everything that is needed to run Adaptive Dialogs. This function builds the Composer runtime.
One of the important mechanisms at play here is that the various dependencies (the Adapter, Middleware, Storage, the Bot itself etc.) are added with the TryAddSingleton function, and this means they can be overridden.
While the default IBot that is registered actually runs Adaptive Dialogs it only takes a line of code in startup to override this with any other implementation. For example, an IBot implementation built in code derived from ActivityHandler. In other words there is nothing that limits this function to Adaptive Dialogs and Composer, it can be used to create the runtime for any IBot implementation.
Other than the obvious appeal of consistency and reuse, the advantage of using this approach is that the model shifts to one of overriding default working behavior with custom behavior. Previously the expectation was that the developer would have to know all the component parts and build up the runtime.
Although ultimately the developer who wishes to customize behavior will need to have an understanding of the various internal dependencies, what needs to be understood by the developer who just wants to get something working is significantly reduced.
This change also means there is an opportunity to consolidate aspects of the documentation as the way by which you customize and extend an Adaptive Dialog based Composer runtime is exactly the same way you customize and extend the runtime for a bot that was written all in code.
Specifically we should switch to using AddBotRuntime across all the SDK samples. For example:
services.AddSingleton<IBot, EchoBot>();
services.AddBotRuntime(Configuration);
and for a bot that uses Dialogs:
services.AddSingleton<MainDialog>();
services.AddSingleton<IBot, DialogBot<MainDialog>>();
services.AddBotRuntime(Configuration);