IGrainFactory and DI
Is there any good sample on how to use any DI container for resolving grains or IGrainFactory from other classes?
Say I have MyService implementing IMyService. Then I have some ParentGrain injecting 'IMyService' in ctor. And then I have some IChildGrain which I want to use from MyService. How would I get IGrainFactory in MyService so that I can use it to create IChildGrain? Note that I want to inject IMyService into ParentGrain so ParentGrain.GrainFactory property cannot be used yet as ParentGrain is not constructed yet.
A sample from any popular DI container (or default Microsoft.Extensions.DependencyInjection) will be very much appreciated.
Hi Dixon, I can share a sample of using SimpleInjector (a very popular DI container) to resolve grains, please be patient until I'm back to my office with the code. I also added infrastructure in my service to define SimpleInjector scopes which are grain oriented which I can share. Regarding the IGrainFactory, it's not simple, to say the least. Here's why and how I solved it: In your example, MyService should use the IGrainFactoryinstance obtained from the ParentGrain, since the implementation may depend on the grain that exposes the factory (I say 'may' because it's an interface). I had the very same issue in my service, and as part of the infrastructure mentioned above I was able to create an injectable IContextProvider<IGrainContext> which provides access to IGrainFactory. The context provider is a singleton that can provides context objects, such as IGrainContext, scoped to the current grain which is the grain that is the root object in the current logical call stack.
Hi, @xiazen is currently working on registering IGrainFactory in the service collection (which would then end up in the default service provider or your own container). This is going through a internal pre-review process as it is the first of many internal types that we want to register in the service provider, but she'll have a PR for public review very soon.
This issue is related to #988.
@DixonDs, here are links to my Orleans DI code:
- OrServiceProvider class - used to tell apart the
IServiceProvidergenerated by Orleans and the one generated by my service. - OrleansServiceProviderBuilder class - the builder configured to Orleans to invoke at startup in order to get the
IServiceProviderto use. - ApplicationServiceProviderBootstrapper grain registration snippet - this bootstrapper class uses SimpleInjector DI container to create an
IServiceProviderwhich resolves my service's grains as well as other business objects. The linked snippet has the grains registration part, which also includes the context provider registration which is part of the infrastructure I mentioned.
Thanks for sharing @Eldar1205. BTW, which version of Orleans are you using? Because we no longer require (not even honor) grain type registrations, so you should be able to skip that scanning phase for types implementing Grain. Also the 2nd link you shared (with builder) actually points to the OrServiceProvider, as in the 1st link :)
@Eldar1205 . BTW To see an example of register dependencies required by grains. You can see DependencyInjectionGrainTests.cs in master branch
You just need to register dependencies to DI container/IServiceProvider, thenGrainCreatorwill resolve the dependency for your grain when creating one.
@Eldar1205 I actually came up with something very similar before I saw your reply (I guess there is no much room to do something different) The only other bit I've added is IBootstrapProvider implementation which uses providerRuntime.GrainFactory to set as IGrainFactory in the context object. Then the context object returns either the IGrainFactory instance set explicitly in boostraper (inside of silo case), or GrainClient.GrainFactory otherwise (outside of silo case)
You are assuming the grain factory is singleton in the silo. I prefer to not assume that unless explicitly documented בתאריך 11 בספט' 2016 3:28 PM, "Dmytro Dziuma" [email protected] כתב:
@Eldar1205 https://github.com/Eldar1205 I actually came up with something very similar before I saw your reply (I guess there is no much room to do something different) The only other bit I've added is IBootstrapProvider implementation which uses providerRuntime.GrainFactory to set as IGrainFactory in the context object. Then the context object returns either the IGrainFactory instance set explicitly in boostraper (inside of silo case), or GrainClient.GrainFactory otherwise (outside of silo case)
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/orleans/issues/2128#issuecomment-246177694, or mute the thread https://github.com/notifications/unsubscribe-auth/AFBYt0Cjwm16sWOk2TIHVqrjfaONgVvqks5qo_QFgaJpZM4J4AlK .
GrainFactory is a singleton in silo. I made a change to make GrainFactory injectable in #2192
We injected GrainFactotry as a singleton in DI container. See startupBuilder.cs
@DixonDs, With change #2192, it should be much easier to do what you're trying to do. The tests added in the change should be very close to the scenario you described.
@Eldar1205, While I agree that we should document that the grain factory is currently a singleton, the part that is unclear to me is how it should be exposed to the application layer. It is part of the actor framework and is not something that can currently be provided by application developers (other than by decorators). Other frameworks that use DI provide framework components to the application layer via DI, but I've not yet seen one that exposes a component that cannot be provided by the application.
We can minimally document that developers should not provide their own implementation. We can prevent them from providing their own implementation by making it IGrainFactory an abstract class with an internal constructor. We can work through the issues that prevent application developers from providing their own implementation (Xiazen has already prototyped this work, though we've not signed off on the change).
In the end, I tend to error on the side of practicality, so I think registering the grain factory so it can be injected into application code (as Xiazen has done in #2192, and documenting that it is a singleton that should not be replaced, is most likely sufficient. There will not likely be need to go beyond that.
Since https://github.com/dotnet/orleans/pull/2192 is merged, I guess this issue could be treated as resolved.