<commit_before>﻿using System.Collections.Generic;
using System.Dynamic;
using ClassLibrary1.Domain;
using ClassLibrary1.Events;
using ClassLibrary1.Framework;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Xunit;

namespace ClassLibrary1
{
    public class Tests
    {

        [Fact]
        public void SimpleTest()
        {
            var jsonCreate = "{ \"Name\": \"Max\", \"Address\": \"Berlin\" }";
            var jsonAddressChanged = "{ \"Address\": \"New York\" }";

            // this is my EventStore
            var dbObjects = new List<DbObject>
            {
                new DbObject {Type = typeof (CustomerCreated).Name, Payload = jsonCreate},
                new DbObject {Type = typeof (CustomerAddressChanged).Namespace, Payload = jsonAddressChanged}
            };


            foreach (var dbObject in dbObjects) {
                var payloadExpando = JsonConvert.DeserializeObject<ExpandoObject>(dbObject.Payload, new ExpandoObjectConverter());
                // Ok, the payload is an expando-object now...
                //
                // dbObject has the type as string.
                //
                // How do I convert the payload/expando object to the correct type?
                

                var domainEvents = new List<DomainEvent>();


                var customer = new Customer();
                foreach (var domainEvent in domainEvents) {
                    customer.Apply(domainEvent);
                }

                customer.Name.Should().Be("Max");
                customer.Address.Should().Be("New York");
            }
        }

        public class DbObject
        {
            public string Type { get; set; }
            public string Payload { get; set; }
        }

        // Just an idea
        public static class MyConverter
        {
            public static T Convert<T>(dynamic source) where T : class, new() // or DomainEvent
            {
                T t = new T();
                Update(source, t);
                return t;
            }

            private static void Update<T>(dynamic source, T o) where T : class
            {

            }
        }
    }
}<commit_msg>broken<commit_after>﻿using System.Collections.Generic;
using System.Dynamic;
using ClassLibrary1.Domain;
using ClassLibrary1.Events;
using ClassLibrary1.Framework;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Xunit;

namespace ClassLibrary1
{
    public class Tests
    {

        [Fact]
        public void SimpleTest()
        {
            var jsonCreate = "{ \"Name\": \"Max\", \"Address\": \"Berlin\" }";
            var jsonAddressChanged = "{ \"Address\": \"New York\" }";

            // this is my EventStore
            var dbObjects = new List<DbObject>
            {
                new DbObject {Type = typeof (CustomerCreated).Name, Payload = jsonCreate},
                new DbObject {Type = typeof (CustomerAddressChanged).Namespace, Payload = jsonAddressChanged}
            };


            foreach (var dbObject in dbObjects) {
                var payloadExpando = JsonConvert.DeserializeObject<ExpandoObject>(dbObject.Payload, new ExpandoObjectConverter());
                // Ok, the payload is an expando-object now...
                //
                // dbObject has the type as string.
                //
                // How do I convert the payload/expando object to the correct type?

                var typeAsString = dbObject.Type; // No idea how to use this
                DomainEvent evt = MyConverter.Convert<I_Have_No_Idea_Which_Event>(payloadExpando);

                var domainEvents = new List<DomainEvent>();


                var customer = new Customer();
                foreach (var domainEvent in domainEvents) {
                    customer.Apply(domainEvent);
                }

                customer.Name.Should().Be("Max");
                customer.Address.Should().Be("New York");
            }
        }

        public class DbObject
        {
            public string Type { get; set; }
            public string Payload { get; set; }
        }

        // Just an idea
        public static class MyConverter
        {
            public static T Convert<T>(dynamic source) where T : DomainEvent, new() // or DomainEvent
            {
                T t = new T();
                Update(source, t);
                return t;
            }

            private static void Update<T>(dynamic source, T o) where T : class
            {

            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09469779620336025, 'fraction_numerical': 0.0017455814968361336, 'mean_word_length': 1.563758389261745, 'pattern_counts': {'":': 6, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1454203', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1082, 'n_words': 332}