Microsoft Unity- 解决问题
我希望这样做:
container.Resolve();
当它这样做时,它会将 IDependency 注入到底层实体对象中。但是,存储在容器内的依赖项需要一个 DependencyValue 类型的对象,该对象由 DependencyFactory 提供一个值。长话短说,我遇到的问题是,当 Unity 创建实现 IDependency 的类的实例时,我需要能够接管并插入不在容器中的自定义值。
这可能吗,或者我最好的选择是在容器上使用 RegisterInstance 并手动实例化对象?我更喜欢使用配置文件来存储映射,然后添加一些参数实例化的逻辑。这就是 ParameterOverrides 的目的吗?或者我是否需要为此添加扩展,或者什么?
谢谢。
I am looking to do this:
container.Resolve();
When it does this, its going to inject a IDependency into the underlying entity object. However, the dependency stored within the container requires an object of type DependencyValue, which is supplied a value from a DependencyFactory. So long story short, the issue I'm having is that I need to, when Unity creates the instance of class implementing IDependency, that I would be able to take over and insert a custom value not in the container.
Is that possible, or is my best bet to use RegisterInstance on the container and manually instantiate the objects? I prefer using the configuration file to store the mapping, and then add some logic for parameter instantiation. Is that what the ParameterOverrides is meant to do? Or do I need to add an extension for this, or what?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用静态工厂扩展?
Why don't you use the static factory extension?
如果您在容器中注册 DependencyFactory 的实现(通过代码或配置)并通过其接口解析会怎么样。然后您将获得注入的 DependencyFactory 并简单地从中请求必要的对象。
除非我对场景的理解不正确,否则您的类具有依赖项,该依赖项是在运行时通过工厂确定的。由于这正是 Unity 所做的,因此您可以使用正确的实例注册(例如使用 ID)来将 Unity 用作您的 DependencyFactory。
我希望这有帮助。
谢谢,
达米安
What if you register an implementation of the DependencyFactory in the container (either through code or config) an resolve through its interface. Then you would get the DependencyFactory injected and simply request the necessary object from it.
Unlesss I'm understanding the scenario incorrectly, your class has a dependency, which is determined at runtime through a factory. As this is exactly what Unity does, you could use the correct instance registrations (for example using IDs) to use Unity as your DependencyFactory.
I hope this helps.
Thanks,
Damian