Microsoft Unity- Issue With Resolve

落花浮王杯 提交于 2019-12-02 13:59:00

问题


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.


回答1:


Why don't you use the static factory extension?

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



回答2:


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



来源:https://stackoverflow.com/questions/3815581/microsoft-unity-issue-with-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!