C#C
C#4y ago
becquerel

Dependency injection, but critical resources are only available after building the service provider

I'm trying to introduce dependency injection to a monogame project. For those unaware, Monogame gives you a Game class with some crucial components (GraphicsDevice and ContentManager) for getting stuff drawn to the screen. These components are only initialized after you construct your Game instance and call .Run() on it, making it 'live'.

So far I've been creating my service provider manually inside the Game class and using it as a faux-entry point. However, I really want to move the DI outside and resolve Game like anything else. This is mainly because it'll let me use proper async/await, which I currently can't do. I have a mix of UI services that rely on the Game components (GraphicsDevice etc.) and 'pure' backend services.

I can't have separate service providers for backend and frontend services, because the frontend relies on the backend. Is there a way to 'add' services to an IServiceProvider after it's been built?

Can give more detail as necessary.
Was this page helpful?