void VerifyInvalid() { var registry = new AspectRegistry(); registry.Get().Open().Should().BeEmpty(); registry.Execute(new Registration(Never <Array <Type> > .Default, typeof(Aspect <,>))); registry.Get().Open().Should().HaveCount(1); var registrations = new AspectRegistrations <string, int>(registry); registrations.Get(GenericArguments.Default.Get(A.Type <ISelect <string, int> >())) .Open() .Should() .BeEmpty(); }
void Verify() { var registry = new AspectRegistry(); registry.Execute(new Registration(typeof(AssignedAspect <,>))); var subject = Start.A.Selection <string>().By.Self.Get(); subject.Invoking(x => x.Get(null)).Should().NotThrow(); new Aspects <string, string>(registry).Get(subject) .Get(subject) .Invoking(x => x.Get(null)) .Should() .Throw <ArgumentNullException>(); }
/*public static ISelect<TIn, TOut> Configured<TIn, TOut>(this ISelect<TIn, TOut> @this) * => Aspects<TIn, TOut>.Default.Get(@this).Get(@this);*/ public static IAspect <TIn, TOut> Registered <TIn, TOut>(this AspectRegistry @this, IAspect <TIn, TOut> aspect) { @this.Execute(new Registration <TIn, TOut>(aspect)); return(aspect); }
public static void RewireWithAspects(this IServiceCollection services, Action <IAspectOptions>?options = null) { var aspectRegistry = new AspectRegistry(); services.AddSingleton(aspectRegistry); var optionsBuilder = new AspectOptionsBuilder(); options?.Invoke(optionsBuilder); foreach (var map in optionsBuilder.Maps) { aspectRegistry.RegisterAspectTrigger(map.Attribute, map.Aspect); } foreach (var aspectType in aspectRegistry.RegisteredAspectTypes) { services.AddTransient(aspectType); } var descriptorsOfAnnotatedServices = services .Where(IsSupportedServiceDescriptor) .Where(x => aspectRegistry.HasTriggers(x.ImplementationType !)) .ToArray(); foreach (var serviceDescriptor in descriptorsOfAnnotatedServices) { aspectRegistry.RegisterAnnotatedService(serviceDescriptor.ImplementationType !); } // remove current service registrations foreach (var serviceDescriptor in descriptorsOfAnnotatedServices) { services.Remove(serviceDescriptor); } // add service descriptor for implementation types foreach (var serviceDescriptor in descriptorsOfAnnotatedServices) { services.Add(ServiceDescriptor.Describe( serviceType: serviceDescriptor.ImplementationType !, implementationType: serviceDescriptor.ImplementationType !, lifetime: serviceDescriptor.Lifetime )); } // add new service descriptor with proxy factory foreach (var serviceDescriptor in descriptorsOfAnnotatedServices) { var implementationType = serviceDescriptor.ImplementationType !; var lifetime = serviceDescriptor.Lifetime; var serviceType = serviceDescriptor.ServiceType; services.Add(ServiceDescriptor.Describe( serviceType: serviceType, provider => { var myClass = provider.GetRequiredService(implementationType); var interceptor = new MethodInterceptor(new PipelineStepFactory(provider, aspectRegistry)); return(_proxyGenerator.CreateInterfaceProxyWithTargetInterface( interfaceToProxy: serviceType, target: myClass, new IAsyncInterceptor[] { interceptor } )); }, lifetime )); } }
public PipelineStepFactory(IServiceProvider serviceProvider, AspectRegistry aspectRegistry) { _serviceProvider = serviceProvider; _aspectRegistry = aspectRegistry; }