Пример #1
0
        public void Type_information_should_match_initialized_type()
        {
            var type = typeof (object);
            var bag = new PropertyBag(type);

            bag.AssemblyName.Should().Be(type.Assembly.FullName);
            bag.AssemblyQualfiedName.Should().Be(type.AssemblyQualifiedName);
            bag.Namespace.Should().Be(type.Namespace);
            bag.TypeName.Should().Be(type.Name);
        }
Пример #2
0
        public void Calling_AddPropertyValue_should_add_the_property_info()
        {
            var thePropName = "MyProperty";
            var thePropValue = "Hello world";

            var bag = new PropertyBag("testing");
            bag.AddPropertyValue(thePropName, thePropValue);

            bag.Properties.Should().Contain(p => p.Key == thePropName && (String)p.Value == thePropValue);
        }
Пример #3
0
        /// <summary>
        /// Converts a command or event into a property bag.
        /// </summary>
        public PropertyBag Convert(object obj)
        {
            Type type = obj.GetType();
            var document = new PropertyBag(type);

            foreach (PropertyInfo propertyInfo in type.GetProperties(PublicInstanceProperties))
            {
                document.AddPropertyValue(propertyInfo.Name, propertyInfo.GetValue(obj, null));
            }

            return document;
        }
Пример #4
0
        public void Adding_the_same_property_twice_should_cause_exception()
        {
            var propertyName = "MyProperty";
            var firstValue = 1;
            var secondValue = 2;

            var bag = new PropertyBag("testing");
            bag.AddPropertyValue(propertyName, firstValue);

            Action act = ()=>bag.AddPropertyValue(propertyName, secondValue);
            act.ShouldThrow<ArgumentException>().WithMessage("An item with the same key has already been added.");
        }
Пример #5
0
        public void Adding_the_same_property_twice_should_cause_exception()
        {
            var propertyName = "MyProperty";
            var firstValue = 1;
            var secondValue = 2;

            var bag = new PropertyBag("testing");
            bag.AddPropertyValue(propertyName, firstValue);

            Action act = ()=>bag.AddPropertyValue(propertyName, secondValue);
            act.ShouldThrow<ArgumentException>();
        }
Пример #6
0
        public void The_number_of_properties_should_be_equal_to_the_number_of_calls_to_AddPropertyValue()
        {
            var callCount = 5;
            var bag = new PropertyBag("testing");

            for(int i =0; i<callCount; i++)
            {
                var name = "prop" + i;
                var value = i;

                bag.AddPropertyValue(name, value);
            }

            bag.Properties.Count.Should().Be(callCount);
        }
Пример #7
0
        /// <summary>
        /// Converts a command or event stored as a property bag back to its object representation.
        /// </summary>
        /// <remarks>
        /// If a post conversion was registered using <see cref="AddPostConversion"/>, it will be invoked after
        /// the default conversion has completed. Moreover, the actual type created can be overridden by
        /// providing a custom <see cref="IPropertyBagTypeResolver"/> through the <see cref="TypeResolver"/> property.
        /// </remarks>
        public object Convert(PropertyBag propertyBag)
        {
            Type targetType = GetDestinationType(propertyBag);
            object instance = CreateInstanceOfType(targetType);

            bool allPropertiesInitialized = InitializeInstancePropertiesFrom(propertyBag, instance);
            bool executedPostConversion = InvokePostConverter(instance, propertyBag);

            if (!allPropertiesInitialized && !executedPostConversion)
            {
                throw new SerializationException(
                    "Not all properties of " + propertyBag.Namespace + " could be deserialized");
            }

            return instance;
        }
Пример #8
0
        public void Restoration_of_an_event_from_a_property_bag_containing_nulls_should_not_fail()
        {
            try
            {
                var converter = new PropertyBagConverter { TypeResolver = new SimpleEventTypeResolver() };

                var bag = new PropertyBag(typeof(TestEvent).AssemblyQualifiedName);
                bag.AddPropertyValue("SomeString", null);

                var obj = converter.Convert(bag);

                obj.Should().NotBeNull();
                obj.Should().BeOfType<TestEvent>();

                ((TestEvent) obj).SomeString.Should().BeNull();
            }
            catch(Exception e)
            {
                Assert.Fail(e.ToString());
            }
        }
Пример #9
0
        private static bool InitializeInstancePropertiesFrom(PropertyBag bag, object target)
        {
            bool completelyInitialized = true;

            foreach (KeyValuePair<string, object> pair in bag.Properties)
            {
                string propertyName = pair.Key;
                object propertyValue = pair.Value;

                PropertyInfo targetProperty = target.GetType().GetProperty(propertyName);
                if (IsPropertyWritable(targetProperty))
                {
                    completelyInitialized &= SetPropertyValue(target, targetProperty, propertyValue);
                }
            }

            return completelyInitialized;
        }
Пример #10
0
        private bool InvokePostConverter(object instance, PropertyBag bag)
        {
            Type instanceType = instance.GetType();
            if (_converters.ContainsKey(instanceType))
            {
                IPropertyBagPostConverter converter = _converters[instanceType];
                converter.ApplyConversion(instance, instanceType, bag.Properties);

                return true;
            }
            else
            {
                return false;
            }
        }
Пример #11
0
        private Type GetDestinationType(PropertyBag bag)
        {
            Type destinationType = null;

            if (TypeResolver != null)
            {
                destinationType = TypeResolver.Resolve(bag.TypeName, bag.Namespace, bag.AssemblyName);
            }

            if (destinationType == null)
            {
                destinationType = Type.GetType(bag.AssemblyQualfiedName);
            }

            return destinationType;
        }
Пример #12
0
 public void EventName_information_should_match_initialized_name()
 {
     var bag = new PropertyBag("testing");
     bag.EventName.Should().Be("testing");
 }
Пример #13
0
 public void An_new_instance_should_not_contain_any_properties()
 {
     var bag = new PropertyBag("testing");
     bag.Properties.Count.Should().Be(0);
 }
Пример #14
0
 public void An_new_instance_should_not_contain_any_properties()
 {
     var bag = new PropertyBag(typeof (object));
     bag.Properties.Count.Should().Be(0);
 }
 private Type GetDestinationType(PropertyBag bag)
 {
     Type destinationType = TypeResolver.ResolveType(bag.EventName);
     return destinationType;
 }