/// <summary> /// Overridden so that we can inject our own interfaces. /// /// </summary> /// <param name="services"></param> protected override void ConfigureServices(IServiceCollection services) { // The original interface is: IProductRepository // The original implementation is: ProductRepository var originalImplementation = new ProductRepository(); var interceptedProductRepository = new InterceptorProxyBuilder <IProductRepository>() .For(originalImplementation) .InterceptAndStub(theMethodCalled: nameof(IProductRepository.GetAll), withValue: new List <Models.Product>() { new Models.Product() { Id = "TheStubbedItemId1", Description = "TheStubbedItemDescription1" }, new Models.Product() { Id = "TheStubbedItemId2", Description = "TheStubbedItemDescription2" } }) .Build(); var existingDescriptors = services.Where(s => s.ServiceType == typeof(IProductRepository)); existingDescriptors.ToList().ForEach(descriptor => { services.Remove(descriptor); }); services.AddSingleton(interceptedProductRepository); }
public void That_Supports_Callbacks_Before_A_Method_Is_Invoked() { // Arrange var _originalImplementation = new BeforeExecutionMethodSignatures(); IInterceptorProxyBuilder <IBeforeExecutionMethodSignatures> builder = new InterceptorProxyBuilder <IBeforeExecutionMethodSignatures>() .For(_originalImplementation); // Arrange var args = default(object[]); var parameters = default(IDictionary <string, object>); var calledBack = false; var proxy = builder.InterceptBeforeExecutionOf(theMethodNamed: nameof(IBeforeExecutionMethodSignatures.HasOneParameter), andCallBackWith: result => { calledBack = true; args = result.Args; parameters = result.Parameters; }) .Build() as IBeforeExecutionMethodSignatures; // Act proxy.HasOneParameter(10); // Assert calledBack.Should().BeTrue(because: "the callback should have been invoked. "); ((int)parameters["parameter1"]).Should().Be(10, because: "the value 10 was passed in. "); }
/// <summary> /// 实例类型,注入所有代理的类型 /// </summary> private static void InitProxyTypes() { InterceptorProxyBuilder proxyBuilder = new InterceptorProxyBuilder(); //遍历所有的需要代理的类型 foreach (var aopConfig in aopConfigList) { try { //调用生成代理的方法 var proxy = InterceptorProxyBuilder.CreateProxy(GetType(aopConfig.Value.Type)); //把生成的代理对象放入对象容器 objList.Add(aopConfig.Key, proxy); //把类型放入类型容器 typeList.Add(proxy.GetType().FullName, proxy.GetType()); } catch { throw new Exception("创建" + aopConfig.Value.Type + "的代理失败"); } } }
/// <summary> /// Overridden so that we can inject our own interfaces. /// /// </summary> /// <param name="services"></param> protected override void ConfigureServices(IServiceCollection services) { // The original interface is: IProductRepository // The original implementation is: ProductRepository // We will provide our own interface and the original implementation; we will callback "just before" the IProductRepository GetAll() method returns - thereby giving us the return value. var originalImplementation = new ProductRepository(); var interceptedProductRepository = new InterceptorProxyBuilder <IProductRepository>() .For(originalImplementation) .InterceptAfterExecutionOf(theMethodCalled: nameof(IProductRepository.GetAll), andCallbackWith: result => ToSnapshot <IEnumerable <IProduct> >(result)) .Build(); var existingDescriptors = services.Where(s => s.ServiceType == typeof(IProductRepository)); existingDescriptors.ToList().ForEach(descriptor => { services.Remove(descriptor); }); services.AddSingleton(interceptedProductRepository); }