Esempio n. 1
0
 public void Remove(ContractIdentity identity)
 {
     if (_contracts.ContainsKey(identity))
     {
         _contracts.Remove(identity);
     }
 }
        public virtual object GetComponent(Type contract, string name)
        {
            if (contract.ContainsGenericParameters)
            {
                throw new CompositionException("Requested contract type " + contract.Name +
                                               " contains open generic parameters. Can not construct a concrete type.");
            }

            var identity  = new ContractIdentity(contract, name);
            var factories = _repository.FindFactories(identity);

            using (var enumerator = factories?.GetEnumerator())
            {
                if (enumerator == null)
                {
                    return(null);
                }

                while (enumerator.MoveNext())
                {
                    var result = enumerator.Current?.GetComponentInstance(identity, _compositionListeners.Values);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
        public virtual void Unregister(ContractIdentity identity)
        {
            if (identity == null)
            {
                throw new ArgumentNullException();
            }

            _repository.Remove(identity);
        }
Esempio n. 4
0
        public void Add(ContractIdentity identity, IComponentFactory factory)
        {
            if (!_contracts.ContainsKey(identity))
            {
                _contracts.Add(identity, new List <IComponentFactory>());
            }

            _contracts[identity].Add(factory);
        }
        public virtual IEnumerable <object> GetAllComponents(Type contract, string name)
        {
            var identity  = new ContractIdentity(contract, name);
            var factories = _repository.FindFactories(identity);

            return(factories
                   .Select(f => f.GetComponentInstance(identity, _compositionListeners.Values))
                   .Where(result => result != null));
        }
Esempio n. 6
0
        public virtual object GetComponent(Type contract, string name)
        {
            ComponentContextUtils.ThrowIfNotContract(contract);

            if (contract.ContainsGenericParameters)
            {
                throw new CompositionException("Requested contract type " + contract.Name +
                                               " contains open generic parameters. Can not construct a concrete type.");
            }

            var identity  = new ContractIdentity(contract, name);
            var factories = _repository.FindFactories(identity);

            return(factories
                   .Select(f => f.GetComponentInstance(identity, _compositionListeners.Values))
                   .FirstOrDefault(result => result != null));
        }
Esempio n. 7
0
        public IEnumerable <IComponentFactory> FindFactories(ContractIdentity identity)
        {
            _contracts.TryGetValue(identity, out var closedResults);
            if (!identity.Type.IsGenericType)
            {
                return(closedResults ?? Enumerable.Empty <IComponentFactory>());
            }

            var genericContractType = identity.Type.GetGenericTypeDefinition();
            var genericIdentity     = new ContractIdentity(genericContractType, identity.Name);

            if (_contracts.TryGetValue(genericIdentity, out var genericResults))
            {
                return(closedResults?.Concat(genericResults) ?? genericResults);
            }

            return(closedResults ?? Enumerable.Empty <IComponentFactory>());
        }
Esempio n. 8
0
        public IEnumerable <IComponentFactory> FindFactories(ContractIdentity identity)
        {
            var closedResults = (_contracts.ContainsKey(identity)
                                                ? _contracts[identity]
                                                : Enumerable.Empty <IComponentFactory>());

            if (identity.Type.IsGenericType)
            {
                var genericContractType = identity.Type.GetGenericTypeDefinition();
                var genericIdentity     = new ContractIdentity(genericContractType, identity.Name);

                var genericResults = (_contracts.ContainsKey(genericIdentity)
                                                        ? _contracts[genericIdentity]
                                                        : Enumerable.Empty <IComponentFactory>());

                return(closedResults.Concat(genericResults));
            }

            return(closedResults);
        }