Пример #1
0
        public bool TryAppendBinding(Type queryType, BindingDescription bindingDescription)
        {
            if (queryType == null)
            {
                throw new ArgumentNullException(nameof(queryType));
            }
            if (bindingDescription == null)
            {
                throw new ArgumentNullException(nameof(bindingDescription));
            }
            if (_typesCollection.ContainsKey(queryType) || _typesCollectionResolved.ContainsKey(queryType))
            {
                return(false);
            }

            _typesCollectionResolved[queryType] = new BindingDescriptionOrdered(bindingDescription, _typesCollectionResolved.Count);
            return(true);
        }
Пример #2
0
        public IEnumerable <T> GetInstances <T>(bool storeInstance, bool useSingleInstance) where T : class
        {
            if (!_typesCollection.TryGetValue(typeof(T), out var bindingDescription))
            {
                if (!_typesCollectionResolved.TryGetValue(typeof(T), out bindingDescription))
                {
                    var resolver = _bindingsResolver;
                    if (resolver == null)
                    {
                        return(null);
                    }

                    var bindingDescriptionPossible = resolver.ResolveType <T>(storeInstance);
                    if (bindingDescriptionPossible != null)
                    {
                        // здесь должны быть проверки.
                        _typesCollectionResolved[typeof(T)] = new BindingDescriptionOrdered(bindingDescriptionPossible, _typesCollectionResolved.Count);
                        bindingDescription = new BindingDescriptionOrdered(bindingDescriptionPossible, 0);
                    }
                    else
                    {
                        // Повторная проверка - во время распознавания типа могли быть загружены дополнительные сборки, предоставившие новые привязки.
                        if (!_typesCollectionResolved.TryGetValue(typeof(T), out bindingDescription))
                        {
                            return(null);
                        }
                    }
                }
            }
            if (bindingDescription == null)
            {
                return(null);
            }

            if (!bindingDescription.Item1.Instances.IsNullOrEmpty())
            {
                return(bindingDescription.Item1.Instances.Select(x => (T)x));
            }
            else
            {
                lock (bindingDescription.Item1.InstancesSetSyncRoot)
                {
                    if (!bindingDescription.Item1.Instances.IsNullOrEmpty())
                    {
                        return(bindingDescription.Item1.Instances.Select(x => (T)x));
                    }
                    else if (bindingDescription.Item1.BindedTypes.Count > 0)
                    {
                        var typesToCreateInstance = useSingleInstance ? bindingDescription.Item1.BindedTypes.Take(1) : bindingDescription.Item1.BindedTypes;
                        var createdInstances      = typesToCreateInstance.Select(x => (T)x.Activator()).ToList();

                        var activatingHadlersSnapshot = _activatingHandlers?.ToList();
                        if (activatingHadlersSnapshot != null)
                        {
                            createdInstances.ForEach(instance => activatingHadlersSnapshot.ForEach(handler => handler.OnInstanceActivating <T>(instance)));
                        }

                        if (storeInstance)
                        {
                            bindingDescription.Item1.Instances = createdInstances;
                        }

                        var activatedHadlersSnapshot = _activatedHandlers?.ToList();
                        if (activatedHadlersSnapshot != null)
                        {
                            createdInstances.ForEach(instance => activatedHadlersSnapshot.ForEach(handler => handler.OnInstanceActivated <T>(instance)));
                        }

                        return(createdInstances);
                    }
                }
            }
            return(null);
        }