0

アセンブリのリストを返す GetAssemblies メソッドと、ConventionBuilder を返す GetConventions というメソッドがあると仮定すると、次のようにコンテナーを構成できます。

CompositionHost container =
    new ContainerConfiguration()
        .WithAssemblies(
            GetAssemblies(), 
            GetConventions())
        .CreateContainer();

しかし、次のように構成することもできます。

CompositionHost container =
    new ContainerConfiguration()
        .WithAssemblies(GetAssemblies())
        .WithDefaultConventions(GetConventions())
    .CreateContainer();

質問: これら 2 つの方法の違いは何ですか?

私を投げているのは、WithDefaultConventions の「デフォルト」という言葉です。MSDN は、これが何を意味するのかをあまり明らかにしていません。メソッドが単純に WithConventions と呼ばれていた場合、私はそれを再考しなかったでしょう。


以下のメソッドの例。

GetAssembly:

private static IEnumerable<Assembly> GetAssemblies()
    {
        return new[]
        {
            typeof(FileQueue).Assembly, 
            typeof(LoggerExport).Assembly, 
        };
    }

GetConventions:

private static ConventionBuilder GetConventions()
    {
        var conventionBuilder = new ConventionBuilder();

        conventionBuilder
            .ForType<OmsDbContext>()
            .Export<OmsDbContext>()
            .SelectConstructor(ctorInfos => ctorInfos.First())
            .Shared("Boundary.UnitOfWork");

        return conventionBuilder;
    }
4

1 に答える 1

0

あなたの例では、2 つのことは同じことを行う必要があります。ただし、WithAssemblies を複数回呼び出す場合は、WithDefaultConventions() を 1 回だけ呼び出して、これらすべての呼び出しからこれらの規則をアセンブリに適用できます。そうしないと、同じ効果を得るために、WithAssemblies() メソッドの 2 番目のパラメーターとして毎回規則を指定する必要があります。

残念ながら、Microsoft 自体から Microsoft.Composition パッケージに関する情報があまり見つかりません。

于 2015-05-21T07:20:28.693 に答える