【发布时间】:2020-12-13 06:22:26
【问题描述】:
假设我有这个服务和两个策略:
public class SomeService<TEntity> : ISomeService<TEntity>
{
public SomeService(ICurrentDbContext context, IStrategy<TEntity>? delete = null)
: base(context, delete ?? new DefaultStrategy<TEntity>())
{}
}
public class DefaultStrategy<TEntity> : IStrategy<TEntity> {}
public class CustomStrategy<TEntity> : IStrategy<TEntity> {}
我当前的服务注册如下:
container.Register(typeof(ISomeService<>), typeof(SomeService<>), Reuse.Transient);
我想在这里实现的是如果泛型类型参数 T 实现了某个接口,例如 IFoo.否则,将其保留为默认值为 null 的参数。
在这两种情况下,第一个参数都应该被自动解析为已注册的依赖项。
暂时不知道该怎么做,我应该使用拦截器吗?
【问题讨论】: