Apr 17, 2009 at 10:18 PM
Edited Apr 17, 2009 at 10:21 PM
I wonder whether it's possible to make Unity resolve an interface to null instead of building an instance. Particularly, when I need to route HTTP traffic produced by HttpWebRequest via Fiddler, I write something like this in my app.config
<type type="IWebProxy" mapTo="WebProxy">
<param name="Host" parameterType="string">
<param name="Port" parameterType="int">
<value value="8888" type="int"/>
However, if I'd like to get rid of the proxy (HttpWebRequest requires null as a Proxy property value in order to make direct "proxyless" call), I would need to make Unity return null each time some class/method/constructor asks for IWebProxy resolution.
Is it possible?
Yes, I know about Null Object pattern and in this particular situation it would be enough to implement IWebProxy interface and return true from IsBypassed method. Let's pretend for a moment that Null Object pattern is not applicable here. This could easily
be the case for some not so well thought-out interfaces.
It is possible, but isn't completely straightforward.
What you'll need to do is register an instance for the interface, and then use a custom TypeConverter in the configuration that returns null instead of a value for that instance.