How to inject the dependency of the next handler in a chain of responsibility?

我是研究僧i 提交于 2019-12-06 15:35:43

I've hacked a simple solution, as I couldn't find anything that did what I wanted. Seems to be working fine, as long as the GetRequiredService can resolve all constructor dependencies of all the handlers of the chain.

My startup class becomes:

public void ConfigureServices(IServiceCollection services)
{
    services.Chain<IChainOfResponsibility>()
        .Add<HandlerOne>()
        .Add<HandlerTwo>()
        .Configure();
}

And the code that does the magic:

public static class ChainConfigurator
{
    public static IChainConfigurator<T> Chain<T>(this IServiceCollection services) where T : class
    {
        return new ChainConfiguratorImpl<T>(services);
    }

    public interface IChainConfigurator<T>
    {
        IChainConfigurator<T> Add<TImplementation>() where TImplementation : T;
        void Configure();
    }

    private class ChainConfiguratorImpl<T> : IChainConfigurator<T> where T : class
    {
        private readonly IServiceCollection _services;
        private List<Type> _types;
        private Type _interfaceType;

        public ChainConfiguratorImpl(IServiceCollection services)
        {
            _services = services;
            _types = new List<Type>();
            _interfaceType = typeof(T);
        }

        public IChainConfigurator<T> Add<TImplementation>() where TImplementation : T
        {
            var type = typeof(TImplementation);

            if (!_interfaceType.IsAssignableFrom(type))
                throw new ArgumentException($"{type.Name} type is not an implementation of {_interfaceType.Name}", nameof(type));

            _types.Add(type);

            return this;
        }

        public void Configure()
        {
            if (_types.Count == 0)
                throw new InvalidOperationException($"No implementation defined for {_interfaceType.Name}");

            bool first = true;
            foreach (var type in _types)
            {
                ConfigureType(type, first);
                first = false;
            }
        }

        private void ConfigureType(Type currentType, bool first)
        {
            var nextType = _types.SkipWhile(x => x != currentType).SkipWhile(x => x == currentType).FirstOrDefault();

            var ctor = currentType.GetConstructors().OrderByDescending(x => x.GetParameters().Count()).First();

            var parameter = Expression.Parameter(typeof(IServiceProvider), "x");

            var ctorParameters = ctor.GetParameters().Select(x =>
            {
                if (_interfaceType.IsAssignableFrom(x.ParameterType))
                {
                    if (nextType == null)
                        return Expression.Constant(null, _interfaceType);
                    else
                        return Expression.Call(typeof(ServiceProviderServiceExtensions), "GetRequiredService", new Type[] { nextType }, parameter);
                }

                return (Expression)Expression.Call(typeof(ServiceProviderServiceExtensions), "GetRequiredService", new Type[] { x.ParameterType }, parameter);
            });

            var body = Expression.New(ctor, ctorParameters.ToArray());

            var resolveType= first ? _interfaceType : currentType;

            var expressionType = Expression.GetFuncType(typeof(IServiceProvider), resolveType);
            var expression = Expression.Lambda(expressionType, body, parameter);
            Func<IServiceProvider, object> x = (Func<IServiceProvider, object>)expression.Compile();
            _services.AddTransient(resolveType, x);
        }
    }
}

PS.: I'm answering my own question for future reference (myself and hopefully others), but I'd love some feedback on this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!