Search code examples
.netwinformscomponentsdesignercomponentmodel

How do I use the service locator implementation in System.ComponentModel for dependency injection in a WinForms application?


I'm trying to allow my business logic components to query for services when they are added to one of my form/control classes. For example, I might have a ClientManager class in my library, which encapsulates some business logic. It requires an ILoginManager instance to query for some data that it needs to operate.

The concrete ILoginManager instance is created in the WinForms application, for example as a singleton. I would like to be able to drop a ClientManager component onto a form, which would make the ILoginManager instance available to the component automatically.

From what I understand from this article on lightweight containers, I could achieve this by using GetService:

public class ClientManager : Component
{
   public ClientManager() {}
   public ClientManager(IContainer container) { 
       container.Add(this);
   } 

   public ILoginManager User 
   {
      // would really be cached in a private field
      get { return GetService(typeof(ILoginManager)) as ILoginManager; }
   }

   // does something that requires the User property to be set
   public void DoSomething(); 
}

I would then have a container that overrides GetService to return my instance:

public class MyContainer : Container
{
    ServiceContainer svc;

    public MyContainer() {
        svc = new ServiceContainer();
        svc.AddService(typeof(ILoginManager), GlobalAppStuff.LoginManager);
    }

    protected override object GetService(Type service) {
        return svc.GetService(service);
    }
}

As a standalone solution, this works fine, but I can't figure out how to integrate this into a designable control, since the designer always generates a default System.ComponentModel.Container container, and I don't know of any way to inject services into it.

The MSDN documentation is vague in describing how these concepts should be actually used. Is there any straightforward way to do this using the ComponentModel classes that's designer friendly?


Solution

  • Don't use System.IServiceProvider for DI - it is mainly intended for design-time use. For IComponent implementations, the VS designer will assign a value to the Site property, which is what enables the whole IServiceProvider mechanism to work, but that property will be null at run-time, which means that all your calls to GetService are going to fail.

    You would be better off using a proper DI Container, such as Castle Windsor, StructureMap etc.