4

我有一个对象,它需要项目的顺序很重要的项目的有序列表(IOrderedEnumerable)。

public class OrderedListWorker : IListWorker
{
    private OrderedListWorker(IOrderedEnumerable<IListItem> orderedListItems)
    {
        foreach (var listItem in orderedListItems)
            listItem.DoSomethingWhereOrderMatters();
    }
}

我有多个类型的对象IListItem

如何OrderedListWorker在 Autofac 注册并确保在运行时按特定顺序获取 ListItems?

我看到这篇文章不保证订单,但我不确定如何保证订单。

4

2 回答 2

4

我有一个解决方案,它是IOrderedEnumerable, 和ResolveOrdered<TComponent>我上面链接到的帖子中的解决方案的组合。

使用 AutofacExtensions 类:

public static class AutofacExtensions
{
    private const string OrderString = "WithOrderTag";
    private static int _orderCounter;

    public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
        WithOrder<TLimit, TActivatorData, TRegistrationStyle>(
        this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registrationBuilder)
    {
        return registrationBuilder.WithMetadata(OrderString, Interlocked.Increment(ref _orderCounter));
    }

    public static IOrderedEnumerable<TComponent> ResolveOrdered<TComponent>(this IComponentContext context)
    {
        return
            context.Resolve<IEnumerable<Meta<TComponent>>>()
                   .OrderBy(m => m.Metadata[OrderString])
                   .Select(m => m.Value).OrderBy(c => true);
    }
}

我可以指定我的注册如下:

builder.RegisterType<ListItemA>().As<IListItem>().WithOrder();
builder.RegisterType<ListItemB>().As<IListItem>().WithOrder();
builder.RegisterType<OrderedListWorker>()
                   .As<IListWorker>()
                   .WithParameter(
                       new ResolvedParameter(
                           (info, context) => true,
                           (info, context) => context.ResolveOrdered<IListItem>()));
于 2013-07-25T17:45:45.097 回答
0

我知道这是一个老问题,但我今天遇到了类似的问题,我想我会分享我的解决方案。

登记:

protected override void Load(ContainerBuilder builder)
{
    builder.RegisterOrdered<IListItem>(scope =>
    {
        scope.Register<Item1>();
        scope.Register<Item2>();
    });
}

基础设施:

public static class AutofacExtensions
{
    public static void RegisterOrdered<TService>(this ContainerBuilder builder, Action<IOrderedScope> setter)
    {
        var scope = new OrderedScope<TService>(builder);
        setter(scope);
        builder.Register(ctx => ctx.Resolve<IEnumerable<TService>>().OrderBy(x => scope.OrderedTypes[x.GetType()]));
    }

    private class OrderedScope<TService> : IOrderedScope
    {
        private readonly ContainerBuilder _builder;
        private int _order = 1;

        public OrderedScope(ContainerBuilder builder)
        {
            _builder = builder;
            OrderedTypes = new Dictionary<Type, int>();
        }

        public Dictionary<Type, int> OrderedTypes {get;}

        public IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> Register<T>()
        {
            OrderedTypes.Add(typeof(T), _order++);
            return _builder.RegisterType<T>().As<TService>();
        }
    }
}

public interface IOrderedScope
{
    IRegistrationBuilder<T, ConcreteReflectionActivatorData, SingleRegistrationStyle> Register<T>();
}

用法:

public class OrderedListWorker : IListWorker
{
    public OrderedListWorker(IOrderedEnumerable<IListItem> items)
    {
    }
}
于 2017-07-12T15:08:11.680 回答