Microsoft Unity- 解决问题

发布于 2024-09-25 03:32:48 字数 368 浏览 3 评论 0原文

我希望这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美的太阳 2024-10-02 03:32:48

为什么不使用静态工厂扩展?

var container = new UnityContainer();
container.AddNewExtension()
         .Configure()
         .RegisterFactory(container =>
                          DependencyFactory.GetValue());

Why don't you use the static factory extension?

var container = new UnityContainer();
container.AddNewExtension()
         .Configure()
         .RegisterFactory(container =>
                          DependencyFactory.GetValue());
巾帼英雄 2024-10-02 03:32:48

如果您在容器中注册 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文