Beispiel #1
0
        private IReadOnlyCollection <Type> RegisterTypes(IDiscoveryService discovery)
        {
            ITypeFactory[] factories = discovery.GetCustomFactories().ToArray();
            var            normal    = new List <Type>();
            var            custom    = new List <Type>(); // We need to store these to return them at the end

            foreach (Type type in discovery.GetDiscoveredTypes())
            {
                Func <IResolver, object> factory = this.GetFactory(factories, type);
                if (factory == null)
                {
                    normal.Add(type);
                }
                else
                {
                    this.adapter.Container.RegisterDelegate(
                        type,
                        factory,
                        ifAlreadyRegistered: IfAlreadyRegistered.Replace);

                    custom.Add(type);
                }
            }

            this.adapter.Container.RegisterMany(
                normal.Where(IsImplementationType),
                (IRegistrator registrator, Type[] serviceTypes, Type implementingType) =>
            {
                IReuse reuse =
                    serviceTypes.Any(discovery.IsSingleInstance) ?
                    Reuse.Singleton : Reuse.Transient;

                registrator.RegisterMany(
                    serviceTypes,
                    implementingType,
                    reuse,
                    ifAlreadyRegistered: IfAlreadyRegistered.Keep);
            },
                nonPublicServiceTypes: true);

            // Normal probably has the most types in it, so fold the others into it
            normal.AddRange(custom);
            return(normal);
        }