如何使用 System.ComponentModel 中的服务定位器实现在 WinForms 应用程序中进行依赖项注入?

发布于 2024-08-05 15:13:18 字数 1461 浏览 4 评论 0原文

我试图允许我的业务逻辑组件在将它们添加到我的表单/控件类之一时查询服务。例如,我的库中可能有一个 ClientManager 类,它封装了一些业务逻辑。它需要一个 ILoginManager 实例来查询它需要操作的一些数据。

具体的 ILoginManager 实例是在 WinForms 应用程序中创建的,例如作为单例。我希望能够将 ClientManager 组件拖放到表单上,这将使 ILoginManager 实例自动可供该组件使用。

据我了解这篇关于轻量级容器的文章,我可以通过使用 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(); 
}

然后我将有一个覆盖 GetService 的容器来返回我的实例:

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);
    }
}

作为一个独立的解决方案,这工作得很好,但我不知道如何将其集成到可设计的控件中,因为设计器总是生成一个默认的 System.ComponentModel.Container 容器,并且我不知道有什么方法可以将服务注入其中。

MSDN 文档对于如何实际使用这些概念的描述含糊不清。有没有任何简单的方法可以使用设计者友好的 ComponentModel 类来执行此操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2024-08-12 15:13:19

不要将 System.IServiceProvider 用于 DI - 它主要用于设计时使用。对于 IComponent 实现,VS 设计器将为 Site 属性分配一个值,这使得整个 IServiceProvider 机制能够工作,但该属性在运行时将为 null,这意味着对 GetService 的所有调用都将失败。

你最好使用合适的 DI 容器,例如 Castle Windsor、StructureMap 等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文