Example #1
0
        public void ConfigureDependencyWithProvider()
        {
            string   name      = "John";
            int      age       = 26;
            DateTime birthDate = new DateTime(1908, 8, 5);

            IDependencyCollection collection = new DependencyCollection();

            collection.AddSingleton <Person>();
            collection.Configure <Person>(p =>
            {
                p.Name      = name;
                p.Age       = age;
                p.BirthDate = birthDate;
            });
            collection.AddScoped <DependencyWithConfigure>();
            collection.Configure <DependencyWithConfigure>((p, c) =>
            {
                Person person = p.GetDependency <Person>();

                c.MyString = person.Name;
                c.MyInt    = person.Age;
                c.MyDate   = person.BirthDate;
            });

            using IDependencyProvider provider = collection.BuildProvider();

            DependencyWithConfigure configureDependency = provider.GetDependency <DependencyWithConfigure>();

            configureDependency.ShouldNotBeNull();

            configureDependency.MyDate.ShouldBe(birthDate);
            configureDependency.MyInt.ShouldBe(age);
            configureDependency.MyString.ShouldBe(name);
        }
Example #2
0
 public void InstallProvider()
 {
     _dependencies
     .BuildProvider()
     .GetRequired <ISettingsProvider>()
     .Should().NotBeNull();
 }
Example #3
0
        public void ConfigureDependency()
        {
            DateTime testDateTime = new DateTime(2000, 06, 01);
            int      testInteger  = 5;
            string   testString   = "Look, A wild string has appeared!";

            IDependencyCollection collection = new DependencyCollection();

            collection.AddSingleton <DependencyWithConfigure>();
            collection.Configure <DependencyWithConfigure>(c =>
            {
                c.MyDate = testDateTime;
            });
            collection.Configure <DependencyWithConfigure>(c =>
            {
                c.MyInt    = testInteger;
                c.MyString = testString;
            });

            using IDependencyProvider provider = collection.BuildProvider();

            DependencyWithConfigure configureDependency = provider.GetDependency <DependencyWithConfigure>();

            configureDependency.ShouldNotBeNull();

            configureDependency.MyDate.ShouldBe(testDateTime);
            configureDependency.MyInt.ShouldBe(testInteger);
            configureDependency.MyString.ShouldBe(testString);
        }
        public static DependencyProvider BuildDependencyProvider(this IServiceCollection services)
        {
            var dependencies = new DependencyCollection();

            AddServiceCollection(dependencies, services);

            return(dependencies.BuildProvider());
        }
Example #5
0
        public static DependencyProvider BuildDependencyProvider(this IServiceCollection services)
        {
            var dependencies = new DependencyCollection();

            dependencies
            .Add(new ServiceScopeDependency())
            .AddServiceCollection(services);

            return(dependencies.BuildProvider());
        }
Example #6
0
        public static IServiceCollection AddDependencyProvider(this IServiceCollection services,
                                                               Action <DependencyCollection> dependencies)
        {
            var dependencyCollection = new DependencyCollection();

            dependencies(dependencyCollection);

            services.AddSingleton(dependencyCollection.BuildProvider());
            return(services);
        }
Example #7
0
        public void Activate()
        {
            var provider   = _dependencies.BuildProvider();
            var repository = provider.Activate(typeof(FooRepository));

            Assert.NotNull(repository);
            Assert.IsType <FooRepository>(repository);
        }
Example #8
0
        public void ShouldGetDependencyWithInnerProvider()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.Register <MyImplementation>()
            //.DefineAs<IMyDefinition>()
            .AsSingleton();

            collection
            .Register(p => p.GetRequiredDependency <MyImplementation>())
            .DefineAs <IMyDefinition>()
            .AsTransient()
            .DoNotBind();

            using var provider = collection.BuildProvider();

            IMyDefinition definition = provider.GetDependency <IMyDefinition>();
        }
Example #9
0
        public void GetDependencyWithScope()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.AddSingleton <DependencyWithScope, DependencyWithScope>();

            using (ILifetimeScope scope = collection.CreateScope())
            {
                using (IDependencyProvider provider = scope.BuildProvider())
                {
                    DependencyWithScope dependency = Should.NotThrow(provider.GetDependency <DependencyWithScope>);

                    dependency.ShouldNotBeNull();
                    dependency.Scope.ShouldNotBeNull();
                }
            }

            using (IDependencyProvider provider = collection.BuildProvider())
            {
                Should.Throw <InvalidScopeException>(provider.GetDependency <DependencyWithScope>);
            }
        }
Example #10
0
        public void GetDependencyWithProvider()
        {
            IDependencyCollection collection = new DependencyCollection();

            collection.AddScoped <DependencyWithProvider, DependencyWithProvider>();

            using (IDependencyProvider provider = collection.BuildProvider())
            {
                DependencyWithProvider dependency = Should.NotThrow(provider.GetDependency <DependencyWithProvider>);

                dependency.ShouldNotBeNull();
                dependency.Provider.ShouldNotBeNull();
            }

            collection = new DependencyCollection();

            collection.AddSingleton <DependencyWithProvider, DependencyWithProvider>();

            using (IDependencyProvider provider = collection.BuildProvider())
            {
                Should.Throw <SingletonDependencyException>(provider.GetDependency <DependencyWithProvider>);
            }
        }
Example #11
0
        public void WriteWriters(LogLevel level, string message)
        {
            var dependencies = new DependencyCollection().AddLogs();
            var writers      = Many(() => new Mock <ILogWriter>());

            foreach (var writer in writers)
            {
                dependencies.AddLogWriter(writer.Object);
            }

            dependencies.BuildProvider()
            .GetRequired <ILogger <LogProviderShould> >()
            .Log(level, message);

            foreach (var writer in writers)
            {
                writer.Verify(w => w
                              .Write(It.Is <LogContext>(context =>
                                                        context.Level == level &&
                                                        context.Sender == _sender &&
                                                        context.Template == message),
                                     It.IsNotNull <JsonObject>()));
            }
        }
Example #12
0
        public void AddProviderToDependencyCollection()
        {
            var provider = _dependencies.BuildProvider();

            provider.GetRequired <DependencyProvider>().Should().Be(provider);
        }