public void CanRetry() { var instance = new ErrorOperation(); var interceptor = new InterfaceInterceptor(); var proxy = interceptor.Intercept(instance, typeof(IErrorOperation), new RetriesInterceptionHandler(3, TimeSpan.FromSeconds(5))) as IErrorOperation; Assert.Throws <InvalidOperationException>(() => proxy.Throw() ); }
public void CanControlAccess() { var instance = new ProtectedClass(); var interceptor = new InterfaceInterceptor(); var proxy = interceptor.Intercept(instance, typeof(IProtectedClass), new AccessControlInterceptionHandler("")) as IProtectedClass; Assert.Throws <InvalidOperationException>(() => proxy.TryAccess() ); }
static void InterfaceInterceptor(object instance) { //Interface interceptor var interceptor = new InterfaceInterceptor(); var canIntercept = interceptor.CanIntercept(instance); var myProxy = interceptor.Intercept(instance, typeof(IMyType), new MyHandler()) as IMyType; var proxy = myProxy as IInterceptionProxy; var otherInterceptor = proxy.Interceptor; var result = myProxy.MyMethod(); Assert.AreEqual(20, result); }
public void CanCache() { var instance = new Calculator(); var interceptor = new InterfaceInterceptor(); var proxy = interceptor.Intercept(instance, typeof(ICalculator), new CachingInterceptionHandler(new MemoryCache(Options.Create <MemoryCacheOptions>(new MemoryCacheOptions())), TimeSpan.FromSeconds(10))) as ICalculator; proxy.Add(1, 2); var timer = Stopwatch.StartNew(); proxy.Add(1, 2); Assert.True(timer.Elapsed < TimeSpan.FromSeconds(5)); }
public void CanCreateTransactions() { var instance = new Calculator(); var interceptor = new InterfaceInterceptor(); var handler = new MultiInterceptionHandler(); var result = false; handler.Handlers.Add(new TransactionInterceptionHandler(TimeSpan.FromSeconds(10), System.Transactions.TransactionScopeAsyncFlowOption.Enabled, System.Transactions.TransactionScopeOption.Required, System.Transactions.IsolationLevel.ReadCommitted)); handler.Handlers.Add(new DelegateInterceptionHandler(ctx => { result = Transaction.Current != null; })); var proxy = interceptor.Intercept(instance, typeof(ICalculator), handler) as ICalculator; proxy.Add(1, 2); Assert.True(result); }
public void CanRaiseNotifyChangeEvents() { var instance = new Notifiable(); var interceptor = new InterfaceInterceptor(); var proxy = interceptor.Intercept(instance, typeof(INotifiable), new NotifyPropertyChangeHandler()) as INotifiable; var propertyChangingFired = false; var propertyChangedFired = false; proxy.PropertyChanging += (sender, args) => propertyChangingFired = true; proxy.PropertyChanged += (sender, args) => propertyChangedFired = true; proxy.Name = ""; Assert.True(propertyChangingFired); Assert.True(propertyChangedFired); }