Example #1
0
        /// <summary>
        /// Accepts the <see cref="TypedFactoryRegistration{TImplementation}"/> to visit.
        /// </summary>
        /// <param name="registration">The <see cref="TypedFactoryRegistration{TImplementation}"/> to visit.</param>
        public void Accept(TypedFactoryRegistration <TImplementation> registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            IComposition composition = new TypedFactoryComposition <TImplementation>(registration.Factory);

            _container.AddComposition(composition);
        }
        /// <summary>
        /// Accepts the <see cref="InjectedSingletonRegistration{TImplementation}"/> to visit.
        /// </summary>
        /// <param name="registration">
        /// The <see cref="InjectedSingletonRegistration{TImplementation}"/> to visit.
        /// </param>
        public void Accept(InjectedSingletonRegistration <TImplementation> registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            IComposition composition = new InjectedSingletonComposition <TImplementation>(registration.Value);

            _container.AddComposition(composition);
        }
Example #3
0
        /// <summary>
        /// Accepts the <see cref="FactoryRegistration"/> to visit.
        /// </summary>
        /// <param name="registration">The <see cref="FactoryRegistration"/> to visit.</param>
        public void Accept(FactoryRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            Type type        = registration.ImplementationType;
            var  composition = new FactoryComposition(type, registration.Factory);

            _container.AddComposition(composition);
        }
        /// <summary>
        /// Accepts the <see cref="SingletonRegistration"/> to visit.
        /// </summary>
        /// <param name="registration">The <see cref="SingletonRegistration"/> to visit.</param>
        public void Accept(SingletonRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            // Visit the inner registration which will add a composition.
            _manager.Visit(registration.Inner);

            // Get the original composition, removing it to allow it to be replaced.
            IComposition inner = _container.RemoveComposition(registration.ImplementationType);

            // Replace the inner composition.
            IComposition composition = new SingletonComposition(inner);

            _container.AddComposition(composition);
        }
        /// <summary>
        /// Accepts the <see cref="SingleConstructorRegistration"/> to visit.
        /// </summary>
        /// <param name="registration">The <see cref="SingleConstructorRegistration"/> to visit.</param>
        public void Accept(SingleConstructorRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            Type type = registration.ImplementationType;

            // Since this is default registrations it can be superseded by other registrations. Therefore if there is
            // already a registration for this type, don't replace it.
            if (_container.Compositions.ContainsKey(type))
            {
                return;
            }

            TypeInfo typeInfo = type.GetTypeInfo();

            ConstructorInfo[] constructors = typeInfo.GetConstructors(BindingFlags.Public | BindingFlags.Instance);

            // There must be a public constructor.
            if (constructors.Length == 0)
            {
                string message = $"The service of type '{type}' has no public constructors.";
                throw new CompositionException(message);
            }

            // There must be just 1 public constructor.
            if (constructors.Length > 1)
            {
                string message = $"The service of type '{type}' has {constructors.Length:N0} " +
                                 "public constructors. There must be just 1.";
                throw new CompositionException(message);
            }

            ParameterInfo[] parameters  = constructors[0].GetParameters();
            IComposition    composition = new ConstructorComposition(type, parameters, isDefault: true);

            _container.AddComposition(composition);
        }