public void CreateOrReplaceCollection_OriginalModelMutable_UpdatesOriginalInstance() { // Arrange List <int> originalInstance = new List <int> { 10, 20, 30 }; ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType( () => originalInstance, typeof(ICollection <int>) ) }; // Act CollectionModelBinderUtil.CreateOrReplaceCollection( bindingContext, new[] { 40, 50, 60 }, () => new List <int>() ); // Assert Assert.Same(originalInstance, bindingContext.Model); Assert.Equal(new[] { 40, 50, 60 }, originalInstance.ToArray()); }
// Extensibility point that allows the bound collection to be manipulated or transformed before // being returned from the binder. protected virtual bool CreateOrReplaceCollection( HttpActionContext actionContext, ModelBindingContext bindingContext, IList <TElement> newCollection ) { CollectionModelBinderUtil.CreateOrReplaceCollection( bindingContext, newCollection, () => new List <TElement>() ); return(true); }
public void CreateOrReplaceCollection_OriginalModelNotCollection_CreatesNewInstance() { // Arrange ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(ICollection <int>)) }; // Act CollectionModelBinderUtil.CreateOrReplaceCollection(bindingContext, new[] { 10, 20, 30 }, () => new List <int>()); // Assert int[] newModel = (bindingContext.Model as ICollection <int>).ToArray(); Assert.Equal(new[] { 10, 20, 30 }, newModel); }
public void CreateOrReplaceCollection_OriginalModelMutable_UpdatesOriginalInstance() { // Arrange List <int> originalInstance = new List <int>() { 10, 20, 30 }; ExtensibleModelBindingContext bindingContext = new ExtensibleModelBindingContext() { ModelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(() => originalInstance, typeof(ICollection <int>)) }; // Act CollectionModelBinderUtil.CreateOrReplaceCollection <int>(bindingContext, new int[] { 40, 50, 60 }, () => new List <int>()); // Assert Assert.AreSame(originalInstance, bindingContext.Model, "Original collection should have been updated."); CollectionAssert.AreEqual(new int[] { 40, 50, 60 }, originalInstance); }