コード例 #1
0
        public IContainerBuilder AddTransient <TService>(Func <Abstractions.IServiceProvider, TService> implementationFactory) where TService : class
        {
            var serviceType = typeof(TService);

            _descriptionMap[serviceType] = new TransientServiceDescriptor(this, serviceType, serviceType, implementationFactory);
            return(this);
        }
コード例 #2
0
        public IContainerBuilder AddTransient <TService, TImplementation>()
            where TImplementation : class, TService
        {
            var serviceType = typeof(TService);

            _descriptionMap[serviceType] = new TransientServiceDescriptor(this, serviceType, typeof(TImplementation), null);
            return(this);
        }
コード例 #3
0
        public IContainerBuilder AddTransient <TService>()
            where TService : class
        {
            var serviceType = typeof(TService);

            _descriptionMap[serviceType] = new TransientServiceDescriptor(this, serviceType, serviceType, null);
            return(this);
        }
コード例 #4
0
        private object AddTypeToMap(Type type)
        {
            if (type.IsInterface || type.IsAbstract)
            {
                var curAssem = Assembly.GetAssembly(type);

                if (!_assemblyTypesCache.ContainsKey(curAssem))
                {
                    _assemblyTypesCache[curAssem] = curAssem.GetTypes();
                }

                var inheritTypes     = _assemblyTypesCache[curAssem].Where(p => !p.Equals(type) && type.IsAssignableFrom(p)).ToList();
                var implementedCount = inheritTypes.Count();

                if (implementedCount == 1)
                {
                    var description = new TransientServiceDescriptor(
                        this,
                        type,
                        inheritTypes.First(),
                        null);
                    _descriptionMap[type] = description;
                }
                else if (implementedCount > 1)
                {
                    throw new MultiplyImplementingException($"Type {type} has more then one child classes..");
                }
                else
                {
                    throw new NotImplementedException($"The type: {type} has not implemented..");
                }
            }
            else
            {
                var description = new TransientServiceDescriptor(
                    this,
                    type,
                    type,
                    null);
                _descriptionMap[type] = description;
            }

            return(_descriptionMap[type].ImplementationInstance);
        }