Created
May 22, 2019 22:12
-
-
Save mshwf/45aa155fb9567611b29cd09c0b00f5c3 to your computer and use it in GitHub Desktop.
implementing DynamicProxy in .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ConsoleTest.Repos; | |
using Goodreads; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.Remoting.Messaging; | |
using System.Runtime.Remoting.Proxies; | |
using System.Security.Principal; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleTest | |
{ | |
class DynamicProxy<T> : RealProxy | |
{ | |
private readonly T _decorated; | |
public DynamicProxy(T decorated) : base(typeof(T)) | |
{ | |
_decorated = decorated; | |
} | |
void Log(string msg, object args = null) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(msg, args); | |
Console.ResetColor(); | |
} | |
public override IMessage Invoke(IMessage msg) | |
{ | |
var methodCall = msg as IMethodCallMessage; | |
var methodInfo = methodCall.MethodBase as MethodInfo; | |
Log($"In dynamic proxy - Before executing {methodCall.MethodName}"); | |
try | |
{ | |
var result = methodInfo.Invoke(_decorated, methodCall.Args); | |
Log($"In dynamic proxy - After executing {methodCall.MethodName}"); | |
return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall); | |
} | |
catch (Exception e) | |
{ | |
Log($"In Dynamic Proxy- Exception {e} executing '{methodCall.MethodName}'"); | |
return new ReturnMessage(e, methodCall); | |
} | |
} | |
} | |
class AuthenticationProxy<T> : RealProxy | |
{ | |
private readonly T _decorated; | |
public AuthenticationProxy(T decorated) | |
: base(typeof(T)) | |
{ | |
_decorated = decorated; | |
} | |
private void Log(string msg, object arg = null) | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine(msg, arg); | |
Console.ResetColor(); | |
} | |
public override IMessage Invoke(IMessage msg) | |
{ | |
var methodCall = msg as IMethodCallMessage; | |
var methodInfo = methodCall.MethodBase as MethodInfo; | |
if (Thread.CurrentPrincipal.IsInRole("ADMIN")) | |
{ | |
try | |
{ | |
Log("User authenticated - You can execute '{0}' ", | |
methodCall.MethodName); | |
var result = methodInfo.Invoke(_decorated, methodCall.InArgs); | |
return new ReturnMessage(result, null, 0, | |
methodCall.LogicalCallContext, methodCall); | |
} | |
catch (Exception e) | |
{ | |
Log(string.Format( | |
"User authenticated - Exception {0} executing '{1}'", e), | |
methodCall.MethodName); | |
return new ReturnMessage(e, methodCall); | |
} | |
} | |
Log("User not authenticated - You can't execute '{0}' ", | |
methodCall.MethodName); | |
return new ReturnMessage(null, null, 0, | |
methodCall.LogicalCallContext, methodCall); | |
} | |
} | |
class Program | |
{ | |
Task<string> GetName() => Task.Run(() => "foo"); | |
async Task<string> GetName2() => "foo"; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("***\r\n Begin program - logging and authentication\r\n"); | |
Console.WriteLine("\r\nRunning as admin"); | |
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Administrator"), new[] { "ADMIN" }); | |
IRepository<Customer> customerRepository = RepositoryFactory.Create<Customer>(); | |
var customer = new Customer | |
{ | |
Id = 1, | |
Name = "Mahinor", | |
Address = "Earth" | |
}; | |
customerRepository.Add(customer); | |
customerRepository.Update(customer); | |
customerRepository.Delete(customer); | |
Console.WriteLine("\r\nRunning as user"); | |
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("NormalUser"), new string[] { }); | |
customerRepository.Add(customer); | |
customerRepository.Update(customer); | |
customerRepository.Delete(customer); | |
Console.WriteLine("\r\nEnd program - logging and authentication\r\n***"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment