private static void RegisterUpcasters(Type type)
 {
     if (!type.IsAbstract && typeof(IUpcaster).IsAssignableFrom(type))
     {
         var realUpcaster = (IUpcaster)type.CreateInstance();
         StaticUpcaster.RegisterUpcaster(realUpcaster);
     }
 }
        public void Simple_Upcast_of_single_event()
        {
            StaticUpcaster.Clear();
            StaticUpcaster.RegisterUpcaster(new UpcastedEvent.Upcaster());
            var evt      = new UpcastedEvent("Hello world");
            var upcasted = StaticUpcaster.UpcastEvent(evt);

            Assert.That(upcasted, Is.InstanceOf <NewEvent>());

            var upcastedEvent = (NewEvent)upcasted;

            Assert.That(upcastedEvent.RenamedProperty, Is.EqualTo("Hello world"));
        }
        public void Chain_of_upcast()
        {
            StaticUpcaster.Clear();
            StaticUpcaster.RegisterUpcaster(new UpcastedEvent.Upcaster());
            StaticUpcaster.RegisterUpcaster(new ReallyOldEvent.Upcaster());

            var evt      = new ReallyOldEvent(42);
            var upcasted = StaticUpcaster.UpcastEvent(evt);

            Assert.That(upcasted, Is.InstanceOf <NewEvent>());

            var upcastedEvent = (NewEvent)upcasted;

            Assert.That(upcastedEvent.RenamedProperty, Is.EqualTo("Property 42"));
        }
        /// <summary>
        /// Enchance and upcast the event.
        /// </summary>
        public void EnhanceAndUpcastEvent()
        {
            if (_enhanced == false && Event is DomainEvent)
            {
                _enhanced = true;
                var domainEvent = Event as DomainEvent;

                domainEvent.CommitId        = CommitId;
                domainEvent.Version         = Version;
                domainEvent.Context         = Context;
                domainEvent.CheckpointToken = CheckpointToken;

                Event = StaticUpcaster.UpcastEvent(domainEvent);
            }
        }
        public void Simple_Upcast_of_changeset()
        {
            StaticUpcaster.Clear();
            StaticUpcaster.RegisterUpcaster(new UpcastedEvent.Upcaster());
            var cs       = new Changeset(1, new object[] { new UpcastedEvent("Hello world") });
            var upcasted = StaticUpcaster.UpcastChangeset(cs);

            Assert.That(object.ReferenceEquals(upcasted, cs));
            Assert.That(upcasted, Is.InstanceOf <Changeset>());
            var upcastedCs = (Changeset)upcasted;

            Assert.That(upcastedCs.Events.Single(), Is.InstanceOf <NewEvent>());

            var upcastedEvent = (NewEvent)upcastedCs.Events.Single();

            Assert.That(upcastedEvent.RenamedProperty, Is.EqualTo("Hello world"));
        }
        public void Upcast_should_be_resilient_to_non_changeset_object()
        {
            var upcasted = StaticUpcaster.UpcastChangeset("Hello world");

            Assert.That(upcasted, Is.EqualTo("Hello world"));
        }
        public void Upcast_should_be_resilient_to_null()
        {
            var upcasted = StaticUpcaster.UpcastChangeset(null);

            Assert.IsNull(upcasted);
        }
 public void Cannot_Register_two_distinct_upcasters()
 {
     StaticUpcaster.Clear();
     StaticUpcaster.RegisterUpcaster(new UpcastedEvent.Upcaster());
     Assert.Throws <ArgumentException>(() => StaticUpcaster.RegisterUpcaster(new UpcastedEvent.Upcaster()));
 }