Exemple #1
0
        private void assertExportsAndDispose(MefSingletonComposer composer)
        {
            int         composedCount = 0;
            MefComposer lastComposer  = null;
            ComposerEventArgs <ContainerConfiguration> lastComposedEventArgs = null;

            composer.Composed += (s, e) =>
            {
                ++composedCount;
                lastComposer          = s as MefComposer;
                lastComposedEventArgs = e;
            };

            Assert.AreSame(composer.Compose(), lastComposedEventArgs.Target);
            Assert.AreEqual(1, composedCount);
            Assert.IsNotNull(lastComposedEventArgs);
            Assert.IsNotNull(lastComposedEventArgs.Target);
            Assert.IsNotNull(lastComposer);
            Assert.AreSame(composer, lastComposer);

            IParent[] exports
                = composer.ExportsList.OfType <IParent>()
                  .ToArray();
            Helper.AssertValidParents(exports);
            composer.Dispose();
            if (composer.DisposeCompositionHostsWithThis)
            {
                Helper.AssertDisposedParents(exports);
            }
            else
            {
                Helper.AssertValidParents(exports);
            }
        }
Exemple #2
0
 /// <summary>
 /// This method is virtual and implements
 /// <see cref="IHandleComposed{TTarget}.HandleComposed{T}"/>.
 /// This implementation invokes all participants added here.
 /// Notice that each is invoked in a catch block, and this will trace exceptions,
 /// AND re-throw a new <see cref="AggregateException"/> if there are
 /// any exceptions.
 /// </summary>
 /// <param name="eventArgs"><see cref="IHandleComposed{TTarget}"/> argument.</param>
 /// <exception cref="AggregateException"></exception>
 public void HandleComposed <T>(ComposerEventArgs <T> eventArgs)
     where T : TTarget
 {
     lock (Participants) {
         checkDisposed();
         List <Exception> exceptions = new List <Exception>(Participants.Count);
         foreach (IHandleComposed <TTarget> participant
                  in Participants.OfType <IHandleComposed <TTarget> >())
         {
             try {
                 participant.HandleComposed(eventArgs);
             } catch (Exception exception) {
                 TraceSources.For(GetType())
                 .Error(
                     exception,
                     "Participant exception invoking HandleComposed: {0}, '{1}'.",
                     participant,
                     exception.Message);
                 exceptions.Add(exception);
             }
         }
         if (exceptions.Count != 0)
         {
             throw new AggregateException(
                       $"One or more exceptions was raised by a {GetType().GetFriendlyName()} participant in"
                       + $" {nameof(IHandleComposed<TTarget>.HandleComposed)}.",
                       exceptions.EnumerateInReverse());
         }
     }
 }
Exemple #3
0
        private void assertExportsAndDispose(MefComposer composer)
        {
            int         composedCount = 0;
            MefComposer lastComposer  = null;
            ComposerEventArgs <ContainerConfiguration> lastComposedEventArgs = null;

            composer.Composed += (s, e) =>
            {
                ++composedCount;
                lastComposer          = s as MefComposer;
                lastComposedEventArgs = e;
            };

            Assert.AreSame(composer.Compose(), lastComposedEventArgs.Target);
            Assert.AreEqual(1, composedCount);
            Assert.IsNotNull(lastComposedEventArgs);
            Assert.IsNotNull(lastComposedEventArgs.Target);
            Assert.IsNotNull(lastComposer);
            Assert.AreSame(composer, lastComposer);

            CompositionHost compositionHost = lastComposedEventArgs.Target.CreateContainer();
            List <IParent>  parts           = new List <IParent>(3);

            parts.AddRange(compositionHost.GetExports <IParent>());
            Assert.AreEqual(3, parts.Count);

            Helper.AssertValidParents(parts);
            composer.Dispose();
            Helper.AssertValidParents(parts);
            compositionHost.Dispose();
            Helper.AssertDisposedParents(parts);
        }
Exemple #4
0
 public void HandleBootstrap <T>(ComposerEventArgs <T> eventArgs)
     where T : MyTarget
 => eventArgs.Target.Bootstrap();
Exemple #5
0
 public void HandleComposed <T>(ComposerEventArgs <T> eventArgs)
     where T : TTarget
 {
     checkDisposed();
     handleComposed?.Invoke(new ComposerEventArgs <TTarget>(eventArgs.Target));
 }
Exemple #6
0
 public void HandleBootstrap <T>(ComposerEventArgs <T> eventArgs)
     where T : TTarget
 {
     checkDisposed();
     bootstrap?.Invoke(new ComposerEventArgs <TTarget>(eventArgs.Target));
 }
Exemple #7
0
 public void HandleComposed <T>(ComposerEventArgs <T> eventArgs)
     where T : TTarget
 => (Participant as IHandleComposed <TSuper>)?.HandleComposed(eventArgs);
Exemple #8
0
 public void HandleBootstrap <T>(ComposerEventArgs <T> eventArgs)
     where T : TTarget
 => (Participant as IBootstrap <TSuper>)?.HandleBootstrap(eventArgs);