예제 #1
0
 /// <summary>
 /// Create an IComponentRegistration from data.
 /// </summary>
 /// <param name="id">Id of the registration.</param>
 /// <param name="data">Registration data.</param>
 /// <param name="activator">Activator.</param>
 /// <param name="services">Services provided by the registration.</param>
 /// <returns>An IComponentRegistration.</returns>
 public static IComponentRegistration CreateRegistration(
     Guid id,
     RegistrationData data,
     IInstanceActivator activator,
     IEnumerable <Service> services)
 {
     return(CreateRegistration(id, data, activator, services, null));
 }
예제 #2
0
 /// <summary>
 /// Create an IComponentRegistration from data.
 /// </summary>
 /// <param name="id">Id of the registration.</param>
 /// <param name="data">Registration data.</param>
 /// <param name="activator">Activator.</param>
 /// <param name="pipelineBuilder">The component registration's resolve pipeline builder.</param>
 /// <param name="services">Services provided by the registration.</param>
 /// <returns>An IComponentRegistration.</returns>
 public static IComponentRegistration CreateRegistration(
     Guid id,
     RegistrationData data,
     IInstanceActivator activator,
     IResolvePipelineBuilder pipelineBuilder,
     Service[] services)
 {
     return(CreateRegistration(id, data, activator, pipelineBuilder, services, null));
 }
        /// <summary>
        /// Configure a single service that the component will provide.
        /// </summary>
        /// <param name="service">Service to expose.</param>
        /// <returns>A registration builder allowing further configuration of the component.</returns>
        public IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> As(Service service)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            RegistrationData.AddService(service);

            return(this);
        }
예제 #4
0
 public void CopyFrom(RegistrationData that, bool includeDefaultService)
 {
     this.Ownership = that.Ownership;
     this.Sharing   = that.Sharing;
     this.Lifetime  = that.Lifetime;
     this._defaultServiceOverridden |= that._defaultServiceOverridden;
     if (includeDefaultService)
     {
         this._defaultService = that._defaultService;
     }
     AddAll <Service>(this._services, that._services);
     AddAll <KeyValuePair <string, object> >(this.Metadata, that.Metadata);
     AddAll <EventHandler <PreparingEventArgs> >(this.PreparingHandlers, that.PreparingHandlers);
     AddAll <EventHandler <ActivatingEventArgs <object> > >(this.ActivatingHandlers, that.ActivatingHandlers);
     AddAll <EventHandler <ActivatedEventArgs <object> > >(this.ActivatedHandlers, that.ActivatedHandlers);
 }
예제 #5
0
        /// <summary>
        /// Copies the contents of another RegistrationData object into this one.
        /// </summary>
        /// <param name="that">The data to copy.</param>
        /// <param name="includeDefaultService">When true, the default service
        /// will be changed to that of the other.</param>
        public void CopyFrom(RegistrationData that, bool includeDefaultService)
        {
            Ownership = that.Ownership;
            Sharing   = that.Sharing;
            Lifetime  = that.Lifetime;

            _defaultServiceOverridden |= that._defaultServiceOverridden;
            if (includeDefaultService)
            {
                _defaultService = that._defaultService;
            }

            AddAll(_services, that._services);
            AddAll(Metadata, that.Metadata);
            AddAll(PreparingHandlers, that.PreparingHandlers);
            AddAll(ActivatingHandlers, that.ActivatingHandlers);
            AddAll(ActivatedHandlers, that.ActivatedHandlers);
        }
예제 #6
0
        public RegistrationBuilder(Service defaultService, TActivatorData activatorData, TRegistrationStyle style)
        {
            if (defaultService == null)
            {
                throw new ArgumentNullException("defaultService");
            }
            if ((object)activatorData == null)
            {
                throw new ArgumentNullException("activatorData");
            }
            if ((object)style == null)
            {
                throw new ArgumentNullException("style");
            }

            _activatorData     = activatorData;
            _registrationStyle = style;
            _registrationData  = new RegistrationData(defaultService);
        }
        public RegistrationBuilder(Service defaultService, TActivatorData activatorData, TRegistrationStyle style)
        {
            if (defaultService == null)
            {
                throw new ArgumentNullException(nameof(defaultService));
            }
            if (activatorData == null)
            {
                throw new ArgumentNullException(nameof(activatorData));
            }
            if (style == null)
            {
                throw new ArgumentNullException(nameof(style));
            }

            ActivatorData     = activatorData;
            RegistrationStyle = style;
            RegistrationData  = new RegistrationData(defaultService);
        }
