public static I Create <I>( I instance, ILogger logger, LogType logType = c_DefaultLogType, params IInterceptor[] extraInterceptors) where I : class { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } Debug.Assert(typeof(I).IsInterface); List <IInterceptor> interceptors = BuildStandardInterceptors(instance, logger, logType); if (extraInterceptors != null && extraInterceptors.Any()) { interceptors.AddRange(extraInterceptors); } return(s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface(instance, interceptors.ToArray())); }
public static T Create <T>( T instance, ILogger logger, LogTypes logTypes = DefaultLogTypes, params IInterceptor[] extraInterceptors) where T : class { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } instance.ThrowIfNotInterface(); List <IInterceptor> interceptors = BuildStandardInterceptors(logger, logTypes); if (extraInterceptors != null && extraInterceptors.Any()) { interceptors.AddRange(extraInterceptors); } return(s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface(instance, interceptors.ToArray())); }
public static IInterfaceToProxy CreateProxy(Func <IInterfaceToProxy> factory, IAsyncInterceptor interceptor) { IInterfaceToProxy implementation = factory(); IInterfaceToProxy proxy = Generator.CreateInterfaceProxyWithTargetInterface(implementation, interceptor); return(proxy); }
private static T Intercept <T>(this IProxyGenerator generator, T instance, IAsyncInterceptor interceptor) where T : class { return(typeof(T).IsInterface ? generator.CreateInterfaceProxyWithTargetInterface(instance, interceptor) : generator.CreateClassProxyWithTarget(instance, interceptor)); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface, IInterceptor[])"/> documentation. /// </summary> public static TInterface CreateInterfaceProxyWithTargetInterface <TInterface>( this IProxyGenerator proxyGenerator, TInterface target, params IAsyncInterceptor[] interceptors) where TInterface : class { return(proxyGenerator.CreateInterfaceProxyWithTargetInterface(target, interceptors.ToInterceptors())); }
public async Task AsyncTrackingInterceptor_GivenNoTrackingContext_WhenReturnAsync_ThenNewTrackingContextReturned() { var instance = new TestTrackingService(); var interceptor = new AsyncTrackingInterceptor(); ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor()); TrackingContext.Current.Should().BeNull(); TrackingContext returnedTrackingContext = null; await proxy.ReturnAsync(() => { returnedTrackingContext = TrackingContext.Current; }).ConfigureAwait(false); returnedTrackingContext.Should().NotBeNull(); TrackingContext.Current.Should().BeNull(); }
public async Task AsyncTrackingInterceptor_ReturnAsyncWithoutCurrent_NewTrackingContextReturned() { var instance = new TestTrackingService(); var interceptor = new AsyncTrackingInterceptor(); ITestTrackingService proxy = s_ProxyGenerator.CreateInterfaceProxyWithTargetInterface <ITestTrackingService>(instance, interceptor.ToInterceptor()); Assert.IsNull(TrackingContext.Current); TrackingContext returnedTrackingContext = null; await proxy.ReturnAsync(() => { returnedTrackingContext = TrackingContext.Current; }); Assert.IsNotNull(returnedTrackingContext); Assert.IsNull(TrackingContext.Current); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateInterfaceProxyWithTargetInterface(Type, object, IInterceptor[])"/> documentation. /// </summary> public static object CreateInterfaceProxyWithTargetInterface( this IProxyGenerator proxyGenerator, Type interfaceToProxy, object target, params IAsyncInterceptor[] interceptors) { return(proxyGenerator.CreateInterfaceProxyWithTargetInterface( interfaceToProxy, target, interceptors.ToInterceptors())); }
public static IInterfaceToProxy CreateProxy(List <string> log, IAsyncInterceptor interceptor) { // Arrange var classWithInterfaceToProxy = new ClassWithInterfaceToProxy(log); var proxy = Generator.CreateInterfaceProxyWithTargetInterface <IInterfaceToProxy>( classWithInterfaceToProxy, interceptor); return(proxy); }
/// <summary> /// Gets an auto-lazy service which will wrap the specified <see cref="Lazy{T}"/> instance. /// </summary> /// <remarks> /// <para> /// This works by creating a DynamicProxy using <see cref="IProxyGenerator.CreateInterfaceProxyWithTargetInterface{TInterface}(TInterface, IInterceptor[])"/>. /// That proxy implements the interface <typeparamref name="T"/> and is initially satisfied by a stub /// implementation of that interface type. The stub implementation is created via <see cref="IGetsStubs"/>. /// </para> /// <para> /// One interceptor is created and used with the proxy, and that interceptor is created via /// <see cref="IGetsAutoLazyInterceptors"/>. That interceptor will redirect any invocation of any /// functionality to <see cref="Lazy{T}.Value"/> of the specified <paramref name="lazy"/> /// object. /// </para> /// </remarks> /// <returns>The auto-lazy service.</returns> /// <param name="lazy">A lazy instance, to wrap in an auto-lazy service.</param> /// <typeparam name="T">The type of service.</typeparam> /// <exception cref="ArgumentNullException">If <paramref name="lazy"/> is <c>null</c>.</exception> /// <exception cref="AutoLazyException">If the service type <typeparamref name="T"/> is not valid to be an auto-lazy service.</exception> public T GetAutoLazyService <T>(Lazy <T> lazy) where T : class { if (lazy == null) { throw new ArgumentNullException(nameof(lazy)); } InterfaceDetector.AssertIsInterface <T>(); var lazyInterceptor = autoLazyInterceptorFactory.GetAutoLazyInterceptor(lazy); var stubImplementation = stubFactory.GetStub <T>(); return(proxyGenerator.CreateInterfaceProxyWithTargetInterface(stubImplementation, lazyInterceptor)); }
/// <summary> /// See the <see cref="IProxyGenerator .CreateInterfaceProxyWithTargetInterface(Type, Type[], object, ProxyGenerationOptions, IInterceptor[])"/> documentation. /// </summary> public static object CreateInterfaceProxyWithTargetInterface( this IProxyGenerator proxyGenerator, Type interfaceToProxy, Type[] additionalInterfacesToProxy, object target, ProxyGenerationOptions options, params IAsyncInterceptor[] interceptors) { return(proxyGenerator.CreateInterfaceProxyWithTargetInterface( interfaceToProxy, additionalInterfacesToProxy, target, options, interceptors.ToInterceptors())); }
private static object Intercept(this IProxyGenerator generator, Type type, object instance, IAsyncInterceptor interceptor) { return(type.IsInterface ? generator.CreateInterfaceProxyWithTargetInterface(type, instance, interceptor) : generator.CreateClassProxyWithTarget(type, instance, interceptor)); }