Using Dependency Injection with .NET Core Class Library (.NET Standard)

32,620

You don't have to do anything in your class library. Only the main application has a composition root (earliest point in an application lifecycle you can set up your object graph).

This happens in Startup.cs in your ASP.NET Core application. There you also register your dependencies:

services.AddScoped<IUserManager,UserManager>();

That's it. Class libraries don't have a composition root. Neither they should, because you can't use them without an application and the IoC Container used is a choice of the application not of the library.

You can however provider convenience methods to do this registrations, like the AddXxx method common in ASP.NET Core or some kind of module system, like in 3rd party IoC containern like Autofac or Castle Windsor.

Share:
32,620
Banketeshvar Narayan
Author by

Banketeshvar Narayan

Senior Technical Consultant, Microsoft MVP(1 time), C# Corner MVP(3 times) working experience on: .NET Core, C#, AngularJS, Angular, Dapper, Web Applications, SignalR, Blazor, SQL Server, Oracle...

Updated on December 26, 2020

Comments

  • Banketeshvar Narayan
    Banketeshvar Narayan over 3 years

    I have gone through the link:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection

    and learnt that how I can use dependency injection for Web API.

    As mentioned in the above link I can use Startup (Startup.cs) class for dependency injection inside API layer. But how can achieve dependency injection for the .NET Core Class Library. Below is the screenshot how I am adding a class library. enter image description here

    And my project structure is

    enter image description here

    In the project “DataManagement.Repository” I have written a class “UserRepository” and in the project “DataManagement.Repository.Interfaces” written an Interface “IUserRepository”.

    In the project “DataManagement.Business” I have written a class “UserManager”

    class UserManager
        {
            private IUserManager _userManager;
            public UserManager(IUserManager userManager)
            {
                _userManager = userManager;
            }
        }
    

    As you can see that I am trying to achieve dependency injection through the constructor.

    But I am not sure that what changes need to be done for enabling dependency injection inside .NET Core Class Library (.NET Standard).

  • pejman
    pejman over 7 years
    as u said in answer, if i add this dependency in Startup.cs om asp.net core application and then i add it's reference to .NetCore class library, i can use it?
  • Tseng
    Tseng over 7 years
    Yes, if you use dependency injection all the way (and never instantiate a service new) it will work
  • pejman
    pejman over 7 years
    I asked a question about this subject in http://stackoverflow.com/questions/41342064/using-ihostingen‌​vironment-in-netcore‌​-library plz give me help if u can. thanks
  • WillC
    WillC over 5 years
    Does the .Api project then have to have a reference to the .Repository project in order to access the implementations of the .Repository interfaces?
  • Tseng
    Tseng over 5 years
    @WillC: Yea, since your composition root is the web application. You can abstract it a bit away, using Extension methods (such as ASP.NET Core does with the .AddXyz where you do the registrations within the .AddXyz` Method.
  • Tseng
    Tseng over 5 years
    You can additionally hide transitive dependencies with <ProjectReference Include="..\ClassLibrary\ClassLibrary.csproj" PrivateAssets="All" /> (by adding PrivateAssets="All" to the reference (in your Xyz.Extensions.DependencyInjection project that contains the .AddXyzmethod). This will hide the concrete implementations in your main application, so new ServiceA() will result in an error, so people won't accidentally. See github.com/dotnet/project-system/issues/…
  • kuldeep
    kuldeep almost 4 years
    my composition root is an api, where i load a plugin (dynamically). Now this plugin uses some library as nuget, how can i in this situation do registration for dependencies required by library ?