示例#1
0
        public void AddInstance()
        {
            var contracts = new[] { _contract };
            var instance  = Mock.Of <IBooRepository>();

            var actual = _dependencies
                         .AddInstance(contracts, instance)
                         .BuildProvider()
                         .GetRequired(_contract);

            actual.Should().Be(instance);
        }
示例#2
0
        public async Task PublishedWithDifferentLifetimes(
            Notification[] notifications,
            DependencyLifetime commandProcessorLifetime,
            DependencyLifetime notificationProcessorLifetime)
        {
            var dependencyCollection = new DependencyCollection();
            var provider             = dependencyCollection
                                       .AddInstance(_fooRepository.Object)
                                       .AddCommandProcessor <Processor>(commandProcessorLifetime)
                                       .AddNotificationProcessor <OnBooCreated>(notificationProcessorLifetime)
                                       .AddEmitter()
                                       .BuildProvider();

            dependencyCollection.GetLifetime <NotificationPipeline <Notification> >().Should().Be(notificationProcessorLifetime);

            var emitter = provider.GetRequiredService <Emitter>();

            foreach (var notification in notifications)
            {
                notification.StopPropagation = false;

                await emitter.Publish(notification);

                _fooRepository.Verify(repository => repository
                                      .AddElement(It.Is <Foo>(foo => foo.Int == notification.Id)));
            }
        }
示例#3
0
 public static DependencyCollection AddPool <T>(this DependencyCollection collection,
                                                Func <T> builder, int capacity = 10)
     where T : class
 {
     collection.AddInstance <IPool <T> >(new Pool <T>(capacity, builder));
     return(collection);
 }
示例#4
0
        public static DependencyCollection AddCommandLineConfiguration(this DependencyCollection collection,
                                                                       string[] commandLineArgs)
        {
            EnsureConfigurationRegistered(collection);

            collection.AddInstance <IConfigurationSource>(new CommandLineSource(commandLineArgs));
            return(collection);
        }
示例#5
0
        public static DependencyCollection AddJsonConfiguration(this DependencyCollection collection,
                                                                string path = "appsettings.json", bool required = false)
        {
            EnsureConfigurationRegistered(collection);

            collection.AddInstance <IConfigurationSource>(new JsonFileSource(path, required));
            return(collection);
        }
示例#6
0
        public static DependencyCollection AddDefaultLogEnrichers(this DependencyCollection collection)
        {
            collection
            .AddInstance <ILogEnricher>(new LevelEnricher())
            .AddInstance <ILogEnricher>(new SenderEnricher())
            .AddInstance <ILogEnricher>(new TimeStampEnricher());

            return(collection);
        }
示例#7
0
        public static DependencyCollection AddFileServer(this DependencyCollection collection, string path = null)
        {
            if (!collection.Contains <HttpServer>())
            {
                AddServer(collection);
            }

            collection.AddInstance <IHttpRequestHandler>(new FileRequestHandler(path));
            return(collection);
        }
        public static DependencyCollection AddJsonConverter(this DependencyCollection collection,
                                                            CultureInfo culture = null)
        {
            var convertersCollection = new ConvertersCollection(culture ?? CultureInfo.InvariantCulture);

            collection
            .AddInstance <IConvertersCollection>(convertersCollection)
            .AddInstance(new JConverter(convertersCollection));

            return(collection);
        }
