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); }
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); }
/// <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; }
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."); }
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>(); }
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); }
/// <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; }
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()); } }
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; }
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; } }
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; }
public void EventName_information_should_match_initialized_name() { var bag = new PropertyBag("testing"); bag.EventName.Should().Be("testing"); }
public void An_new_instance_should_not_contain_any_properties() { var bag = new PropertyBag("testing"); bag.Properties.Count.Should().Be(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; }