的UserService
构造函数有两个参数,一个IUnitOfWork
和IUserRepository
:统一:隐ResolvedParameter未命名注册
public UserService(IUnitOfWork unitofWork, IUserRepository userRepository)
{ ... }
我使用named registrations与Unity注册UserService
时IUnitOfWork
多个实例之间进行区分,所以容器,我需要明确指定参数使用InjectionConstructor
:
container.RegisterType<IUserService, UserService>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("someContext"),
new ResolvedParameter<IUserRepository>()
)
);
是否可以省略new ResolvedParameter<IUserRepository>()
?我希望Unity隐含地推导出这个参数,因为不需要命名注册。的代码应该是这样的:
container.RegisterType<IUserService, UserService>(
new InjectionConstructor(
new ResolvedParameter<IUnitOfWork>("someContext")
)
);
这将做的是,当我不需要使用InjectionConstructor
任何情况。
这很好。使用这种反射会有什么缺点(例如性能)?非常感谢你的帮助。 – davenewza
这使用与标准InjectionConstructor相同的庄园反射,所以它应该具有相同的性能。很高兴我能帮上忙! =) – TylerOhlsen