/// <summary> /// Constructor. /// </summary> /// <param name="participant">Required.</param> public VariantParticipant(IComposerParticipant <TSuper> participant) { Participant = participant ?? throw new ArgumentNullException(nameof(participant)); if (Participant is IRequestComposition <TTarget> requestComposition) { requestComposition.CompositionRequested += handleCompositionRequested; } }
/// <summary> /// This protected virtual method raises the /// <see cref="IRequestComposition{TTarget}.CompositionRequested"/> /// event. /// </summary> /// <param name="participant">Optional parameter for a new /// <see cref="RequestCompositionEventArgs{TTarget}"/>: notice that if this is not null, /// the <paramref name="request"/> must be set..</param> /// <param name="request">Optional parameter for a new /// <see cref="RequestCompositionEventArgs{TTarget}"/>.</param> protected virtual void RaiseCompositionRequested( IComposerParticipant <ContainerConfiguration> participant = null, RequestCompositionEventArgs <ContainerConfiguration> .ParticipantRequest request = RequestCompositionEventArgs <ContainerConfiguration> .ParticipantRequest.None) => CompositionRequested?.Invoke( this, participant != null ? new RequestCompositionEventArgs <ContainerConfiguration>(participant, request) : new RequestCompositionEventArgs <ContainerConfiguration>());
/// <summary> /// Constructor sets the <see cref="Participant"/>; and the /// <see cref="Request"/>. /// </summary> /// <param name="participant">Required in this constructor.</param> /// <param name="request">Required in this constructor; and must not be /// <see cref="ParticipantRequest.None"/>.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InvalidEnumArgumentException"></exception> public RequestCompositionEventArgs(IComposerParticipant <TTarget> participant, ParticipantRequest request) { if (!Enum.IsDefined(typeof(ParticipantRequest), request) || (request == ParticipantRequest.None)) { throw new InvalidEnumArgumentException(nameof(request), (int)request, typeof(ParticipantRequest)); } Participant = participant ?? throw new ArgumentNullException(nameof(participant)); Request = request; }
/// <summary> /// This helper method will check and construct a new variant participant, that /// implements the composer's covariant type, and contributes all supported interfaces to /// the composer. Since this will construct a new participant, the actual /// added instance is returned here. /// </summary> /// <typeparam name="TSuper">The delegate's actual type.</typeparam> /// <typeparam name="TTarget">This composer's implemented type.</typeparam> /// <param name="composer">Required.</param> /// <param name="participant">Required.</param> /// <param name="oneTimeOnly">Optional.</param> /// <returns>The actual added instance.</returns> /// <exception cref="ArgumentNullException"/> public static VariantParticipant <TSuper, TTarget> ParticipateAs <TSuper, TTarget>( this IComposer <TTarget> composer, IComposerParticipant <TSuper> participant, bool oneTimeOnly = false) where TTarget : TSuper { if (composer == null) { throw new ArgumentNullException(nameof(composer)); } VariantParticipant <TSuper, TTarget> variantParticipant = new VariantParticipant <TSuper, TTarget>(participant); composer.Participate(variantParticipant, oneTimeOnly); return(variantParticipant); }
/// <summary> /// Removes the participant. /// </summary> /// <param name="participant">Not null.</param> /// <returns>False if the argument is not found.</returns> /// <exception cref="ArgumentNullException"/> public bool Remove(IComposerParticipant <TTarget> participant) { if (participant == null) { throw new ArgumentNullException(nameof(participant)); } lock (Participants) { if (participant is IRequestComposition <TTarget> requestComposition) { requestComposition.CompositionRequested -= HandleParticipantCompositionRequested; } if (!Participants.Remove(participant)) { return(false); } HandleParticipantRemoved(participant); return(true); } }
/// <summary> /// Adds the participant. /// </summary> /// <param name="participant">Not null.</param> /// <returns>False if the argument is already added.</returns> /// <exception cref="ArgumentNullException"/> public bool Add(IComposerParticipant <TTarget> participant) { if (participant == null) { throw new ArgumentNullException(nameof(participant)); } lock (Participants) { checkDisposed(); if (Participants.Contains(participant)) { return(false); } Participants.Add(participant); if (participant is IRequestComposition <TTarget> requestComposition) { requestComposition.CompositionRequested -= HandleParticipantCompositionRequested; requestComposition.CompositionRequested += HandleParticipantCompositionRequested; } HandleParticipantAdded(participant); return(true); } }
/// <summary> /// Will be invoked with each removed participant, while holding the sync lock. /// </summary> /// <param name="removedParticipant">Not null.</param> protected virtual void HandleParticipantRemoved(IComposerParticipant <TTarget> removedParticipant) { }
/// <summary> /// Will be invoked with each added participant, while holding the sync lock. /// </summary> /// <param name="addedParticipant">Not null.</param> protected virtual void HandleParticipantAdded(IComposerParticipant <TTarget> addedParticipant) { }
public bool Equals(IComposerParticipant <TSuper> other) => (other != null) && object.ReferenceEquals(Participant, other);
public bool Equals(IComposerParticipant <TTarget> other) => Equals(other as VariantParticipant <TSuper, TTarget>);