public void CreateTypeResolverReturnsCorrectResult()
        {
            var assemblyToScanForEvents =
                typeof(DataContractContentSerializerTests).Assembly;
            var mappings =
                (from t in assemblyToScanForEvents.GetExportedTypes()
                 from a in t.GetCustomAttributes(
                     typeof(DataContractAttribute), inherit: false)
                 .Cast <DataContractAttribute>()
                 where t.IsDefined(a.GetType(), inherit: false)
                 select new TypeResolutionEntry(a.Namespace, a.Name, t))
                .ToArray();

            Assert.NotEmpty(mappings);
            var sut =
                DataContractContentSerializer.CreateTypeResolver(
                    assemblyToScanForEvents);

            Array.ForEach(mappings, entry =>
            {
                var expected = entry.ResolvedType;
                var actual   = sut.Resolve(entry.LocalName, entry.XmlNamespace);
                Assert.Equal(expected, actual);
            });
        }
 static FeedController()
 {
     s_storage    = new AtomEventsInFiles(new DirectoryInfo(Globals.StoragePath));
     s_serializer = new DataContractContentSerializer(
         DataContractContentSerializer
         .CreateTypeResolver(typeof(ProductEntry).Assembly)
         );
 }
        public ProductAddedEventHandler(ILog logger) : base(logger)
        {
            var storage    = new AtomEventsInFiles(new DirectoryInfo(Globals.StoragePath));
            var serializer = new DataContractContentSerializer(
                DataContractContentSerializer
                .CreateTypeResolver(typeof(ProductEntry).Assembly)
                );

            _observer = new AtomEventObserver <ProductEntry>(
                Globals.EventStreamId,
                25,
                storage,
                serializer
                );
        }
        public void CreateTypeResolverWithAssemblyWithoutAnnotatedTypesThrows()
        {
            var assembly = typeof(Version).Assembly;

            Assert.Empty(
                from t in assembly.GetExportedTypes()
                from a in t.GetCustomAttributes(
                    typeof(DataContractAttribute), inherit: false)
                .Cast <DataContractAttribute>()
                where t.IsDefined(a.GetType(), inherit: false)
                select t);

            Assert.Throws <ArgumentException>(() =>
                                              DataContractContentSerializer.CreateTypeResolver(assembly));
        }
Ejemplo n.º 5
0
        public void WriteASingleEventSynchronously()
        {
            var eventStreamId =
                new Guid("A0E50259-7345-48F9-84B4-BEEB5CEC662C");

            /* Uses file storage to provide a documentation example of how to
             * do that. */
            var directory =
                new DirectoryInfo(
                    Path.Combine(
                        Environment.CurrentDirectory,
                        Guid.NewGuid().ToString("N")));

            try
            {
                var storage  = new AtomEventsInFiles(directory);
                var pageSize = 25;

                /* This is an example of using
                 * DataContractContentSerializer.CreateTypeResolver, so it
                 * should not be refactored to one of the terser
                 * alternatives. */
                var resolver =
                    DataContractContentSerializer.CreateTypeResolver(
                        typeof(UserCreated).Assembly);
                var serializer             = new DataContractContentSerializer(resolver);
                IObserver <IUserEvent> obs = new AtomEventObserver <IUserEvent>(
                    eventStreamId, // a Guid
                    pageSize,      // an Int32
                    storage,       // an IAtomEventStorage object
                    serializer);   // an IContentSerializer object

                var userCreated = new UserCreated
                {
                    UserId   = eventStreamId,
                    UserName = "******",
                    Password = "******",
                    Email    = "*****@*****.**"
                };
                obs.OnNext(userCreated);

                Assert.NotEmpty(storage);
            }
            finally
            {
                directory.Delete(recursive: true);
            }
        }
        public HttpResponseMessage Recent()
        {
            var feed = _events.ReadFirst();

            if (feed != null)
            {
                var sb = new StringBuilder();
                using (var xmlWriter = XmlWriter.Create(sb))
                {
                    feed.WriteTo(xmlWriter,
                                 new DataContractContentSerializer(
                                     DataContractContentSerializer.CreateTypeResolver(typeof(ProductEntry).Assembly)));
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(sb.ToString())
                    });
                }
            }

            return(new HttpResponseMessage(HttpStatusCode.NoContent));
        }
