2017-09-04 42 views
15

Przenoszę moją aplikację z .Net Framework 4.5.1 do Dot Net Core. używałem RealProxy klasy dla rejestrowania informacji i parametrów użytkownika na BeforeExecute i AfterExecute (jak to link)AOP w rdzeniu Dotnet: Dynamic Proxy z Real Proxy w rdzeniu Dotnet

Teraz wydaje się, że nie ma czegoś takiego w Dot core.Plus Nie chcę używać trzeci parties.I znalazłem ten link, który używa Actionfilter, ale nie wykona zadania.

moje pytanie jest Jak mogę wdrożyć Dynamic Proxy w Dot net Core? Czy są jakieś alternatywy dla RealProxy Class?

Odpowiedz

0

Jak już odpowiedziałem w RealProxy in dotnet core?, RealProxy nie istnieje w .NET Core.

Alternatywą jest DispatchProxy, która ma wspaniały przykład tutaj: http://www.c-sharpcorner.com/article/aspect-oriented-programming-in-c-sharp-using-dispatchproxy/.

Gdybyśmy uprościć kod, to co mamy:

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"); 
    } 
} 

Więc jeśli mamy przykładową klasę Calculator z odpowiednim interfejsem (nie pokazanego tutaj):

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

możemy po prostu użyj go w ten sposób:

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

Otrzymasz żądane logowanie.

+1

"Jak już odpowiedziałem w ..." dlaczego nie oznaczyłeś wtedy tego pytania jako duplikatu? –

Powiązane problemy