Open Generics & dependency injection in the runtime #1374
jmzagorski
started this conversation in
General
Replies: 1 comment 1 reply
-
What's worng with the current implementation using IoC container as a service provider?
IIUC, if you eliminate usage of DI framework and its magic reflection methods like |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I read through a few issues/discussions and understand how @louthy feels about DI frameworks 😄 , and I appreciate that philosophy.
I have an application that heavily uses a DI framework to setup our services, and I find it beneficial for open generic interfaces. For example, a common interface I often create is
IValidator<TCommand>
whereTCommand
changes for each endpoint in a Web API application. I can register this service once with a DI framework, such ascontainer.Register(typeof(IValidator<>), assemblies)
without having to update the bootstrap code for new implementations. Alternatively, I cancontainer.Register(typeof(IValidator<>), typeof(GenericValidator<>))
, and the framework will create theGenericValidator<SomeModel>
when needed, without requiring me to register every "Model" in the bootstrap.I am trying to refactor our application to use
Eff<TRuntime, T>
when services are needed, but I find myself passing the container to the runtime constructor and using it likeI understand I could pass an internal "env" instead of the container as mentioned #1188, but my questions is more about open generics and eliminating the DI framework
This requires the runtime to be an open generic, such as
ProdRuntime<TCommand>
, so that each endpoint can create an instance of the runtime based on the model it is using. The benefit is that I don't have to create a runtime class for each model, allowing me to share many of the same services such as validation, logging, authorization, caching etc.Are there better strategies for these open generics that could eliminate the DI framework? (I thought about replacing the interfaces with delegates, but run into the same issue)
Here is a complete example of a Runtime for background workers that caches
T
models, so every worker would get a new instance of the runtime with a differentT
model.Beta Was this translation helpful? Give feedback.
All reactions