Autofac Keyed/Named 方法不提供服务映射函数的重载
Autofac Keyed/Named methods do not provide overload with service mapping func
As
方法提供了一个带有 Func<Type, Type> serviceMapping
参数的重载,但 Keyed
和 Named
方法没有。它们分别只提供Func<Type, object> serviceKeyMapping
和Func<Type, string> serviceNameMapping
参数。
但是,我想向 RegisterAssemblyTypes
注册一组类型,对所有类型使用相同的键,但使用由类型本身决定的不同接口。我期待找到一个方法重载,例如 Keyed(object serviceKey, Func<Type, Type> serviceMapping)
或 Keyed(Func<Type, object> serviceKeyMapping, Func<Type, Type> serviceMapping)
.
这是 API 设计中的疏忽吗?还是我遗漏了什么?
API好像没有这个功能。但是,您可以将 As(Func<Type, Service> serviceMapping)
重载与 KeyedService
对象一起使用。
例如
builder.RegisterAssemblyTypes(typeof(Parent).Assembly)
.Where(t => t.IsAssignableTo<ICommon>())
.As(t => new KeyedService(keyObject, t.GetType().GetInterfaces()[0]));
KeyedService
在 Autofac.Core
命名空间中。没有 NamedService
对象,但您可以使用 KeyedService
和 string
As
方法提供了一个带有 Func<Type, Type> serviceMapping
参数的重载,但 Keyed
和 Named
方法没有。它们分别只提供Func<Type, object> serviceKeyMapping
和Func<Type, string> serviceNameMapping
参数。
但是,我想向 RegisterAssemblyTypes
注册一组类型,对所有类型使用相同的键,但使用由类型本身决定的不同接口。我期待找到一个方法重载,例如 Keyed(object serviceKey, Func<Type, Type> serviceMapping)
或 Keyed(Func<Type, object> serviceKeyMapping, Func<Type, Type> serviceMapping)
.
这是 API 设计中的疏忽吗?还是我遗漏了什么?
API好像没有这个功能。但是,您可以将 As(Func<Type, Service> serviceMapping)
重载与 KeyedService
对象一起使用。
例如
builder.RegisterAssemblyTypes(typeof(Parent).Assembly)
.Where(t => t.IsAssignableTo<ICommon>())
.As(t => new KeyedService(keyObject, t.GetType().GetInterfaces()[0]));
KeyedService
在 Autofac.Core
命名空间中。没有 NamedService
对象,但您可以使用 KeyedService
和 string