4

I have an object that takes an ordered List (IOrderedEnumerable) of items where the order of the items is important.

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

I have multiple objects of type IListItem.

How can I register OrderedListWorker with Autofac and ensure that I get the ListItems in a specifc order at run time?

I see that order isn't guaranteed in This Post, but I'm not sure how to guarantee order.

Community
  • 1
  • 1
Max Barfuss
  • 1,432
  • 2
  • 9
  • 14

2 Answers2

4

I have a solution, that is a combination of the IOrderedEnumerable, and the ResolveOrdered<TComponent> solution from the post I linked to above.

Using the AutofacExtensions Class:

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);
    }
}

I can specify my registrations as follows:

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>()));
Max Barfuss
  • 1,432
  • 2
  • 9
  • 14
0

I know this is an old question but I had a similar problem today and I thought I'd share my solution.

Registration:

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

Infrastructure:

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>();
}

Usage:

public class OrderedListWorker : IListWorker
{
    public OrderedListWorker(IOrderedEnumerable<IListItem> items)
    {
    }
}
Magnus
  • 45,362
  • 8
  • 80
  • 118