feanz
6/25/2015 - 1:27 PM

Register specific decorators in autofac manually.

Register specific decorators in autofac manually.

 private static void RegisterCommandHandlers(ContainerBuilder builder)
        {
            var commandHandlerTypes = Assembly.GetAssembly(typeof (CreateIdentityCommandHandler))
                .GetTypes()
                .Where(type => !type.IsAbstract &&
                               !type.IsInterface &&
                               !type.IsGenericType)
                .Select(t => new {T = t, I = t.GetInterfaces()})
                .SelectMany(x => x.I, (t, i) => new {Details = t, I = i})
                .Where(x => x.I.IsGenericType)
                .Where(x => x.I.GetGenericTypeDefinition() == typeof (ICommandHandler<>))
                .Select(x => x.Details.T)
                .ToList();

            foreach (var type in commandHandlerTypes)
            {
                var @interface = type
                    .GetInterfaces()
                    .First(i => i.IsGenericType &&
                                i.GetGenericTypeDefinition() == typeof (ICommandHandler<>));

                builder.RegisterType(type)
                    .As(@interface)
                    .EnableInterfaceInterceptors()
                    .InterceptedBy(typeof (MethodLogger));
            }
        }