private static Type DefineHandlerType(IDependency[] dependencies) { if (dependencies.Length == 1) { return(typeof(SystemSinglePipeline <>)); } bool hasParallels = false, hasSequential = false; foreach (var dependency in dependencies) { if (ParallelAttribute.IsDefined(dependency.Implementation)) { hasParallels = true; } else { hasSequential = true; } } return(hasParallels ? hasSequential ? typeof(SystemFullPipeline <>) : typeof(SystemParallelPipeline <>) : typeof(SystemSequentialPipeline <>)); }
public IDependency BuildDependency(Type contract, IDependencyEngine engine) { var contractGenericArgs = contract.GenericTypeArguments; var notificationType = contractGenericArgs[0]; var processorType = typeof(INotificationProcessor <>).MakeGenericType(contractGenericArgs); var dependencies = engine.GetApplicable(processorType); var dependenciesLength = dependencies.Length; var pipelineType = dependenciesLength switch { 0 => throw Error.DependencyNotRegistered(processorType), 1 => typeof(NotificationSimplePipeline <>), _ => ParallelAttribute.IsDefined(notificationType) ? typeof(NotificationParallelPipeline <>) : typeof(NotificationSequentialPipeline <>) }; var implementation = pipelineType.MakeGenericType(contractGenericArgs); var lifetime = dependencies.DefineLifetime(); var resolver = DependencyResolver.Build(lifetime, implementation); return(Dependency.Build(lifetime, new[] { contract }, resolver)); } }
private static object Activator <TNotification>(IServiceProvider provider) where TNotification : INotification { var processors = provider.GetArray <INotificationProcessor <TNotification> >(); return(processors.Length switch { 0 => throw Error.DependencyNotRegistered(typeof(INotificationProcessor <TNotification>)), 1 => new NotificationSimplePipeline <TNotification>(processors[0]), _ => (ParallelAttribute.IsDefined(typeof(TNotification)) ? (INotificationPipeline <TNotification>) new NotificationParallelPipeline <TNotification>(processors) : new NotificationSequentialPipeline <TNotification>(processors)) });
public SystemFullPipeline(TSystem[] systems) { var parallelSystems = new List <TSystem>(); var sequenceSystems = new List <TSystem>(); foreach (var system in systems) { if (ParallelAttribute.IsDefined(system.GetType())) { parallelSystems.Add(system); } else { sequenceSystems.Add(system); } } _parallelPipeline = new SystemParallelPipeline <TSystem>(parallelSystems.ToArray()); _sequentialPipeline = new SystemSequentialPipeline <TSystem>(sequenceSystems.ToArray()); }