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
Implemented DI property injection in addition to used dependency provider #3933
Implemented DI property injection in addition to used dependency provider #3933
Conversation
Ah I see. Instantiate an actor which requires services and also properties
The first ctor paramter will be resolved by autofac.
This is what I want to be covered. In my PR it would look like:
|
Mixed DI with manual argument insertion is not supported by default, which is pretty much the case with every other .NET framework in existence. If it's really needed (which honestly I doubt), you can always create 2 constructors: 1 for production system usage (which takes eg. ActorSystem's service resolver) and second used for testing. |
The use case I wanted to support is for production environment. My example was only taken from a unittest. I try to explain the use case in other words: If your actors doesnt need external dependencies you can use the props to initialize the actor with property values like an entity guid or so.
But if your actor also requires external dependencies you cant express this in the current DI implementation and I also didnt found anything in the new approach.
In the second second ctor only the last parameter "database" should be injected by DI. The underlying intention is: The current workaround is to create the actor with DI and send an "Initialization" message to the actor which would provide the additional properties (in the example above "customerID").
As we use this pattern often in our code and in my opinion this is no unusual case I thought it would be nice if this would be better supported when using DI. |
Maybe let me simply challenge the core concept: if you need to do fight so to make this work with dependency injection, what stops you from getting rid of dependency injection and just pass the necessary parameter explicitly? Remember that actors are statefull, can live for long time. They are not equivalent of controllers and patterns used there are not always applicable in actor systems. |
Of course I can pass all parameters explicit into the ctor but then I loose the nice benefit of DI but the idea was to support both. So
If it is not possible what I proposed we have to stick with the "Initialize" method approach as this would be the smaller workaround in my opinion. |
Completed as part of Akka.NET v1.4.15 |
The current implementation of Akka.DI only allows injection of services via a dependency resolver (e.g. AutoFac).
But when instantiating actors it would be nice to also allow creating props not only via IoC but also set properties for an actor (e.g. an entity guid).
Here is a proposal to inject properties which are provided on actor props creation time.
These properties will be set after the dependency provider has resolved the required services.
The API proposal can be checked out in the unit test "DependencyResolver_should_inject_property_values_into_DiPerRequestActor"
Requirements:
The current implementation should be independent of the used dependency resolver because here we just try to mimic additional ctor arguments for an actor.
Our current workaround was to send a "Initalize" message directly after creating an actor via DI and take care to resend this message in case of actor restart. But this is error prone and unnecessary.
What do you think?