如何使用Unity容器解析通用类作为构造函数参数解析通用接口
我不知道如何向我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是个白痴...调用
ReportProviderFactory.Create<>()
的客户端正在为TReport
指定不同的类型:这意味着我有错误在
ExperienceReportBaseFactory
和ExperienceDetailsReportProvider
上键入TReport
,因为客户端正在使用 .应该是:I'm an idiot... The client calling
ReportProviderFactory<Experience>.Create<>()
is specifying a different type forTReport
:This means I have the wrong type for
TReport
onExperienceReportBaseFactory
andExperienceDetailsReportProvider
because the client that is using . It should be: