Как внедрить зависимость следующего обработчика в цепочку ответственности? - PullRequest
0 голосов
/ 02 апреля 2019

В моем текущем проекте я использую довольно много шаблонов Chain of Responsibility.

Однако, мне немного неудобно конфигурировать цепочку путем внедрения зависимостей.

Учитывая этомодель:

public interface IChainOfResponsibility 
{
    IChainOfResponsibility Next { get; }
    void Handle(Foo foo);
}

public class HandlerOne : IChainOfResponsibility 
{
    private DbContext _dbContext;

    public HandlerOne(IChainOfResponsibility next, DbContext dbContext)
    {
        Next = next;
        _dbContext = dbContext;
    }

    public IChainOfResponsibility Next { get; }

    public void Handle(Foo foo) { /*...*/}
}

public class HandlerTwo : IChainOfResponsibility 
{
    private DbContext _dbContext;

    public HandlerTwo(IChainOfResponsibility next, DbContext dbContext)
    {
        Next = next;
        _dbContext = dbContext;
    }

    public IChainOfResponsibility Next { get; }

    public void Handle(Foo foo) { /*...*/}
}

Мой запуск становится:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IChainOfResponsibility>(x => 
        new HandlerOne(x.GetRequiredService<HandlerTwo>(), x.GetRequiredService<DbContext>())
    );

    services.AddTransient(x => 
        new HandlerTwo(null, x.GetRequiredService<DbContext>())
    );
}

Как более четко настроить мою цепочку ответственности?

1 Ответ

0 голосов
/ 02 апреля 2019

Я взломал простое решение, так как не мог найти ничего, что делало бы то, что я хотел.Кажется, работает нормально, пока GetRequiredService может разрешить все зависимости конструктора всех обработчиков цепочки.

Мой класс запуска становится:

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

И код, который творит чудеса:

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. Я отвечаю на свой вопрос для дальнейшего использования.(я и, надеюсь, другие), но я хотел бы получить некоторые отзывы об этом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...