Answer the question
In order to leave comments, you need to log in
How can I use IoC container IServiceProvider in .NET Core Class Library?
The application architecture is three-tiered:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IService<Person>, PersonService>();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question