views:

207

answers:

1

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?

+1  A: 

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.

Mark Seemann
Castle doesn't allow me to drag my components to a form because it uses constructor injection. StructureMap uses a static object which I'm not fond of since it ties my objects specifically to StructureMap. It seems like basic .NET components already support most of what I need though, it's just the trouble of getting it to work with the designer.
Ilia Jerebtsov
Perhaps, but even if it's difficult, it will *only* work in the designer. IComponent.Site will be null at run-time, but it is that ISite value that provides the IServiceProvider implementation, so it's not going to work at run-time.
Mark Seemann
I'm not sure if that's all there is to it. The designer doesn't set the ISite - the container does. The designer uses its own System.ComponentModel.Design.DesignerHost container at design time. One should be able to supply one's own container at runtime, and Visual Studio actually generates code for that, but I don't know how to override the container type that it generates.
Ilia Jerebtsov
You may be able to do that, but it's not what it was intended for.
Mark Seemann
Check out ObjectBuilder from the Patterns and Practices group. http://www.codeplex.com/ObjectBuilder
Travis Heseman
Mark Seemann