Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sure - the <a href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.ole.interop.iserviceprovider.aspx" rel="nofollow noreferrer">IServiceProvider</a> interface has been part of the Framework since 1.0. This isn't DI as it is typically discussed here (using a "kernel"), but it is IoC using the Service Locator pattern.</p> <p>If you dig into any of the Windows Forms Designer code, you'll see it peppered liberally with lines like this one:</p> <pre><code>IDesignerOptionService service = (IDesignerOptionService)this.GetService(typeof(IDesignerOptionService)); </code></pre> <p>If you're working with a <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.component.aspx" rel="nofollow noreferrer">Component</a>, then you get access to this via the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.component.site.aspx" rel="nofollow noreferrer">Site</a> property. It's quite common and practically required knowledge when creating custom controls.</p> <p>This is Service Location, textbook example. You've got a generic <code>IServiceProvider</code> that hands out abstract services you request by service type. If you ever want to create custom designers - smart tags and so on - then you need to know all of this. It's similar for ASP.NET as well.</p> <hr> <p><sub>P.S. Please don't <em>use</em> <code>IServiceProvider</code> in new code. It's a very old, non-generic interface. If you are creating reusable libraries that require an IoC container in order to work, you should use the <a href="http://commonservicelocator.codeplex.com/" rel="nofollow noreferrer">Common Service Locator</a> instead. But don't even use <em>that</em> unless you absolutely require that your library be agnostic of the DI library used at the application tier; most of the implementation-specific containers/kernels offer much richer APIs that you'll miss out on if you nail yourself to the CSL.</sub></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload