Замок-перехватчик DryIoc 3.0

Рекомендуемый способ использования перехвата в DryIoc больше не работает с версии 3.0:

https://bitbucket.org/dadhi/dryioc/wiki/Interception

Метод GetPublicInstanceConstructors отсутствует. Каким новым способом добиться этого?


person user2429841    schedule 09.08.2018    source источник
comment
Необходимо обновить документы :(   -  person dadhi    schedule 09.08.2018
comment
Тем временем Гир работает с примером из репо:   -  person dadhi    schedule 09.08.2018


Ответы (2)


Made.Of((Type type) => type.GetConstructors().SingleOrDefault(c => c.GetParameters().Length != 0)

Кажется, работает.

person user2429841    schedule 09.08.2018

Вот рабочий код интеграции ( который, вероятно, в будущем будет перенесен в отдельный пакет):

using System;
using Castle.DynamicProxy;
using ImTools;

namespace DryIoc.Interception
{
    // Extension methods for interceptor registration using Castle Dynamic Proxy.
    public static class DryIocInterception
    {
        public static void Intercept<TService, TInterceptor>(this IRegistrator registrator, object serviceKey = null)
            where TInterceptor : class, IInterceptor => 
            registrator.Intercept<TInterceptor>(typeof(TService), serviceKey);

        public static void Intercept<TInterceptor>(this IRegistrator registrator, Type serviceType, object serviceKey = null)
            where TInterceptor : class, IInterceptor =>
            registrator.Intercept(serviceType, Parameters.Of.Type(typeof(IInterceptor[]), typeof(TInterceptor[])), serviceKey);

        public static void Intercept(this IRegistrator registrator,
            Type serviceType, ParameterSelector interceptorsParameterSelector, object serviceKey = null)
        {
            Type proxyType;
            if (serviceType.IsInterface())
                proxyType = ProxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(
                    serviceType, ArrayTools.Empty<Type>(), ProxyGenerationOptions.Default);
            else if (serviceType.IsClass())
                proxyType = ProxyBuilder.CreateClassProxyTypeWithTarget(
                    serviceType, ArrayTools.Empty<Type>(), ProxyGenerationOptions.Default);
            else
                throw new ArgumentException(
                    $"Intercepted service type {serviceType} is not a supported, cause it is nor a class nor an interface");

            registrator.Register(serviceType, proxyType,
                made: Made.Of(
                    pt => pt.PublicConstructors().FindFirst(ctor => ctor.GetParameters().Length != 0),
                    interceptorsParameterSelector),
                setup: Setup.DecoratorOf(useDecorateeReuse: true, decorateeServiceKey: serviceKey));
        }

        private static DefaultProxyBuilder ProxyBuilder => _proxyBuilder ?? (_proxyBuilder = new DefaultProxyBuilder());
        private static DefaultProxyBuilder _proxyBuilder;
    }
}
person dadhi    schedule 29.11.2018