示例#9
0
        public async Task ExecutedWithDifferentLifetimes(
            Boo[] boos,
            DependencyLifetime measureLifetime,
            DependencyLifetime exceptionLifetime,
            DependencyLifetime preProcessorLifetime,
            DependencyLifetime processorLifetime,
            DependencyLifetime postProcessorLifetime)
        {
            var dependencyCollection = new DependencyCollection();
            var dependencyProvider   = dependencyCollection
                                       .AddInstance(_repository.Object)
                                       .AddCommandBehaviour <MeasureBehaviour>(measureLifetime)
                                       .AddCommandBehaviour <ExceptionBehaviour <Command> >(exceptionLifetime)
                                       .AddCommandProcessor <PreProcessor>(preProcessorLifetime)
                                       .AddCommandProcessor <Processor>(processorLifetime)
                                       .AddCommandProcessor <PostProcessor>(postProcessorLifetime)
                                       .AddEmitter()
                                       .BuildProvider();

            var expectedLifetime = new[]
            { measureLifetime, exceptionLifetime, preProcessorLifetime, processorLifetime, postProcessorLifetime }
            .DefineLifetime();

            dependencyCollection.GetLifetime <CommandPipeline <Command> >().Should().Be(expectedLifetime);

            var emitter = dependencyProvider.GetRequiredService <Emitter>();

            for (var i = 0; i < 10; i++)
            {
                var commands = boos.Select(b => new Command {
                    Id = b.Id, Int = b.Int
                });

                foreach (var command in commands)
                {
                    await emitter.Execute(command);

                    command.Measured.Should().BeTrue();
                    command.PreProcessed.Should().BeTrue();
                    command.PostProcessed.Should().BeTrue();

                    _repository.Verify(repository => repository
                                       .AddElement(It.Is <Boo>(boo => boo.Id == command.Id && boo.Int == command.Int)));
                }
            }
        }
        private static void AddSingleton(DependencyCollection dependencies, ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationInstance != null)
            {
                dependencies.AddInstance(descriptor.ServiceType, descriptor.ImplementationInstance);
            }
            else if (descriptor.ImplementationFactory == null)
            {
                dependencies.AddSingleton(descriptor.ServiceType, descriptor.ImplementationType);
            }
            else
            {
                var contracts = new[] { descriptor.ServiceType };
                var resolver  = BuildDelegateResolver(descriptor);

                dependencies.AddDependency(new SingletonDependency(contracts, resolver));
            }
        }
示例#11
0
        public async Task ExecutedWithDifferentLifetimes(
            Boo[] boos,
            DependencyLifetime behaviourLifetime,
            DependencyLifetime preProcessorLifetime,
            DependencyLifetime processorLifetime,
            DependencyLifetime postProcessorLifetime)
        {
            var dependencyCollection = new DependencyCollection();
            var dependencyProvider   = dependencyCollection
                                       .AddInstance(_repository.Object)
                                       .AddQueryBehaviour <Behaviour>(behaviourLifetime)
                                       .AddQueryProcessor <PreProcessor>(preProcessorLifetime)
                                       .AddQueryProcessor <Processor>(processorLifetime)
                                       .AddQueryProcessor <PostProcessor>(postProcessorLifetime)
                                       .AddEmitter()
                                       .BuildProvider();

            var expectedLifetime = new[] { behaviourLifetime, preProcessorLifetime, processorLifetime, postProcessorLifetime }.DefineLifetime();

            dependencyCollection.GetLifetime <QueryPipeline <Query, Boo> >().Should().Be(expectedLifetime);

            var emitter = dependencyProvider.GetRequiredService <Emitter>();

            for (var i = 0; i < 10; i++)
            {
                var queries = boos.Select(b => new Query(b.Id));

                foreach (var query in queries)
                {
                    var result = await emitter.Ask(query);

                    query.PreProcessed.Should().BeTrue();
                    query.PostProcessed.Should().BeTrue();

                    _repository.Verify(repository => repository
                                       .GetElement(It.Is <int>(id => id == query.Id)));

                    result.Id.Should().Be(query.Id);
                }
            }
        }
示例#12
0
 public static DependencyCollection AddQueryProcessor <TQuery, TResult>(this DependencyCollection collection,
                                                                        Func <TQuery, TResult> processor)
     where TQuery : IQuery <TResult>
 {
     return(collection.AddInstance(new ActionQueryProcessor <TQuery, TResult>(processor)));
 }
示例#13
0
 public static DependencyCollection AddCommandProcessor <TCommand>(this DependencyCollection collection, Action <TCommand> processor)
     where TCommand : ICommand
 {
     return(collection.AddInstance(new ActionCommandProcessor <TCommand>(processor)));
 }
示例#14
0
 public static DependencyCollection AddLogWriter(this DependencyCollection collection, ILogWriter logWriter)
 {
     collection.AddInstance(logWriter);
     return(collection);
 }
示例#15
0
 public static DependencyCollection AddDefaultConsoleLogWriter(this DependencyCollection collection,
                                                               LogLevel level = LogLevel.Debug)
 {
     collection.AddInstance(new ConsoleLogWriter(level));
     return(collection);
 }
示例#16
0
 public static DependencyCollection AddPool <T>(this DependencyCollection collection, T[] buffer)
     where T : class
 {
     collection.AddInstance <IPool <T> >(new Pool <T>(buffer));
     return(collection);
 }