0

So I have read the article about dependency injection about Asp.net core:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1

But I don't see how it do the inversion of dependency

The way of the binding interface to the concrete class in the StartUp class ConfigureService method means that your main project has to reference whatever dependency is injected to. And what even worse, if your dependency reference the main project(use its models for example), you will get into circular dependency doing this way. Is there any way that you can inject the dependency from a separate third project(assembly)? How can you avoid this?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Matthew Wang
  • 3
  • 1
  • 5

1 Answers1

1

You should not depend on the main project, since it is the root of your application.

If you place your common interfaces, or your models for that matter, in a separate assembly then you can safely use or implement them without cross-references and avoid circular dependencies. This is what the dependency inversion is all about: Service -> Contract <- Implementation

With that being said, the way to register services without explicitly referencing their implementations in the code is to use the IHostingStartup interface. If an assembly has a class that implements the interface and a HostingStartup attribute with the type of the class, the ASP.NET Core Web Host will use it to enhance its configuration. Of course you will have to add a reference to that assembly first.

You can get more information from the docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/platform-specific-configuration

Ranopano
  • 51
  • 2