Ejemplo n.º 1
0
        public void When_removing_a_part_from_the_intercepted_catalog_intercepting_catalog_is_recomposed_and_removes_that_part()
        {
            var innerCatalog1 = new TypeCatalog(typeof(RecomposablePart1), typeof(RecomposablePartImporter));
            var innerCatalog2 = new TypeCatalog(typeof(RecomposablePart2));
            var cfg = new InterceptionConfiguration().AddInterceptor(new RecomposablePartInterceptor());
            var aggregateCatalog = new AggregateCatalog(innerCatalog1, innerCatalog2);
            var catalog = new InterceptingCatalog(aggregateCatalog, cfg);
            container = new CompositionContainer(catalog);

            var importer = container.GetExportedValue<RecomposablePartImporter>();
            Assert.That(importer, Is.Not.Null);
            Assert.That(importer.Parts, Is.Not.Null);
            Assert.That(importer.Parts.Length, Is.EqualTo(2));
            Assert.That(importer.Parts[0].Count, Is.EqualTo(1));
            Assert.That(importer.Parts[1].Count, Is.EqualTo(1));
            Assert.That(catalog.Parts.Count(), Is.EqualTo(3));

            // Recompose
            aggregateCatalog.Catalogs.Remove(innerCatalog2);

            Assert.That(importer, Is.Not.Null);
            Assert.That(importer.Parts, Is.Not.Null);
            Assert.That(importer.Parts.Length, Is.EqualTo(1));
            Assert.That(importer.Parts[0].Count, Is.EqualTo(1));
            Assert.That(catalog.Parts.Count(), Is.EqualTo(2));
        }
        public void Adding_s_is_reflecteed_in_the_Interceptors_collection()
        {
            var cfg = new InterceptionConfiguration()
                .AddInterceptionCriteria(new PredicateInterceptionCriteria(new EmptyInterceptor(), part => true));

            Assert.That(cfg.InterceptionCriteria.Count(), Is.EqualTo(1));
            Assert.That(cfg.InterceptionCriteria.OfType<PredicateInterceptionCriteria>().Any());
        }
        public void When_querying_for_a_part_not_being_intercepted_it_should_return_original_part_definition()
        {
            var innerCatalog = new TypeCatalog(typeof(Logger));
            var cfg = new InterceptionConfiguration();
            var catalog = new InterceptingCatalog(innerCatalog, cfg);

            var partDefinition = catalog.Parts.First();
            partDefinition.ShouldNotBeOfType<InterceptingComposablePartDefinition>();
        }
        public void When_querying_for_a_part_it_should_return_an_intercepting_part_definition()
        {
            var innerCatalog = new TypeCatalog(typeof(Logger));
            var mockInterceptor = new Mock<IExportedValueInterceptor>();
            var cfg = new InterceptionConfiguration().AddInterceptor(mockInterceptor.Object);
            var catalog = new InterceptingCatalog(innerCatalog, cfg);

            var partDefinition = catalog.Parts.First();
            partDefinition.ShouldBeOfType<InterceptingComposablePartDefinition>();
        }
        public void Adding_part_handlers_is_reflected_in_the_PartHandlers_collection()
        {
            var cfg = new InterceptionConfiguration()
                .AddHandler(new FakePartHandler1())
                .AddHandler(new FakePartHandler1());

            Assert.That(cfg.PartHandlers.Count(), Is.EqualTo(2));
            Assert.That(cfg.PartHandlers.OfType<FakePartHandler1>().Any());
            Assert.That(cfg.PartHandlers.OfType<FakePartHandler1>().Any());
        }
        public void Adding_export_handlers_is_reflecteed_in_the_Handlers_collection()
        {
            var cfg = new InterceptionConfiguration()
                .AddHandler(new GenericExportHandler())
                .AddHandler(new ConcreteTypeExportHandler());

            Assert.That(cfg.Handlers.Count(), Is.EqualTo(2));
            Assert.That(cfg.Handlers.OfType<GenericExportHandler>().Any());
            Assert.That(cfg.Handlers.OfType<ConcreteTypeExportHandler>().Any());
        }
        public void Adding_interceptors_is_reflecteed_in_the_Interceptors_collection()
        {
            var cfg = new InterceptionConfiguration()
                .AddInterceptor(new CompositeValueInterceptor())
                .AddInterceptor(new EmptyInterceptor());

            Assert.That(cfg.Interceptors.Count(), Is.EqualTo(2));
            Assert.That(cfg.Interceptors.OfType<CompositeValueInterceptor>().Any());
            Assert.That(cfg.Interceptors.OfType<EmptyInterceptor>().Any());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConventionCatalog"/> class, using the provided array part registries.
        /// </summary>
        /// <param name="registries">An array of <see cref="IPartRegistry{T}"/> instance.</param>
        public ConventionCatalog(params IPartRegistry<IContractService>[] registries)
        {
            if (registries == null)
            {
                throw new ArgumentNullException("registries", "The registries parameter cannot be null.");
            }

            var cfg = new InterceptionConfiguration()
                .AddHandler(new ConventionPartHandler(registries));
            this.interceptingCatalog = new InterceptingCatalog(new EmptyCatalog(), cfg);
        }
 public void TestSetUp()
 {
     var innerCatalog = new TypeCatalog(typeof(Customer));
     var interceptor = new FreezableInterceptor();
     interceptor.Freeze();
     var valueInterceptor = new DynamicProxyInterceptor(interceptor);
     var cfg = new InterceptionConfiguration()
         .AddInterceptor(valueInterceptor);
     var catalog = new InterceptingCatalog(innerCatalog, cfg);
     container = new CompositionContainer(catalog);
 }
 public void TestSetUp()
 {
     var innerCatalog = new TypeCatalog(typeof(Part1), typeof(Part2), typeof(Part3));
     var cfg = new InterceptionConfiguration()
         .AddInterceptor(new GeneralInterceptor())
         .AddInterceptionCriteria(
             new PredicateInterceptionCriteria(
                 new PartInterceptor(), d => d.Metadata.ContainsKey("metadata1")));
     var catalog = new InterceptingCatalog(innerCatalog, cfg);
     container = new CompositionContainer(catalog);
 }
        public void TestSetUp()
        {
            var typeCatalog = new TypeCatalog(typeof(CtorOrderProcessor), typeof(OrderProcessor), typeof(TestGenericContractRegistry));
            var cfg = new InterceptionConfiguration().AddHandler(new GenericExportHandler());
            var catalog = new InterceptingCatalog(typeCatalog, cfg);

            var provider = new CatalogExportProvider(catalog);
            provider.SourceProvider = provider;

            ExportProvider = provider;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FilteringCatalog"/> class.
        /// </summary>
        /// <param name="inner">A <see cref="ComposablePartCatalog"/> whose parts
        /// are to be filtered based on a given criteria.</param>
        /// <param name="filter">A filter query.</param>
        public FilteringCatalog(ComposablePartCatalog inner, Func<ComposablePartDefinition, bool> filter)
        {
            if (inner == null)
                throw new ArgumentNullException("inner");

            if (filter == null)
                throw new ArgumentNullException("filter");

            var cfg = new InterceptionConfiguration()
                .AddHandler(new FilteringPartHandler(filter));
            this.interceptingCatalog = new InterceptingCatalog(inner, cfg);
        }
        public void Catalog_should_filter_out_parts()
        {
            var innerCatalog = new TypeCatalog(typeof(Part0), typeof(Part1), typeof(Part2), typeof(Part3));
            var cfg = new InterceptionConfiguration()
                .AddHandler(new PartFilter());
            var catalog = new InterceptingCatalog(innerCatalog, cfg);
            container = new CompositionContainer(catalog);

            var parts = container.GetExportedValues<IPart>();
            Assert.That(parts.Count(), Is.EqualTo(1));
            Assert.That(parts.First().GetType(), Is.EqualTo(typeof(Part0)));
        }
Ejemplo n.º 14
0
        private ComposablePartCatalog GetInterceptionCatalog()
        {
            _applicationCatalog = new ApplicationCatalog();
            _adapterdirectoryCatalog = new DirectoryCatalog(AdaptersDirectory);
            _pluginsdirectoryCatalog = new DirectoryCatalog(PluginsDirectory);

            var catalog = new AggregateCatalog(_applicationCatalog, _adapterdirectoryCatalog, _pluginsdirectoryCatalog);

            var cfg = new InterceptionConfiguration().AddInterceptionCriteria(
                            new PredicateInterceptionCriteria(
                                new CopyConfigInterceptor(_robot.Settings),
                                def => def.ExportDefinitions.First().ContractName.Contains("IAdapter") ||
                                       def.ExportDefinitions.First().ContractName.Contains("IRobotPlugin")));

            return new InterceptingCatalog(catalog, cfg);
        }
Ejemplo n.º 15
0
        private static CompositionContainer CreateContainer1()
        {
            // Create source catalog, note we are passing the registry part.
            // During the runtime, GenericExportHandler will query this catalog for
            // all types implementing IGenericContractRegistry
            var typeCatalog = new TypeCatalog(typeof(Trampoline), typeof(MyGenericContractRegistry));

            // Create the interception configuration and add support for open generics
            var cfg = new InterceptionConfiguration()
                .AddHandler(new GenericExportHandler());

            // Create the InterceptingCatalog and pass the configuration
            var interceptingCatalog = new InterceptingCatalog(typeCatalog, cfg);

            // Create the container
            return new CompositionContainer(interceptingCatalog);
        }
Ejemplo n.º 16
0
        public void Run()
        {
            Console.WriteLine("\n*** Interception Demo ***");

            // Create source catalog
            var catalog = new TypeCatalog(typeof(Bar), typeof(Foo));

            // Create interception configuration
            var cfg = new InterceptionConfiguration()

                // Add catalog wide startable interceptor
                .AddInterceptor(new StartableStrategy())

                /*
                .AddInterceptionCriteria(
                    new LogInterceptionCriteria(
                        new DynamicProxyInterceptor(
                            new LoggingInterceptor())))
                */

                // Add Castle DynamicProxy based logging interceptor for parts
                // which want to be logged, does exactly the same as the above code
                .AddInterceptionCriteria(
                    new PredicateInterceptionCriteria(

                        // Apply the interceptor only to parts which contain
                        // Log export metadata which equals to true
                        new DynamicProxyInterceptor(new LoggingInterceptor()), def =>
                            def.ExportDefinitions.First().Metadata.ContainsKey("Log") &&
                            def.ExportDefinitions.First().Metadata["Log"].Equals(true)));

            // Create the InterceptingCatalog with above configuration
            var interceptingCatalog = new InterceptingCatalog(catalog, cfg);

            // Create the container
            var container = new CompositionContainer(interceptingCatalog);

            // Bar part will be intercepted only by the startable strategy
            var barPart = container.GetExportedValue<IBar>();
            barPart.Foo();

            // Foo part will be intercepted by both startable and logging strategies
            var fooPart = container.GetExportedValue<IFoo>();
            fooPart.Bar();
        }
Ejemplo n.º 17
0
        public void When_adding_new_part_to_the_intercepted_catalog_intercepting_catalog_raises_recomposition_events()
        {
            var innerCatalog = new TypeCatalog(typeof(RecomposablePart1));
            var cfg = new InterceptionConfiguration().AddInterceptor(new RecomposablePartInterceptor());
            var aggregateCatalog = new AggregateCatalog(innerCatalog);
            var catalog = new InterceptingCatalog(aggregateCatalog, cfg);
            container = new CompositionContainer(catalog);
            
            uint changingEventInvokeCount = 0;
            uint changedEventInvokeCount = 0;

            catalog.Changing += (s, e) => changingEventInvokeCount++;
            catalog.Changed += (s, e) => changedEventInvokeCount++;
            
            // Recompose
            aggregateCatalog.Catalogs.Add(new TypeCatalog(typeof(RecomposablePart2)));

            Assert.That(changingEventInvokeCount, Is.EqualTo(1));
            Assert.That(changedEventInvokeCount, Is.EqualTo(1));
        }
        public void Disposing_catalog_should_dispose_parts_implementing_dispose_pattern()
        {
            var innerCatalog = new TypeCatalog(typeof(DisposablePart));
            var cfg = new InterceptionConfiguration();
            var catalog = new InterceptingCatalog(innerCatalog, cfg);
            container = new CompositionContainer(catalog);

            var part = container.GetExportedValueOrDefault<DisposablePart>();
            Assert.That(part.IsDisposed, Is.False);
            container.Dispose();
            Assert.That(part.IsDisposed, Is.True);
        }
Ejemplo n.º 19
0
        public Request(ComposablePartCatalog parentCatalog, CompositionContainer parentContainer)
        {
            Console.WriteLine("/* Request */");

            // Create interception configuration with non-shared parts filter
            var cfg = new InterceptionConfiguration()
                .AddHandler(new PartCreationPolicyFilter(CreationPolicy.NonShared));

            // Create the InterceptingCatalog with above configuration
            var interceptingCatalog = new InterceptingCatalog(parentCatalog, cfg);

            // Create the child container
            this.requestContainer = new CompositionContainer(interceptingCatalog, parentContainer);
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GenericCatalog"/> class.
 /// </summary>
 /// <param name="catalog"><see cref="ComposablePartCatalog"/> from which <see cref="IGenericContractRegistry"/> instances will be retrieved.</param>
 /// <param name="registries">Additional registries.</param>
 public GenericCatalog(ComposablePartCatalog catalog, params IGenericContractRegistry[] registries)
 {
     var cfg = new InterceptionConfiguration()
         .AddHandler(new GenericExportHandler(registries));
     this.interceptingCatalog = new InterceptingCatalog(catalog, cfg);
 }
Ejemplo n.º 21
0
        public void When_removing_existing_part_from_the_part_handler_intercepting_catalog_is_recomposed_and_removes_that_part()
        {
            var partHandler = new RecomposablePartHandler();
            var part2Catalog = new TypeCatalog(typeof (RecomposablePart2));
            var innerCatalog = new TypeCatalog(typeof(RecomposablePartImporter), typeof(RecomposablePart1));
            var aggregateCatalog = new AggregateCatalog(innerCatalog, part2Catalog);
            var cfg = new InterceptionConfiguration()
                .AddInterceptor(new RecomposablePartInterceptor())
                .AddHandler(partHandler);
            var catalog = new InterceptingCatalog(aggregateCatalog, cfg);
            container = new CompositionContainer(catalog);

            var importer = container.GetExportedValue<RecomposablePartImporter>();
            Assert.That(importer, Is.Not.Null);
            Assert.That(importer.Parts, Is.Not.Null);
            Assert.That(importer.Parts.Length, Is.EqualTo(2));
            Assert.That(importer.Parts[0].Count, Is.EqualTo(1));
            Assert.That(importer.Parts[1].Count, Is.EqualTo(1));
            Assert.That(catalog.Parts.Count(), Is.EqualTo(3));
            Assert.That(catalog.Parts.OfType<InterceptingComposablePartDefinition>().Count(), Is.EqualTo(3));

            // Recompose
            partHandler.RemoveParts(part2Catalog);

            Assert.That(importer, Is.Not.Null);
            Assert.That(importer.Parts, Is.Not.Null);
            Assert.That(importer.Parts.Length, Is.EqualTo(1));
            Assert.That(importer.Parts[0].Count, Is.EqualTo(1));
            Assert.That(importer.Parts[0].GetType(), Is.EqualTo(typeof(RecomposablePart1)));
            Assert.That(catalog.Parts.Count(), Is.EqualTo(2));
            Assert.That(catalog.Parts.OfType<InterceptingComposablePartDefinition>().Count(), Is.EqualTo(2));
        }
        public void Catalog_should_call_Initialize_on_a_given_part_handlers()
        {
            var innerCatalog = new TypeCatalog();
            var partHandlerMock = new Mock<IPartHandler>(MockBehavior.Strict);
            partHandlerMock.Setup(p => p.Initialize(innerCatalog)).Verifiable();

            var cfg = new InterceptionConfiguration()
                .AddHandler(partHandlerMock.Object);
            var catalog = new InterceptingCatalog(innerCatalog, cfg);
            Assert.That(catalog, Is.Not.Null);
            partHandlerMock.Verify();
        }