解析为 null 而不是异常
Resolve as null instead of an exception
在我们的一个项目中,我们有以下场景:我们有一个工厂,它使用它的工厂创建另一个包的服务(我们的工厂基本上配置了底层工厂)。底层工厂使用流畅的语法,最小示例如下所示:
public class SomeFactory<T> where T: class
{
private readonly IFooService _fooService;
private readonly IBarService<T> _barService;
private readonly IUnderlyingFactory _factory;
public SomeFactory(
IFooService fooService, IBarService<T> barService, IUnderlyingFactory factory)
{
_fooService = fooService,
_barService = barService,
_factory = factory
}
public SomeService<T> Create()
{
return _factory
.WithFooService(_fooService)
.WithBarService(_barService)
.Create();
}
}
现在手头的问题是:对于一些 T
,我们已经实现了 IBarService<T>
。然而,底层包提供了一个默认实现(这是它内部的,所以我们不能一般地注册它),我们想要使用它以防我们没有那个 T
的实现。在那种情况下,我们根本不应该调用 .WithBarService
方法。
但是,如果我们不注册 IBarService<T>
,我们会从容器中得到一个异常,因为它无法解析它。
所以问题是: 可以配置容器,以便它 returns null 而不是抛出,当它无法解析 IBarService<T>
某些T
?或者有其他方法可以解决这个问题吗?
如果我们收到 null
,我们可以将 Create 方法更改为:
public SomeService<T> Create()
{
var service = _factory.WithFooService(_fooService);
if (_barService != null){
_factory.WithBarService(_barService);
}
return _factory.Create();
}
为了完整起见,这就是我们为某些 T
注册 BarService
的方式,即通过对实现类型的反思:
container.RegisterMany(
ourAssembly.GetTypes()
.Where(type => type.GetInterfaces().Any(i => i.IsGenericType
&& !type.IsAbstract)));
Can the container be configured so that it returns null instead of throwing, when it can't resolve IBarService for some T? Or is there another way to solve this problem?
您可以将 IBarService<T>
参数设为可选,这样如果服务未注册,容器将不会抛出:
public SomeFactory( IFooService fooService, IUnderlyingFactory factory,
IBarService<T> barService = null) { _fooService = fooService, _barService = barService, _factory = factory }
或者您可以将服务配置为使用 IfUnresolved.ReturnDefault
选项注入。
container.Register(typeof(SomeFactory<>),
made: Parameters.Of.Type(typeof(IBarService<>), ifUnresolved: IfUnresolved.ReturnDefault))
注意:我可能会把最后一个语法弄错,但应该是这样的。
在我们的一个项目中,我们有以下场景:我们有一个工厂,它使用它的工厂创建另一个包的服务(我们的工厂基本上配置了底层工厂)。底层工厂使用流畅的语法,最小示例如下所示:
public class SomeFactory<T> where T: class
{
private readonly IFooService _fooService;
private readonly IBarService<T> _barService;
private readonly IUnderlyingFactory _factory;
public SomeFactory(
IFooService fooService, IBarService<T> barService, IUnderlyingFactory factory)
{
_fooService = fooService,
_barService = barService,
_factory = factory
}
public SomeService<T> Create()
{
return _factory
.WithFooService(_fooService)
.WithBarService(_barService)
.Create();
}
}
现在手头的问题是:对于一些 T
,我们已经实现了 IBarService<T>
。然而,底层包提供了一个默认实现(这是它内部的,所以我们不能一般地注册它),我们想要使用它以防我们没有那个 T
的实现。在那种情况下,我们根本不应该调用 .WithBarService
方法。
但是,如果我们不注册 IBarService<T>
,我们会从容器中得到一个异常,因为它无法解析它。
所以问题是: 可以配置容器,以便它 returns null 而不是抛出,当它无法解析 IBarService<T>
某些T
?或者有其他方法可以解决这个问题吗?
如果我们收到 null
,我们可以将 Create 方法更改为:
public SomeService<T> Create()
{
var service = _factory.WithFooService(_fooService);
if (_barService != null){
_factory.WithBarService(_barService);
}
return _factory.Create();
}
为了完整起见,这就是我们为某些 T
注册 BarService
的方式,即通过对实现类型的反思:
container.RegisterMany(
ourAssembly.GetTypes()
.Where(type => type.GetInterfaces().Any(i => i.IsGenericType
&& !type.IsAbstract)));
Can the container be configured so that it returns null instead of throwing, when it can't resolve IBarService for some T? Or is there another way to solve this problem?
您可以将 IBarService<T>
参数设为可选,这样如果服务未注册,容器将不会抛出:
public SomeFactory( IFooService fooService, IUnderlyingFactory factory,
IBarService<T> barService = null) { _fooService = fooService, _barService = barService, _factory = factory }
或者您可以将服务配置为使用 IfUnresolved.ReturnDefault
选项注入。
container.Register(typeof(SomeFactory<>),
made: Parameters.Of.Type(typeof(IBarService<>), ifUnresolved: IfUnresolved.ReturnDefault))
注意:我可能会把最后一个语法弄错,但应该是这样的。