AOP в ядре Dotnet: динамический прокси с реальным прокси в ядре Dotnet

Я переношу свое приложение из .Net Framework 4.5.1 в Dot Net Core. Я использовал RealProxy Класс для регистрации информации и параметров пользователя в BeforeExecute и AfterExecute (например, ссылка)

Теперь кажется, что в Dot core нет ничего подобного. Плюс я не хочу использовать третьих сторон. Я нашел ссылку который использует Actionfilter, но он не будет выполнять эту работу.

мой вопрос Как я могу реализовать Dynamic Proxy в Dot net Core? Есть ли альтернатива для класса RealProxy?

Ответы

Ответ 1

Как я уже ответил в RealProxy в ядре dotnet?, RealProxy не существует в .NET Core.

Альтернативой является DispatchProxy, который имеет прекрасный пример здесь: http://www.c-sharpcorner.com/article/aspect-oriented-programming-in-c-sharp-using-dispatchproxy/.

Если мы упростим код, это то, что мы получаем:

public class LoggingDecorator<T> : DispatchProxy
{
    private T _decorated;

    protected override object Invoke(MethodInfo targetMethod, object[] args)
    {
        try
        {
            LogBefore(targetMethod, args);

            var result = targetMethod.Invoke(_decorated, args);

            LogAfter(targetMethod, args, result);
            return result;
        }
        catch (Exception ex) when (ex is TargetInvocationException)
        {
            LogException(ex.InnerException ?? ex, targetMethod);
            throw ex.InnerException ?? ex;
        }
    }

    public static T Create(T decorated)
    {
        object proxy = Create<T, LoggingDecorator<T>>();
        ((LoggingDecorator<T>)proxy).SetParameters(decorated);

        return (T)proxy;
    }

    private void SetParameters(T decorated)
    {
        if (decorated == null)
        {
            throw new ArgumentNullException(nameof(decorated));
        }
        _decorated = decorated;
    }

    private void LogException(Exception exception, MethodInfo methodInfo = null)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} threw exception:\n{exception}");
    }

    private void LogAfter(MethodInfo methodInfo, object[] args, object result)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} executed, Output: {result}");
    }

    private void LogBefore(MethodInfo methodInfo, object[] args)
    {
        Console.WriteLine($"Class {_decorated.GetType().FullName}, Method {methodInfo.Name} is executing");
    }
}

Итак, если у нас есть пример класса Calculator с соответствующим интерфейсом (здесь не показано):

public class Calculator : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

мы можем просто использовать его так:

static void Main(string[] args)
{
    var decoratedCalculator = LoggingDecorator<ICalculator>.Create(new Calculator());
    decoratedCalculator.Add(3, 5);
    Console.ReadKey();
}

И вы получите требуемый журнал.