Ejemplo n.º 7
0
        public IEnumerable <ProductEntry> GetFeedEntries(Uri uri)
        {
            try
            {
                _logger.DebugFormat("Reading reference data from {0}", uri);
                var response = Client().GetAsync(uri).Result;
                response.EnsureSuccessStatusCode();

                var serializer = new DataContractContentSerializer(DataContractContentSerializer.CreateTypeResolver(typeof(ProductEntry).Assembly));
                var feed       = AtomFeed.Parse(response.Content.ReadAsStringAsync().Result, serializer);
                var reader     = new ReferenceDataFeedReader <ProductEntry>(_lastReadFeedItemDao, feed);
                return(reader);
            }
            catch (AggregateException ae)
            {
                foreach (var exception in ae.Flatten().InnerExceptions)
                {
                    _logger.InfoFormat("Threw exception getting feed from the Server {0}", uri, exception.Message);
                }

                throw new ApplicationException(string.Format("Error retrieving the feed from the server, see log for details"));
            }
        }
Ejemplo n.º 8
0
        public static void Run(UnityContainer container)
        {
            container.RegisterType <FeedController>();
            container.RegisterType <ProductsController>();
            container.RegisterInstance(typeof(ILog), LogProvider.For <ProductService>(), new ContainerControlledLifetimeManager());
            container.RegisterType <IProductsDAO, ProductsDAO>();
            container.RegisterType <AddProductCommandHandler>();
            container.RegisterType <ChangeProductCommandHandler>();
            container.RegisterType <ProductAddedEventHandler>();
            container.RegisterType <ProductChangedEventHandler>();
            container.RegisterType <ProductRemovedEventHandler>();
            container.RegisterType <RemoveProductCommandHandler>();

            var storage    = new AtomEventsInFiles(new DirectoryInfo(Globals.StoragePath));
            var serializer = new DataContractContentSerializer(
                DataContractContentSerializer
                .CreateTypeResolver(typeof(ProductEntry).Assembly)
                );

            var events = new FifoEvents <ProductEntry>(
                Globals.EventStreamId,
                storage,
                serializer);

            container.RegisterInstance(typeof(IObserver <ProductEntry>), events, new TransientLifetimeManager());

            var handlerFactory = new UnityHandlerFactory(container);

            var subscriberRegistry = new SubscriberRegistry
            {
                { typeof(AddProductCommand), typeof(AddProductCommandHandler) },
                { typeof(ChangeProductCommand), typeof(ChangeProductCommandHandler) },
                { typeof(RemoveProductCommand), typeof(RemoveProductCommandHandler) },
                { typeof(ProductAddedEvent), typeof(ProductAddedEventHandler) },
                { typeof(ProductRemovedEvent), typeof(ProductRemovedEventHandler) },
            };

            //create policies
            var retryPolicy = Policy
                              .Handle <Exception>()
                              .WaitAndRetry(new[]
            {
                TimeSpan.FromMilliseconds(50),
                TimeSpan.FromMilliseconds(100),
                TimeSpan.FromMilliseconds(150)
            });

            var circuitBreakerPolicy = Policy
                                       .Handle <Exception>()
                                       .CircuitBreaker(1, TimeSpan.FromMilliseconds(500));

            var policyRegistry = new PolicyRegistry()
            {
                { CommandProcessor.RETRYPOLICY, retryPolicy },
                { CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy }
            };


            var commandProcessor = CommandProcessorBuilder.With()
                                   .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory))
                                   .Policies(policyRegistry)
                                   .Logger(container.Resolve <ILog>())
                                   .NoTaskQueues()
                                   .RequestContextFactory(new InMemoryRequestContextFactory())
                                   .Build();

            container.RegisterInstance(typeof(IAmACommandProcessor), commandProcessor);
        }
 public void CreateTypeResolverWithNullAssemblyThrows()
 {
     Assert.Throws <ArgumentNullException>(() =>
                                           DataContractContentSerializer.CreateTypeResolver(null));
 }