S
S
Stas Volyansky2016-10-05 20:28:08
ASP.NET
Stas Volyansky, 2016-10-05 20:28:08

How can I use IoC container IServiceProvider in .NET Core Class Library?

The application architecture is three-tiered:

  • The presentation layer is an ASP.NET Core project;
  • Business logic and data access levels - Class Library Core projects.

As you know, .NET Core provides its own IoC container - IServiceProvider.
The connection between the presentation layer and the business logic is provided in the Startup class of an ASP.NET Core project:
public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IService<Person>, PersonService>();
        }

I would like to know how I can implement this approach to connect business logic and data access layers?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-10-06
@stasphere

And what about IoC? I always thought that IoC is the opposite, to break the connection between the layers.
services.AddTransient, PersonService>(); this line does not provide a link, it only tells the container which particular class to instantiate when requested from the container.
if you need the data context to be passed to the service constructor, then add it to the IoC container
services.AddTransient<DdContext>(),
and add a parameter to the service constructor, for example,
Those when you request an IService instance, the container, let's say, will recursively resolve dependencies, those having seen the service dependency from DbContext, and knowing the registered class, the container will first create a DbContext, then create a PersonService, passing the created DbContext instance to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question