0

假设我有一个方法 GetAssemblies,它返回一个程序集列表,还有一个名为 GetConventions 的方法,它返回一个 ConventionBuilder,我可能会像这样组成我的容器:

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

但我也可以这样写:

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

问题:这两种方式之间有什么区别(如果有的话)?

正是 WithDefaultConventions 中的“默认”一词让我感到震惊。MSDN 并没有说明这意味着什么。如果该方法被简单地称为 WithConventions,我不会再考虑它了。


下面的示例方法。

获取程序集:

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

获取约定:

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

在您的示例中,这两件事应该做同样的事情。但是,如果您要多次调用 WithAssemblies,则可以仅调用一次 WithDefaultConventions() 以将这些约定应用于所有这些调用的程序集。否则,您必须每次都提供约定作为 WithAssemblies() 方法的第二个参数,以达到相同的效果。

太糟糕了,您没有从 Microsoft 本身找到有关 Microsoft.Composition 包的太多信息。

于 2015-05-21T07:20:28.693 回答