Ejemplo n.º 1
0
        private static IServiceProvider CreateTestServiceProvider(Type serviceType, Type implementationType, LifeTime lifeTime = LifeTime.Transient, bool doOpenGenericWireups = true)
        {
            var typeCacheMock             = new Mock <ITypeCache>();
            var implementationsFinderMock = new Mock <IImplementationsFinder>();

            SetupTypeCache(AutoType.Implementation, implementationType);
            SetupTypeCache(AutoType.Abstraction, serviceType);

            implementationsFinderMock
            .Setup(x => x.FindImplemenationsOf(It.Is <Type>(t => t == serviceType)))
            .Returns(new List <Type>
            {
                implementationType
            });

            var services = new ServiceCollection();

            var serviceCollectionPopulator = new ServiceCollectionPopulator(
                typeCacheMock.Object,
                implementationsFinderMock.Object,
                new SuperLiberalTypeNameFilter());

            serviceCollectionPopulator.Populate(services, lifeTime, doOpenGenericWireups);
            return(services.BuildServiceProvider());

            void SetupTypeCache(AutoType autoType, Type type)
            {
                typeCacheMock.Setup(x => x.GetTypes(It.Is <AutoType>(t => t == autoType)))
                .Returns(new List <Type>
                {
                    type
                });
            }
        }
Ejemplo n.º 2
0
        internal static IServiceCollection AddAutoDI(this IServiceCollection serviceCollection, IAssemblyProvider assemblyProvider, Action <Configuration> configureWireUp)
        {
            var typeCache     = new TypeCache(assemblyProvider);
            var configuration = new Configuration
            {
                DefaultLifeTime = LifeTime.Transient
            };

            configureWireUp(configuration);

            var serviceCollectionPopulator = new ServiceCollectionPopulator(
                typeCache,
                new ImplementationsFinder(typeCache),
                CreateNameFilter());

            serviceCollectionPopulator.Populate(serviceCollection, configuration.DefaultLifeTime, configuration.DoOpenGenericWireups);
            return(serviceCollection);

            IServiceTypeNameFilter CreateNameFilter()
            {
                if (configuration.NamingConventionProviderType != null)
                {
                    return((IServiceTypeNameFilter)Activator.CreateInstance(configuration.NamingConventionProviderType));
                }

                return(new SameNameServiceTypeNameFilter());
            }
        }