Пример #1
0
        /// <summary>
        /// Register a provided object instance within the component provider as an implementation of the provided
        /// interface type, using the default singleton lifetime.
        /// </summary>
        /// <typeparam name="TInterface">The interface type.</typeparam>
        /// <param name="instance">The object instance.</param>
        /// <exception cref="ArgumentNullException">instance - The component instance cannot be null</exception>
        /// <exception cref="ArgumentException">
        /// </exception>
        public void Register <TInterface>(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance), "The component instance cannot be null");
            }

            var type          = instance.GetType();
            var interfaceType = typeof(TInterface);

            if (!interfaceType.IsAssignableFrom(type))
            {
                throw new ArgumentException($"The component instance does not implement the interface '{interfaceType.FullName}'");
            }

            var entry = new ComponentDescriptor(type, null, new[] { interfaceType }, null);

            entry.Designer = new InstancedDesigner(instance);

            if (implementedEntries.ContainsKey(interfaceType))
            {
                throw new ArgumentException($"Conflicting implementation of '{interfaceType.FullName}'");
            }

            implementedEntries.Add(interfaceType, entry);
        }
Пример #2
0
        /// <summary>
        /// Register a provided object instance within the component provider using the default singleton lifetime.
        /// </summary>
        /// <param name="instance">The object instance.</param>
        /// <param name="names">An optional collection of identifiers which the components will be stored under.</param>
        public void Register(object instance, params string[] names)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance), "The component instance cannot be null");
            }

            if (names == null)
            {
                names = new string[0];
            }

            var type       = instance.GetType();
            var implements = new List <Type>(GetImplementedTypes(type));

            implements.Add(type);

            var entry = new ComponentDescriptor(type, null, Type.EmptyTypes, names);

            entry.Designer = new InstancedDesigner(instance);

            foreach (var implementedType in implements)
            {
                implementedEntries.AddIfMissing(implementedType, entry);
            }

            if (!typedEntries.ContainsKey(type))
            {
                typedEntries.Add(type, entry);
            }

            foreach (var name in names)
            {
                if (name == null)
                {
                    continue;
                }

                if (namedEntries.TryGetValue(name, out var existing))
                {
                    throw new ArgumentException($"Conflicting named '{name}' component, adding '{type.FullName}' against '{existing.Type.FullName}'");
                }

                namedEntries.Add(name, entry);
            }
        }
Пример #3
0
        /// <summary>
        /// Register a provided interface and concrete type within the component provider using the default singleton lifetime.
        /// </summary>
        /// <param name="interfaceType">The interface type.</param>
        /// <param name="concreteType">The concrete type.</param>
        /// <param name="names">An optional collection of identifiers which the components will be stored under.</param>
        /// <returns>
        /// A configurator which can be used to further configure the component state.
        /// </returns>
        public IComponentRegistration Register(Type interfaceType, Type concreteType, params string[] names)
        {
            var implements = new List <Type>(GetImplementedTypes(concreteType));
            var hasName    = names != null && names.Length != 0;

            if (names == null || names.Length == 0)
            {
                names = implements.Concat(new[] { interfaceType, concreteType }).Where(t => t != null).Distinct().Select(t => t.Name).ToArray();
            }

            var constructor = CreateConstructor(concreteType);
            var entry       = new ComponentDescriptor(concreteType, constructor, interfaceType != null ? new[] { interfaceType } : Type.EmptyTypes, names);

            entry.Designer = new SingletonDesigner(constructor);

            foreach (var implementedType in implements)
            {
                implementedEntries.AddIfMissing(implementedType, entry);
            }

            if (interfaceType != null)
            {
                if (implementedEntries.TryGetValue(interfaceType, out var existing) && !hasName)
                {
                    throw new ArgumentException($"Conflicting '{interfaceType.FullName}' component, adding '{concreteType.FullName}' against '{existing.Type.FullName}'");
                }

                if (existing == null)
                {
                    implementedEntries.Add(interfaceType, entry);
                }
            }

            if (!typedEntries.ContainsKey(concreteType))
            {
                typedEntries.Add(concreteType, entry);
            }

            foreach (var name in names)
            {
                if (name == null)
                {
                    continue;
                }

                if (namedEntries.TryGetValue(name, out var existing))
                {
                    throw new ArgumentException($"Conflicting named '{name}' component, adding '{concreteType.FullName}' against '{existing.Type.FullName}'");
                }

                namedEntries.Add(name, entry);
            }

            allEntries.Add(entry);

            if (ReflectionHelper.GetAttributeInherited <ConfigurationAttribute>(concreteType) != null)
            {
                configurationEntries.Add(entry);
            }

            return(new ComponentRegistration(new[] { entry }));
        }