Exemplo n.º 1
0
        /// <summary>
        /// Creates a new relation between the supplied <see cref="InstanceDescriptor"/> and instance.
        /// </summary>
        /// <param name="instanceDescriptor">The <see cref="InstanceDescriptor"/>.</param>
        /// <param name="instance">The instance.</param>
        /// <exception cref="InvalidOperationException">The relation between the supplied <see cref="InstanceDescriptor"/> and instance already exists.</exception>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="instanceDescriptor"/>' and '<paramref name="instance"/>' cannot be null.</exception>
        public void CreateRelation(InstanceDescriptor instanceDescriptor, Object instance)
        {
            if (instanceDescriptor == null)
            {
                throw new ArgumentNullException(nameof(instanceDescriptor));
            }

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

            if (
                this.instanceDescriptorInstanceRelations.Any(
                    idir => idir.InstanceDescriptor == instanceDescriptor && idir.Instance == instance))
            {
                throw new InvalidOperationException(String.Format(ExceptionMessages.RelationAlreadyPresentExceptionMessage, instanceDescriptor.AssemblyQualifiedTypeName, instance.GetType().Name));
            }

            var newInstanceRelation = new InstanceDescriptorInstanceRelation(instanceDescriptor, instance);

            this.instanceDescriptorInstanceRelations.Add(newInstanceRelation);

            this.OnInstanceRelationChanged(newInstanceRelation, InstanceRelationChangeType.Created);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Raises the InstanceRelationChanged event.
        /// </summary>
        /// <param name="relation">The <see cref="InstanceDescriptorInstanceRelation"/> relation.</param>
        /// <param name="changeType">The type of change occured.</param>
        /// <exception cref="ArgumentNullException">The value of '<paramref name="relation"/>' cannot be null.</exception>
        private void OnInstanceRelationChanged([NotNull] InstanceDescriptorInstanceRelation relation,
                                               InstanceRelationChangeType changeType)
        {
            if (relation == null)
            {
                throw new ArgumentNullException(nameof(relation));
            }

            var handler = this.instanceRelationChanged;

            handler?.Invoke(this, new InstanceRelationChangedEventArgs(relation.InstanceDescriptor, relation.Instance, changeType));
        }