Ejemplo n.º 1
0
        /// <summary>
        /// This method provides an implementation for an <see cref="IComposer{TTarget}"/>
        /// to handle this event. This checks the <see cref="Participant"/>, and the
        /// <see cref="Request"/>, and will invoke your <paramref name="composer"/>
        /// as specified.
        /// </summary>
        /// <typeparam name="T">Your <paramref name="composer"/>
        /// actual (covariant) type.</typeparam>
        /// <param name="composer">Not null.</param>
        /// <returns>True if any method was invoked on the <paramref name="composer"/>.
        /// False if not invoked.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public bool Handle <T>(IComposer <T> composer)
            where T : TTarget
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            if (Participant == null)
            {
                return(false);
            }
            IComposer <TTarget> targetComposer = composer as IComposer <TTarget>;

            switch (Request)
            {
            case ParticipantRequest.Add:
                if (targetComposer != null)
                {
                    targetComposer.Participate(Participant);
                    return(true);
                }
                composer.ParticipateAs(Participant);
                return(true);

            case ParticipantRequest.AddOneTimeOnly:
                if (targetComposer != null)
                {
                    targetComposer.Participate(Participant, true);
                    return(true);
                }
                composer.ParticipateAs(Participant, true);
                return(true);

            case ParticipantRequest.Remove:
                if (targetComposer != null)
                {
                    targetComposer.Remove(Participant);
                    return(true);
                }
                composer.Remove(new VariantParticipant <TTarget, T>(Participant));
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convenience method that constructs a new <see cref="IProvideParts{TTarget}"/>
        /// participant that will invoke your action, and participates it with this composer.
        /// Since this will construct a new participant, the new
        /// added instance is returned here.
        /// See also <see cref="DelegateComposerParticipant{TTarget}"/>.
        /// </summary>
        /// <typeparam name="TTarget">The type of the target handled
        /// by the specific <see cref="IComposer{TTarget}"/>.</typeparam>
        /// <param name="composer">Required.</param>
        /// <param name="provideParts">Required.</param>
        /// <param name="oneTimeOnly">Optional.</param>
        /// <returns>Not null: the added participant.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static DelegateComposerParticipant <TTarget> ParticipatePartsDelegate <TTarget>(
            this IComposer <TTarget> composer,
            Action <ProvidePartsEventArgs <TTarget> > provideParts,
            bool oneTimeOnly = false)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            DelegateComposerParticipant <TTarget> delegateParticipant = new DelegateComposerParticipant <TTarget>(provideParts);

            composer.Participate(delegateParticipant, oneTimeOnly);
            return(delegateParticipant);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Convenience method that constructs a new <see cref="IBootstrap{TTarget}"/>
        /// participant that will invoke your action, and participates it with this composer.
        /// Since this will construct a new participant, the new
        /// added instance is returned here.
        /// See also <see cref="DelegateComposerParticipant{TTarget}"/>.
        /// </summary>
        /// <typeparam name="TTarget">The type of the target handled
        /// by the specific <see cref="IComposer{TTarget}"/>.</typeparam>
        /// <param name="composer">Required.</param>
        /// <param name="bootstrap">Required.</param>
        /// <param name="oneTimeOnly">Optional.</param>
        /// <returns>Not null: the added participant.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static DelegateComposerParticipant <TTarget> ParticipateBootstrapDelegate <TTarget>(
            this IComposer <TTarget> composer,
            Action <ComposerEventArgs <TTarget> > bootstrap,
            bool oneTimeOnly = false)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            DelegateComposerParticipant <TTarget> delegateParticipant = new DelegateComposerParticipant <TTarget>(null, bootstrap);

            composer.Participate(delegateParticipant, oneTimeOnly);
            return(delegateParticipant);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Convenience method that constructs a new <see cref="IRequestComposition{TTarget}"/>
        /// participant that will raise the <see cref="IRequestComposition{TTarget}.CompositionRequested"/>
        /// event, and participates it with this composer.
        /// Since this will construct a new participant, the new
        /// added instance is returned here.
        /// See also <see cref="DelegateComposerParticipant{TTarget}"/>.
        /// </summary>
        /// <typeparam name="TTarget">The type of the target handled
        /// by the specific <see cref="IComposer{TTarget}"/>.</typeparam>
        /// <param name="composer">Required.</param>
        /// <param name="requestComposition">Will be set to an action that will
        /// raise the <see cref="IRequestComposition{TTarget}.CompositionRequested"/>
        /// event from the returned participant.</param>
        /// <param name="oneTimeOnly">Optional.</param>
        /// <returns>Not null: the added participant.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static DelegateComposerParticipant <TTarget> ParticipateRequestCompositionDelegate <TTarget>(
            this IComposer <TTarget> composer,
            out Action <RequestCompositionEventArgs <TTarget> > requestComposition,
            bool oneTimeOnly = false)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            DelegateComposerParticipant <TTarget> delegateParticipant
                = new DelegateComposerParticipant <TTarget>(out requestComposition);

            composer.Participate(delegateParticipant, oneTimeOnly);
            return(delegateParticipant);
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Convenience method that constructs a new
        /// <see cref="AggregateComposerParticipant{TTarget}"/> from the
        /// given list, and participates it with your composer. All interfaces
        /// implemented by your participants will be invoked. Since this
        /// will construct a new participant, the actual
        /// added instance is returned here.
        /// </summary>
        /// <typeparam name="TTarget">The type of the target handled
        /// by the specific <see cref="IComposer{TTarget}"/>.</typeparam>
        /// <param name="composer">Required.</param>
        /// <param name="participants">Required.</param>
        /// <param name="oneTimeOnly">Optional.</param>
        /// <param name="disposeParticipantsWithAggregate">Defaults to true: the participants
        /// will be disposed with this new aggregate participant.</param>
        /// <returns>Not null.</returns>
        /// <exception cref="ArgumentNullException"/>
        public static AggregateComposerParticipant <TTarget> Aggregate <TTarget>(
            this IComposer <TTarget> composer,
            IEnumerable <IComposerParticipant <TTarget> > participants,
            bool oneTimeOnly = false,
            bool disposeParticipantsWithAggregate = true)
        {
            if (composer == null)
            {
                throw new ArgumentNullException(nameof(composer));
            }
            AggregateComposerParticipant <TTarget> aggregateParticipant
                = new AggregateComposerParticipant <TTarget>(participants)
                {
                DisposeParticipantsWithThis = disposeParticipantsWithAggregate
                };

            composer.Participate(aggregateParticipant, oneTimeOnly);
            return(aggregateParticipant);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Convenience method contributes each participant to your composer.
 /// </summary>
 /// <typeparam name="TTarget">The type of the target handled
 /// by the specific <see cref="IComposer{TTarget}"/>.</typeparam>
 /// <param name="composer">Required.</param>
 /// <param name="participants">Required.</param>
 /// <param name="oneTimeOnly">Optional.</param>
 /// <returns>Your composer.</returns>
 /// <exception cref="ArgumentNullException"/>
 public static IComposer <TTarget> ParticipateRange <TTarget>(
     this IComposer <TTarget> composer,
     IEnumerable <IComposerParticipant <TTarget> > participants,
     bool oneTimeOnly = false)
 {
     if (composer == null)
     {
         throw new ArgumentNullException(nameof(composer));
     }
     if (participants == null)
     {
         throw new ArgumentNullException(nameof(participants));
     }
     foreach (IComposerParticipant <TTarget> composerParticipant in participants)
     {
         composer.Participate(composerParticipant, oneTimeOnly);
     }
     return(composer);
 }