예제 #8
0
        /// <summary>
        /// Copies the contents of another RegistrationData object into this one.
        /// </summary>
        /// <param name="that">The data to copy.</param>
        /// <param name="includeDefaultService">When true, the default service
        /// will be changed to that of the other.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="that" /> is <see langword="null" />.
        /// </exception>
        public void CopyFrom(RegistrationData that, bool includeDefaultService)
        {
            if (that == null)
            {
                throw new ArgumentNullException(nameof(that));
            }

            Ownership = that.Ownership;
            Sharing   = that.Sharing;
            Lifetime  = that.Lifetime;

            _defaultServiceOverridden |= that._defaultServiceOverridden;
            if (includeDefaultService)
            {
                _defaultService = that._defaultService;
            }

            AddAll(_services, that._services);
            AddAll(Metadata, that.Metadata.Where(m => m.Key != MetadataKeys.RegistrationOrderMetadataKey));
        }
예제 #9
0
        /// <summary>
        /// Copies the contents of another RegistrationData object into this one.
        /// </summary>
        /// <param name="that">The data to copy.</param>
        /// <param name="includeDefaultService">When true, the default service
        /// will be changed to that of the other.</param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="that" /> is <see langword="null" />.
        /// </exception>
        public void CopyFrom(RegistrationData that, bool includeDefaultService)
        {
            if (that == null)
            {
                throw new ArgumentNullException("that");
            }
            Ownership = that.Ownership;
            Sharing   = that.Sharing;
            Lifetime  = that.Lifetime;

            _defaultServiceOverridden |= that._defaultServiceOverridden;
            if (includeDefaultService)
            {
                _defaultService = that._defaultService;
            }

            AddAll(_services, that._services);
            AddAll(Metadata, that.Metadata);
            AddAll(PreparingHandlers, that.PreparingHandlers);
            AddAll(ActivatingHandlers, that.ActivatingHandlers);
            AddAll(ActivatedHandlers, that.ActivatedHandlers);
            AddAll(InjectPropertiesHandlers, that.InjectPropertiesHandlers);
        }
예제 #10
0
        public static IComponentRegistration CreateRegistration(Guid id, RegistrationData data, IInstanceActivator activator, IEnumerable <Service> services, IComponentRegistration target)
        {
            IComponentRegistration registration;
            Type limitType = activator.LimitType;

            if (limitType != typeof(object))
            {
                foreach (IServiceWithType type2 in services.OfType <IServiceWithType>())
                {
                    if (!type2.ServiceType.IsAssignableFrom(limitType))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationBuilderResources.ComponentDoesNotSupportService, new object[] { limitType, type2 }));
                    }
                }
            }
            if (target == null)
            {
                registration = new ComponentRegistration(id, activator, data.Lifetime, data.Sharing, data.Ownership, services, data.Metadata);
            }
            else
            {
                registration = new ComponentRegistration(id, activator, data.Lifetime, data.Sharing, data.Ownership, services, data.Metadata, target);
            }
            foreach (EventHandler <PreparingEventArgs> handler in data.PreparingHandlers)
            {
                registration.Preparing += handler;
            }
            foreach (EventHandler <ActivatingEventArgs <object> > handler2 in data.ActivatingHandlers)
            {
                registration.Activating += handler2;
            }
            foreach (EventHandler <ActivatedEventArgs <object> > handler3 in data.ActivatedHandlers)
            {
                registration.Activated += handler3;
            }
            return(registration);
        }
