1

私の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 */);
    }
}

インポートされた各サービスを個別のパラメーターとして渡すこともできますが、それは多くの退屈なコードです。もっといいものがあるに違いない。何か案は?

4

3 に答える 3

1

これがあなたの質問に答えるかどうかは完全にはわかりませんが、コンストラクターインジェクションの使用を検討しましたか?

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

ImportingConstructor属性でコンストラクターをマークすることにより、基本的に、そのコンストラクターのすべてのパラメーターをインポートする必要があります。

于 2008-10-16T05:39:57.390 に答える
0

IImportsインポートしたすべてのサービスを含むインターフェースを作成し、それをどこにでも渡すことができます。そうすれば、クラスは好きなものを使用することも使用しないこともできます。しかし、それはすべてのクラスを一緒に結合します。

于 2008-10-13T19:19:40.403 に答える
0

これらのサービスを提供するためのインターフェイスを作成することを考えました。

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そのように消費者に結合したくありません。

于 2008-10-13T19:18:25.023 に答える