Skip to content
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

More examples of Singleton, Transient and Scoped Injection Dependency #41082

Open
zabpetrus opened this issue May 26, 2024 · 4 comments
Open
Labels
dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged

Comments

@zabpetrus
Copy link

Type of issue

Thank you

Description

I need to use two types of constructors: one for the application and another for the tester. So, I need to master the subject of dependency injection to avoid the aggregation error, which indicates circular references. I tried to implement the example given, but it didn't work. And there's just one example... What changes should be made to the classes to be able to do dependency injection? What should be added? what should be removed? Help me, because I'm using Moq and I can't remove the constructor with injection! I could give two examples: normal mode and using transient, scoped and singleton and teach how the student can migrate the modes to make it work. Thanks

Page URL

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection

Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/extensions/dependency-injection.md

Document Version Independent Id

cc5f00d0-fb50-1229-8fa2-e66b1f45b284

Article author

@IEvangelist

Metadata

  • ID: 8fd55089-b683-2202-6641-c7b61a132f14
  • Service: dotnet-fundamentals
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label May 26, 2024
@issues-automation issues-automation bot added dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 labels May 26, 2024
@IEvangelist
Copy link
Member

Show me what you're trying to do? If you could provide the code that you have, I might be able to help.

@zabpetrus
Copy link
Author

zabpetrus commented May 29, 2024 via email

@IEvangelist
Copy link
Member

Got it, thank you for the added context, that's very helpful! Ok, I see the issue. When you call:

builder.Services.AddScoped<IContatoAppService, IContatoAppService>();

That's registering a scoped IContatoAppService type with a corresponding implementation of the interface. That's not going to work, since it cannot instantiate an interface, nothing can. Instead, you need to register (or map) the corresponding implementation.

Consider this:

builder.Services.AddScoped<IContatoAppService, DefaultContatoAppService>();

In this example, the implemenation is the DefaultContatoAppService. It has to be defined somewhere:

public class DefaultContatoAppService : IContatoAppService
{
    // TODO: Implement members.
}

Does that make more sense now? Then for unit tests, you could mock the interface and use that.

@zabpetrus
Copy link
Author

zabpetrus commented Jun 9, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

3 participants