예제 #11
0
        /// <summary>
        /// Create an IComponentRegistration from data.
        /// </summary>
        /// <param name="id">Id of the registration.</param>
        /// <param name="data">Registration data.</param>
        /// <param name="activator">Activator.</param>
        /// <param name="services">Services provided by the registration.</param>
        /// <param name="target">Optional; target registration.</param>
        /// <returns>An IComponentRegistration.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="activator" /> or <paramref name="data" /> is <see langword="null" />.
        /// </exception>
        public static IComponentRegistration CreateRegistration(
            Guid id,
            RegistrationData data,
            IInstanceActivator activator,
            IEnumerable <Service> services,
            IComponentRegistration target)
        {
            if (activator == null)
            {
                throw new ArgumentNullException(nameof(activator));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var limitType = activator.LimitType;

            if (limitType != typeof(object))
            {
                foreach (var ts in services.OfType <IServiceWithType>())
                {
                    if (!ts.ServiceType.GetTypeInfo().IsAssignableFrom(limitType.GetTypeInfo()))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                                  RegistrationBuilderResources.ComponentDoesNotSupportService, limitType, ts));
                    }
                }
            }

            IComponentRegistration registration;

            if (target == null)
            {
                registration = new ComponentRegistration(
                    id,
                    activator,
                    data.Lifetime,
                    data.Sharing,
                    data.Ownership,
                    services,
                    data.Metadata);
            }
            else
            {
                registration = new ComponentRegistration(
                    id,
                    activator,
                    data.Lifetime,
                    data.Sharing,
                    data.Ownership,
                    services,
                    data.Metadata,
                    target);
            }

            foreach (var p in data.PreparingHandlers)
            {
                registration.Preparing += p;
            }

            foreach (var ac in data.ActivatingHandlers)
            {
                registration.Activating += ac;
            }

            foreach (var ad in data.ActivatedHandlers)
            {
                registration.Activated += ad;
            }

            return(registration);
        }
예제 #12
0
        /// <summary>
        /// Create an IComponentRegistration from data.
        /// </summary>
        /// <param name="id">Id of the registration.</param>
        /// <param name="data">Registration data.</param>
        /// <param name="activator">Activator.</param>
        /// <param name="pipelineBuilder">The component registration's resolve pipeline builder.</param>
        /// <param name="services">Services provided by the registration.</param>
        /// <param name="target">Optional; target registration.</param>
        /// <returns>An IComponentRegistration.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown if <paramref name="activator" /> or <paramref name="data" /> is <see langword="null" />.
        /// </exception>
        public static IComponentRegistration CreateRegistration(
            Guid id,
            RegistrationData data,
            IInstanceActivator activator,
            IResolvePipelineBuilder pipelineBuilder,
            Service[] services,
            IComponentRegistration?target)
        {
            if (activator == null)
            {
                throw new ArgumentNullException(nameof(activator));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (pipelineBuilder is null)
            {
                throw new ArgumentNullException(nameof(pipelineBuilder));
            }

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            var limitType = activator.LimitType;

            if (limitType != typeof(object))
            {
                foreach (var ts in services)
                {
                    if (!(ts is IServiceWithType asServiceWithType))
                    {
                        continue;
                    }

                    if (!asServiceWithType.ServiceType.IsAssignableFrom(limitType))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationBuilderResources.ComponentDoesNotSupportService, limitType, ts));
                    }
                }
            }

            // The pipeline builder fed into the registration is a copy, so that the original builder cannot be edited after the registration has been created,
            // and the original does not contain any auto-added items.
            var clonedPipelineBuilder = pipelineBuilder.Clone();

            IComponentRegistration registration;

            if (target == null)
            {
                registration = new ComponentRegistration(
                    id,
                    activator,
                    data.Lifetime,
                    data.Sharing,
                    data.Ownership,
                    clonedPipelineBuilder,
                    services,
                    data.Metadata,
                    data.Options);
            }
            else
            {
                registration = new ComponentRegistration(
                    id,
                    activator,
                    data.Lifetime,
                    data.Sharing,
                    data.Ownership,
                    clonedPipelineBuilder,
                    services,
                    data.Metadata,
                    target,
                    data.Options);
            }

            return(registration);
        }