public IEnumerable <object> GetServices(Type serviceType)
        {
            var baseResult = _baseResolver.GetServices(serviceType);

            baseResult.ToList().ForEach(o => Composer.InitializePlugs(o, o.GetType()));
            return(ComponentContextUtils.HasContractAttribute(serviceType) ? Composer.GetAllComponents(serviceType).Concat(baseResult) : baseResult);
        }
Esempio n. 2
0
        public object GetService(Type serviceType)
        {
            object result = null;

            if (ComponentContextUtils.HasContractAttribute(serviceType))
            {
                result = Composer.GetComponent(serviceType);
            }

            if (result == null)
            {
                result = _baseResolver.GetService(serviceType);

                if (result != null)
                {
                    Composer.InitializePlugs(result, result.GetType());
                }
            }

            return(result);
        }
Esempio n. 3
0
        private void LoadTargetConstructor()
        {
            // If the constructor arguments are specified by the creator of the factory,
            // ignore finding the constructor. The constructor to be used will be bound
            // when creating the component.

            if (_constructorArgs != null)
            {
                _targetConstructor = null;
                return;
            }

            // Ignore finding the constructor if the creator of the factory has specified one.
            // Just extract the constructor args.

            if (_targetConstructor == null)
            {
                // Find the appropriate constructor for instantiating the component.
                // Order of precedence:
                //     1. The one marked with [CompositionConstructor]
                //     2. If there's a single public constructor, use it.
                //     3. If there's the default constructor, use it.
                // And it is an exception if none of the above is found.

                var candidateConstructors = _targetType.GetConstructors();
                _targetConstructor = FindMarkedConstructor(_targetType, candidateConstructors) ??
                                     FindSingleConstructor(candidateConstructors) ??
                                     FindDefaultConstructor(candidateConstructors);

                if (_targetConstructor == null)
                {
                    throw new CompositionException(
                              "There's no appropriate constructor identified as the composition constructor for type '" + _targetType.FullName +
                              "'" +
                              "You can fix this by using [CompositionConstructor] attribute on the constructor that you intend to be used by Composer.");
                }
            }

            _constructorArgs = new List <ConstructorArgSpecification>();
            string[] queryNames = null;

            if (ComponentContextUtils.HasCompositionConstructorAttribute(_targetConstructor))
            {
                queryNames = ComponentContextUtils.GetCompositionConstructorAttribute(_targetConstructor).Names;
            }

            foreach (var parameterInfo in _targetConstructor.GetParameters())
            {
                if (!ComponentContextUtils.HasContractAttribute(parameterInfo.ParameterType))
                {
                    throw new CompositionException(
                              $"Parameter '{parameterInfo.Name}' of the constructor of type '{_targetType.FullName}' is not of a Contract type. " +
                              "All parameters of the composition constructor must be of Contract types, so that Composer can query for a component and pass it to them.");
                }

                if ((queryNames != null) && (queryNames.Length > parameterInfo.Position))
                {
                    _constructorArgs.Add(new ConstructorArgSpecification(true,
                                                                         new ComponentQuery(parameterInfo.ParameterType,
                                                                                            queryNames[parameterInfo.Position])));
                }
                else
                {
                    _constructorArgs.Add(new ConstructorArgSpecification(true, new ComponentQuery(parameterInfo.ParameterType, null)));
                }
            }

            if ((queryNames != null) && (queryNames.Length > _constructorArgs.Count))
            {
                throw new CompositionException("Extra names are specified for the constructor of type '" +
                                               _targetType.FullName + "'");
            }
        }