2017-09-04 32 views
15

Tôi đang di chuyển ứng dụng của mình từ .Net Framework 4.5.1 sang Dot Net Core. Tôi đã sử dụng RealProxy lớp để đăng nhập thông tin người dùng và các thông số trên BeforeExecute và AfterExecute (như thế này link)AOP trong lõi Dotnet: Proxy động với Proxy thực trong lõi DotNet

Bây giờ có vẻ như không có một điều như vậy trong Dot core.Plus Tôi không muốn sử dụng parties.I thứ ba tìm thấy điều này link đang sử dụng Actionfilter nhưng nó sẽ không thực hiện công việc.

câu hỏi của tôi là Làm cách nào để triển khai Dynamic Proxy trong Dot net Core? Có sự thay thế nào cho Lớp RealProxy không?

Trả lời

0

Như tôi đã trả lời trong RealProxy in dotnet core?, RealProxy không tồn tại trong .NET Core.

Cách khác là DispatchProxy, có ví dụ tuyệt vời ở đây: http://www.c-sharpcorner.com/article/aspect-oriented-programming-in-c-sharp-using-dispatchproxy/.

Nếu chúng ta đơn giản hóa mã, đây là những gì chúng tôi nhận được:

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

Vì vậy, nếu chúng ta có một lớp dụ Calculator với một giao diện tương ứng (không hiển thị ở đây):

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

chúng ta có thể chỉ cần sử dụng nó như thế này

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

Và bạn sẽ nhận được đăng nhập mong muốn.

+1

"Như tôi đã trả lời ..." tại sao bạn không đánh dấu câu hỏi là trùng lặp? –

Các vấn đề liên quan