DryIoC 将参数传递给基于泛型类型参数的开放泛型服务的构造函数
DryIoC pass param to constructor of open generic service based on generic type parameter
假设我有这项服务和两个策略:
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,那么在 ISomeService 的解析尝试中传递 CustomStrategy。否则,将其保留为默认值为 null 的参数。
Ofc 第一个参数在这两种情况下都应该自动解析为已注册的依赖项。
现在不知道该怎么做,我应该使用拦截器吗?
您可以这样做(但是从您的问题中不清楚为什么您有两种策略 DefaultStrategy<TEntity>
和 CustomStrategy<TEntity>
)。
using System.Linq;
using NUnit.Framework;
namespace DryIoc.IssuesTests
{
[TestFixture]
public class SO_DryIoC_pass_param_to_constructor_of_open_generic_service_based_on_generic_type_parameter
{
[Test]
public void Test()
{
var container = new Container();
container.Register(typeof(ISomeService<>), typeof(SomeService<>), Reuse.Transient,
made: Parameters.Of.Details((req, p) =>
p.ParameterType.GetGenericDefinitionOrNull() == typeof(IStrategy<>) &&
p.ParameterType.GetGenericParamsAndArgs().Any(x => x.IsAssignableTo<IFoo>())
? null // the default injection behavior
: ServiceDetails.Of(value: null)) // otherwise return the `null` value
);
container.Register<ICurrentDbContext, MyDbContext>();
container.Register(typeof(IStrategy<>), typeof(DefaultStrategy<>));
var s1 = container.Resolve<ISomeService<OtherEntity>>();
Assert.IsNull(((SomeService<OtherEntity>)s1).Delete);
var s2 = container.Resolve<ISomeService<FooEntity>>();
Assert.IsNotNull(((SomeService<FooEntity>)s2).Delete);
}
public interface ISomeService<TEntity> {}
public class SomeService<TEntity> : ISomeService<TEntity>
{
public readonly IStrategy<TEntity> Delete;
public SomeService(ICurrentDbContext context, IStrategy<TEntity> delete = null) => Delete = delete;
}
public interface IFoo {}
public class FooEntity : IFoo {}
public class OtherEntity {}
public interface IStrategy<TEntity> {}
public class DefaultStrategy<TEntity> : IStrategy<TEntity> { }
public class CustomStrategy<TEntity> : IStrategy<TEntity> { }
public interface ICurrentDbContext {}
public class MyDbContext : ICurrentDbContext {}
}
}
假设我有这项服务和两个策略:
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,那么在 ISomeService 的解析尝试中传递 CustomStrategy。否则,将其保留为默认值为 null 的参数。
Ofc 第一个参数在这两种情况下都应该自动解析为已注册的依赖项。
现在不知道该怎么做,我应该使用拦截器吗?
您可以这样做(但是从您的问题中不清楚为什么您有两种策略 DefaultStrategy<TEntity>
和 CustomStrategy<TEntity>
)。
using System.Linq;
using NUnit.Framework;
namespace DryIoc.IssuesTests
{
[TestFixture]
public class SO_DryIoC_pass_param_to_constructor_of_open_generic_service_based_on_generic_type_parameter
{
[Test]
public void Test()
{
var container = new Container();
container.Register(typeof(ISomeService<>), typeof(SomeService<>), Reuse.Transient,
made: Parameters.Of.Details((req, p) =>
p.ParameterType.GetGenericDefinitionOrNull() == typeof(IStrategy<>) &&
p.ParameterType.GetGenericParamsAndArgs().Any(x => x.IsAssignableTo<IFoo>())
? null // the default injection behavior
: ServiceDetails.Of(value: null)) // otherwise return the `null` value
);
container.Register<ICurrentDbContext, MyDbContext>();
container.Register(typeof(IStrategy<>), typeof(DefaultStrategy<>));
var s1 = container.Resolve<ISomeService<OtherEntity>>();
Assert.IsNull(((SomeService<OtherEntity>)s1).Delete);
var s2 = container.Resolve<ISomeService<FooEntity>>();
Assert.IsNotNull(((SomeService<FooEntity>)s2).Delete);
}
public interface ISomeService<TEntity> {}
public class SomeService<TEntity> : ISomeService<TEntity>
{
public readonly IStrategy<TEntity> Delete;
public SomeService(ICurrentDbContext context, IStrategy<TEntity> delete = null) => Delete = delete;
}
public interface IFoo {}
public class FooEntity : IFoo {}
public class OtherEntity {}
public interface IStrategy<TEntity> {}
public class DefaultStrategy<TEntity> : IStrategy<TEntity> { }
public class CustomStrategy<TEntity> : IStrategy<TEntity> { }
public interface ICurrentDbContext {}
public class MyDbContext : ICurrentDbContext {}
}
}