-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Extend DI documentation for classes with multiple interfaces #19280
Comments
hi @puschie286 Can you share some code to explain this? Thanks. |
sure: interface IFirstSingle;
interface IFirstMulti;
interface ISecondSingle;
interface ISecondMulti;
[ExposeServices( typeof( SingleInstance ), typeof( IFirstSingle ), typeof( ISecondSingle ) )]
class SingleInstance : IFirstSingle, ISecondSingle, ISingletonDependency;
[ExposeServices( typeof( IFirstMulti ), typeof( ISecondMulti ) )]
class MultipleInstances : IFirstMulti, ISecondMulti, ISingletonDependency;
public class TestClass
{
public void Test( IServiceProvider provider )
{
var firstSingle = provider.GetService<IFirstSingle>();
var secondSingle = provider.GetService<ISecondSingle>();
Debug.Assert( firstSingle == secondSingle );
var firstMulti = provider.GetService<IFirstMulti>();
var secondMulti = provider.GetService<ISecondMulti>();
Debug.Assert( firstMulti != secondMulti );
}
} its just example code, but this is the behavior you will see when using ABP auto registration process |
hi In this case, you only added 2 services(
You have to add a new service to the DI to [ExposeServices( typeof( IFirstMulti ), typeof( ISecondMulti ) )]
class MultipleInstances : IFirstMulti, ISecondMulti, ISingletonDependency; |
Yes, i understand that - but its still somehow surprising that ExposeServices can change the behavior. Would be great if the documentation state something like "If you dont expose the class itself, each interface get their own instance" |
Thanks. I will mention this in the document. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
The documentation doesn't describe how exposing the interfaces for a class with multiple interfaces can change the instantiation behavior (for singleton and scoped lifetime)
Describe the solution you'd like
We have 2 behaviors:
This should be made clear in the ExposeService Attribute section.
But the behavior itself gives you two limits:
( well you can always fall back to manually register those cases )
Additional context
This is somehow surprising: you might try to prevent the usage of the implementation class by only exposing the interfaces but result in having multiple singleton instances (one for each interface).
Maybe this is even classified as bug, because you change the behavior (that is defined by the Dependency attribute) by the way you use the ExposeServices attribute
The text was updated successfully, but these errors were encountered: