使导入可用在您的代码周围

发布于 2024-07-07 18:05:57 字数 897 浏览 5 评论 0原文

在我的 MEF 用法中,我有一堆导入,我想在许多其他部分使用它们我的代码。 就像:

[Export (typeof (IBarProvider))]
class MyBarFactory : IBarPovider
{
    [Import]
    public IFoo1Service IFoo1Service { get; set; }

    [Import]
    public IFoo2Service IFoo2Service { get; set; }

    [Import]
    public IFoo3Service IFoo3Service { get; set; }

    [Import]
    public IFoo4Service IFoo4Service { get; set; }

    [Import]
    public IFoo5Service IFoo5Service { get; set; }

    public IBar CreateBar()
    {
        return new BarImplementation(/* want to pass the imported services here */);
    }
}

class BarImplementation : IBar
{
    readonly zib zib;

    public BarImplementation(/* ... */)
    {
        this.zib = new Zib(/* pass services here, too */);
    }
}

我可以将每个导入的服务作为单独的参数传递,但这是很多无聊的代码。 一定有更好的东西。 有任何想法吗?

In my MEF usage, I have a bunch of imports that I want to make available in many other parts of my code. Something like:

[Export (typeof (IBarProvider))]
class MyBarFactory : IBarPovider
{
    [Import]
    public IFoo1Service IFoo1Service { get; set; }

    [Import]
    public IFoo2Service IFoo2Service { get; set; }

    [Import]
    public IFoo3Service IFoo3Service { get; set; }

    [Import]
    public IFoo4Service IFoo4Service { get; set; }

    [Import]
    public IFoo5Service IFoo5Service { get; set; }

    public IBar CreateBar()
    {
        return new BarImplementation(/* want to pass the imported services here */);
    }
}

class BarImplementation : IBar
{
    readonly zib zib;

    public BarImplementation(/* ... */)
    {
        this.zib = new Zib(/* pass services here, too */);
    }
}

I could pass each imported service as an individual parameter, but it's a lot of boring code. There's gotta be something better. Any ideas?

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

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

发布评论

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

评论(3

微暖i 2024-07-14 18:05:57

我不完全确定这能回答您的问题,但您是否考虑过使用构造函数注入?

class BarImplementation : IBar
{
    [ImportingConstructor]
    public BarImplementation(IFoo1Service foo1, IFoo2Service foo2, ...) { }
}

通过使用 ImportingConstructor 属性标记构造函数,它实际上会使该构造函数的所有参数都需要导入。

I'm not entirely sure this answers your question but have you considered using the constructor injection yet?

class BarImplementation : IBar
{
    [ImportingConstructor]
    public BarImplementation(IFoo1Service foo1, IFoo2Service foo2, ...) { }
}

By marking your constructor with the ImportingConstructor attribute it will essentially make all the parameters of that constructor required imports.

将军与妓 2024-07-14 18:05:57

我考虑过创建一个接口来提供这些服务:

partial class BarImplementation
{
    public IRequiredServices
    {

        public IFoo1Service IFoo1Service { get; set; }
        public IFoo2Service IFoo2Service { get; set; }  
        public IFoo3Service IFoo3Service { get; set; }          
        public IFoo4Service IFoo4Service { get; set; }      
        public IFoo5Service IFoo5Service { get; set; }
    }
}

然后 MyBarFactory 实现 BarImplementation : BarImplementation.IRequiredServices。 这很容易编写,但是如何将它们传递给 Zib 呢? 我不想以这种方式将 Zib 与其消费者耦合。

I thought about making an interface to provide these services:

partial class BarImplementation
{
    public IRequiredServices
    {

        public IFoo1Service IFoo1Service { get; set; }
        public IFoo2Service IFoo2Service { get; set; }  
        public IFoo3Service IFoo3Service { get; set; }          
        public IFoo4Service IFoo4Service { get; set; }      
        public IFoo5Service IFoo5Service { get; set; }
    }
}

Then MyBarFactory implements BarImplementation : BarImplementation.IRequiredServices. That's easy to write, but then, how do I pass them down to Zib? I don't want to couple Zib to its consumer that way.

把时间冻结 2024-07-14 18:05:57

我可以使 IImports 成为一个包含我导入的所有服务的接口,将其传递到各处,然后类可以使用或不使用它们喜欢的任何一个。 但这将所有类结合在一起。

I could make IImports an interface that contains all the services I import, pass that around everywhere, and then classes can use or not use whichever they like. But that couples all the classes toegether.

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