每次使用 unity 解析类型时注入新的构造函数参数
我最近遇到一个问题,我希望每次解决时都将新类型注入到请求的类型中。
我必须注册类型的当前代码是
container.RegisterType<IFirstT, FirstT>();
container.RegisterType<ISecondT, SecondT>();
container.RegisterType<IInjectableT, InjectableT>()
.Configure<InjectedMembers>()
.ConfigureInjectionFor<InjectableT>(
new InjectionConstructor(
container.Resolve<IFirstT>(),
container.Resolve<ISecondT>(),
)
);
我现在开始意识到每次解析 IInjectableT 时都会使用相同的注入构造函数。
InjectionConstructor 是否有可能每次都会统一创建新的依赖项?
我意识到我可以解决 InjectableT 构造函数内部的依赖关系并实现相同的目标,但是我打算让 IOC 控制这种类型的行为并选择是否应该注入新实例或将现有的传递给它。
I've just come across an issue recently where I want new types injected into the requested type every time it is resolved.
The current code I have to register the type is
container.RegisterType<IFirstT, FirstT>();
container.RegisterType<ISecondT, SecondT>();
container.RegisterType<IInjectableT, InjectableT>()
.Configure<InjectedMembers>()
.ConfigureInjectionFor<InjectableT>(
new InjectionConstructor(
container.Resolve<IFirstT>(),
container.Resolve<ISecondT>(),
)
);
I've now come to realise that the same injection constructor is being used every time I resolve the IInjectableT.
Is it possible that the InjectionConstructor will create new Dependencies everytime with unity?
I realise that I can just resolve the dependencies inside of the constructor of InjectableT and achieve the same thing, however I was intending for the IOC to controll this type of behaviour and choose if a new instance should be injected or an existing one passed to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 ResolvedParameter:
You should use ResolvedParameter: