Skip to content

Commit

Permalink
CollectionPropertyBinder checks for existing collection before it cre…
Browse files Browse the repository at this point in the history
…ates a new. Closes FubuMVC issue 196.

DarthFubuMVC/fubumvc#196
  • Loading branch information
bardurdam committed Dec 12, 2011
1 parent 2466edd commit ea1ad8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/FubuCore.Testing/Binding/CollectionPropertyBinderTester.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using FubuCore.Binding;
using FubuCore.Reflection;
using FubuTestingSupport;
Expand Down Expand Up @@ -50,5 +51,26 @@ public void set_a_property_correctly_against_a_binding_context()

model.Localities[0].ZipCode.ShouldEqual("84115");
}

[Test]
public void existing_collection_is_not_discarded()
{
var model = new AddressViewModel
{
Localities = new List<LocalityViewModel>
{
new LocalityViewModel {ZipCode = "previously_set_zipcode"}
}
};

context.WithData("Localities[0]ZipCode", "84115");
context.StartObject(model);

var property = ReflectionHelper.GetProperty<AddressViewModel>(x => x.Localities);
propertyBinder.Bind(property, context);

model.Localities[0].ZipCode.ShouldEqual("previously_set_zipcode");
model.Localities[1].ZipCode.ShouldEqual("84115");
}
}
}
3 changes: 2 additions & 1 deletion src/FubuCore/Binding/CollectionPropertyBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void Bind(PropertyInfo property, IBindingContext context)
type = _collectionTypeProvider.GetCollectionType(type, itemType);
}

object collection = Activator.CreateInstance(type);
var currentCollection = property.GetValue(context.Object, null);
object collection = currentCollection ?? Activator.CreateInstance(type);
var collectionType = collection.GetType();

Func<object, bool> addToCollection = obj =>
Expand Down

0 comments on commit ea1ad8f

Please sign in to comment.