Пример #1
0
        public void Throw_Not_Generic_Implementation()
        {
            var builder = new DependencyCollection();

            Assert.Throws <InvalidOperationException>(() =>
                                                      builder.AddScoped(typeof(IRepository <>), typeof(FooRepository)));
        }
Пример #2
0
        public void Compile()
        {
            var provider = _dependencies
                           .AddScoped <ISession, Session>()
                           .BuildProvider();

            var first  = provider.GetService <ISession>();
            var second = provider.GetService <ISession>();

            Assert.Same(first, second);

            using (var scope = provider.CreateScope())
            {
                Assert.NotSame(first, scope.GetService <ISession>());
            }
        }
Пример #3
0
        private static void AddScoped(DependencyCollection dependencies, ServiceDescriptor descriptor)
        {
            if (descriptor.ImplementationFactory == null)
            {
                dependencies.AddScoped(descriptor.ServiceType, descriptor.ImplementationType);
            }
            else
            {
                var contracts = new[] { descriptor.ServiceType };
                var resolver  = BuildDelegateResolver(descriptor);

                dependencies.Add(new ScopedDependency(contracts, resolver));
            }
        }