示例#1
0
        public override void AddType(IServiceCollection services, Type type)
        {
            if (IsConventionalRegistrationDisabled(type))
            {
                return;
            }

            var dependencyAttribute = GetDependencyAttributeOrNull(type);
            var lifeTime            = GetLifeTimeOrNull(type, dependencyAttribute);

            if (lifeTime == null)
            {
                return;
            }

            foreach (var serviceType in AutoRegistrationHelper.GetExposedServices(services, type))
            {
                var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime.Value);

                if (dependencyAttribute?.ReplaceServices == true)
                {
                    services.Replace(serviceDescriptor);
                }
                else if (dependencyAttribute?.TryRegister == true)
                {
                    services.TryAdd(serviceDescriptor);
                }
                else
                {
                    services.Add(serviceDescriptor);
                }
            }
        }
        public void Should_Get_Custom_Exposed_Types_If_Available()
        {
            //Act

            var exposedServices = AutoRegistrationHelper.GetExposedServices(new ServiceCollection(), typeof(ExplicitDerivedService)).ToList();

            //Assert

            exposedServices.Count.ShouldBe(1);
            exposedServices.ShouldContain(typeof(IDerivedService));
        }
        public void Should_Get_Conventional_Exposed_Types_By_Default()
        {
            //Act

            var exposedServices = AutoRegistrationHelper.GetExposedServices(new ServiceCollection(), typeof(DefaultDerivedService)).ToList();

            //Assert

            exposedServices.Count.ShouldBe(3);
            exposedServices.ShouldContain(typeof(DefaultDerivedService));
            exposedServices.ShouldContain(typeof(IService));
            exposedServices.ShouldContain(typeof(IDerivedService));
        }