-
-
Notifications
You must be signed in to change notification settings - Fork 447
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
V4 projections and DI containers #1670
Comments
I'm vetoing this for now. |
Hi sorry for asking in this long closed issue. Is there any way to perform DI into projections now? I would like to inject asp.net core ILogger into my projections. |
|
Hi @mysticmind, I don't see any DI in the custom projection you shared? |
here's an approach that keeps the "complexity" low, from the dev point of view
I am sure there are devils in the details, like... This works for me and my specific use case. Your experience will be different 😄 Do not use this if one of your dependencies is |
So, we know that some projections are gonna want to use services registered in the application's DI container. V2/3 has the
LazyLoadedProjection
to work around this a little bit, but for V4 let's do something more systematic and take advantage of the generic host builder integration.Also, I'm killing off LazyLoadedProjection as part of the V4 projections anyway:)
If the projection's dependencies are all singletons
Meaning in this case that the projection only needs a single instance of the service dependency that can be used everywhere, we could:
AddMarten()
bootstrapping to just say "use this projection type from the DI container" likeoptions.Events.Projections.Async(Type type)
. I'd vote for no generic () registrations because you can't overload that w/ generic constraints. At bootstrapping time, the creation of the DocumentStore would build that upfront and stick it in the right placesoptions.Events.Projections.Inline(Func<IServiceProvider, IInlineProjection>)
that does the same. That gives you the option to useIConfiguration
andIHostingEnvironment
as well. Not that you can't use that other wise inAddMarten()
anyway though, so maybe nevermind.If the projection needs to use a new instance of the service dependency every time...
One, let's try really hard not to need this because the complexity would skyrocket.
Could use method injection ala ASP.Net Core controller actions like:
Apply(MyEvent e, MyAggregate a, [FromServices] ISomething)
. That makes Marten responsible for doing service location, lifecycle management through scoped containers, disposal, and generally slows the projections down quite a bit.Or force the author of the custom projection to deal with all that themselves and just have them inject
IServiceProvider
into their own projection class and make them deal with all that crap themselves. Serves them right for doing funky stuff like this anyway.The text was updated successfully, but these errors were encountered: