依赖注入 asp.net n 层 c#

Dependency injection asp.net n-tier c#

我正在使用 asp.net 核心 3.1

开发 N 层应用程序

在 application.domain 中,我将不得不接口和一个存储库

在我的数据层中,我将有 1 个接口

在我的 startup.cs 文件中,我会像下面这样调用每个依赖项注入。

services.AddTransient<IIntentRepository,IntentRepository>();
services.AddTransient<IPredictionRepository,PredictionRepository>();
services.AddTransient<IIntentDataRepository,IntentDataRepository>();

那我一般把Interface放在构造函数里比如

        private readonly IIntentDataRepository intentDataRepsoitory;
        public IntentRepository(IIntentDataRepository _intentDataRepsoitory)
        {
            intentDataRepsoitory = _intentDataRepsoitory;
        }

问题出在我的 PredictionRepository 我想在域层中调用 IntentRepository。我不知道该怎么做,这就是我在我的领域层中的称呼

    public class PredictionRepository : IPredictionRepository
    {
        private readonly IIntentRepository intentRepository;
        PredictionRepository(IIntentRepository _intentRepository)
        {
            intentRepository = _intentRepository;
        }
...}

但我不断收到 PredictionRepository 'could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.)'

异常消息看起来很准确:

... Ensure the type is concrete and services are registered for all parameters of a public constructor.

您粘贴的代码显示 PredictionRepository 构造函数未标记为 public,这就是 DI 机制无法使用它的原因。