Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Implement Generic Host #75

Open
Foxtrek64 opened this issue May 20, 2020 · 2 comments
Open

[Discussion] Implement Generic Host #75

Foxtrek64 opened this issue May 20, 2020 · 2 comments

Comments

@Foxtrek64
Copy link

A Generic Host encapsulates Dependency Injection, Logging, and Configuration in a nice, easy package.

Our current setup seems to want to achieve this but does this in a sort of homebrew way.

Changes to make to implement this:

  • Rewrite Program.cs to use a generic host
  • Services will be injected and hosted as either a singleton or in a scoped context.
  • Service and module dependencies will be injected (automatically) into the constructor.

Benefits of using a Generic Host:

  • Error handling is more intuitive
  • Scoped services can be created and destroyed as needed, proving some light scaling.
  • Resources are injected through the constructor, meaning they're easily available
  • Scoped ILogger<TService> instances
  • Configurations constructed from multiple sources, such as json configs, environment variables, or command line arguments, and injected as an IOptions<TConfiguration> object.
  • Easy implementation of libraries to assist the bot with growth, such as MediatR.

If this is something we might be interested in, I would be happy to build a PR with all of the necessary tests.

@ghost
Copy link

ghost commented May 23, 2020

The idea sounds good.
I'm wondering what you mean by the error handling being more intuitive?
Also, the current services might not be services that you're familiar with; they're more helper classes.

@Foxtrek64
Copy link
Author

"Intuitive" may not be the right word here but it helps reinforce good error handling practices by providing tools that tie in to establishing a leveled handling system. Anything fatal bubbles up all the way to the top whereas one-off issues (like network timeouts) can be handled locally in that service (through Polly or something similar). Teamed with Microsoft.Extensions.Logging and the ability to make scoped loggers, it makes it easy to track where errors came from on the other end.

And as far as services go, a host's definition of a service is rather weak. Essentially, anything that can be instantiated and consumed somewhere in the bot qualifies as a service. It has a few modes for this. Singletons used application-wide, scoped where a new instance is generated any time an object of that type is requested, but is consistent within that request, and transient where it's always a new object no matter what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant