如何使用Unity容器解析通用类作为构造函数参数解析通用接口

发布于 2024-12-25 16:02:49 字数 1951 浏览 2 评论 0原文

我不知道如何向我的 Unity (2.0) 容器注册具有注入通用接口的通用抽象类。这是接口和类:

public interface IReportFactory<in TEntity, out TReport>
{ ... }

public class ExperienceReportBaseFactory :
    IReportFactory<Experience, ExperienceReportBase>
{ ... }

public abstract class ReportProvider<TEntity, TReport>
{
    protected ReportProvider(
        IReportFactory<TEntity, TReport> reportFactory)
    { ... }
    ...
}

public class ExperienceDetailsReportProvider
    : ReportProvider<Experience, ExperienceReportBase>
{
    public ExperienceDetailsReportProvider(
        IReportFactory<Experience, ExperienceReportBase>
        reportFactory)
        : base(reportFactory) { ... }
    ...
}

这是注册代码:

Container.RegisterInstance(typeof(IReportFactory<Experience, ExperienceReportBase>), new ExperienceReportBaseFactory())
         .RegisterType(typeof(ReportProvider<Experience, ExperienceReportBase>),
             typeof (ExperienceDetailsReportProvider),
             new InjectionConstructor(Container.Resolve(typeof (IReportFactory<Experience, ExperienceReportBase>))));

这是我的提供程序工厂,它尝试解析 ReportProvider<,>

public class ReportProviderFactory<TEntity> : IReportProviderFactory<TEntity>
{
    private readonly IServiceLocator _serviceLocator;

    public ReportProviderFactory(IServiceLocator serviceLocator)
    {
        _serviceLocator = serviceLocator;
    }

    public ReportProvider<TEntity, TReport> Create<TReport>()
        where TReport : ActiveReport
    {
        var reportProvider =
            _serviceLocator.GetService(typeof (ReportProvider<TEntity, TReport>)) as
                ReportProvider<TEntity, TReport>;
        return reportProvider;
    }
}

我得到,“无法构造类型 ReportProvider`2。您必须配置容器提供此值”,位于 _serviceLocator.GetService(...) 行。

I can't figure out how to register a generic abstract class that has a injected generic interface with my Unity (2.0) container. Here's the interface and classes:

public interface IReportFactory<in TEntity, out TReport>
{ ... }

public class ExperienceReportBaseFactory :
    IReportFactory<Experience, ExperienceReportBase>
{ ... }

public abstract class ReportProvider<TEntity, TReport>
{
    protected ReportProvider(
        IReportFactory<TEntity, TReport> reportFactory)
    { ... }
    ...
}

public class ExperienceDetailsReportProvider
    : ReportProvider<Experience, ExperienceReportBase>
{
    public ExperienceDetailsReportProvider(
        IReportFactory<Experience, ExperienceReportBase>
        reportFactory)
        : base(reportFactory) { ... }
    ...
}

Here's the registration code:

Container.RegisterInstance(typeof(IReportFactory<Experience, ExperienceReportBase>), new ExperienceReportBaseFactory())
         .RegisterType(typeof(ReportProvider<Experience, ExperienceReportBase>),
             typeof (ExperienceDetailsReportProvider),
             new InjectionConstructor(Container.Resolve(typeof (IReportFactory<Experience, ExperienceReportBase>))));

Here's my provider factory that's tries to resolve the ReportProvider<,>:

public class ReportProviderFactory<TEntity> : IReportProviderFactory<TEntity>
{
    private readonly IServiceLocator _serviceLocator;

    public ReportProviderFactory(IServiceLocator serviceLocator)
    {
        _serviceLocator = serviceLocator;
    }

    public ReportProvider<TEntity, TReport> Create<TReport>()
        where TReport : ActiveReport
    {
        var reportProvider =
            _serviceLocator.GetService(typeof (ReportProvider<TEntity, TReport>)) as
                ReportProvider<TEntity, TReport>;
        return reportProvider;
    }
}

I get, "The type ReportProvider`2 cannot be constructed. You must configure the container to supply this value," on the _serviceLocator.GetService(...) line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2025-01-01 16:02:49

我是个白痴...调用 ReportProviderFactory.Create<>() 的客户端正在为 TReport 指定不同的类型:

var reportProvider = _reportProviderFactory<Experience>.Create<ExperienceDetailsReport>();

这意味着我有错误在 ExperienceReportBaseFactoryExperienceDetailsReportProvider 上键入 TReport,因为客户端正在使用 .应该是:

public class ExperienceReportBaseFactory : IReportFactory<Experience, ExperienceDetailsReport> { ... }

public class ExperienceDetailsReportProvider : ReportProvider<Experience, ExperienceDetailsReport>
{
    public ExperienceDetailsReportProvider(IReportFactory<Experience, ExperienceDetailsReport> reportFactory) : base(reportFactory) { ... }
    ...
}

I'm an idiot... The client calling ReportProviderFactory<Experience>.Create<>() is specifying a different type for TReport:

var reportProvider = _reportProviderFactory<Experience>.Create<ExperienceDetailsReport>();

This means I have the wrong type for TReport on ExperienceReportBaseFactory and ExperienceDetailsReportProvider because the client that is using . It should be:

public class ExperienceReportBaseFactory : IReportFactory<Experience, ExperienceDetailsReport> { ... }

public class ExperienceDetailsReportProvider : ReportProvider<Experience, ExperienceDetailsReport>
{
    public ExperienceDetailsReportProvider(IReportFactory<Experience, ExperienceDetailsReport> reportFactory) : base(reportFactory) { ... }
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文