Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Handle broader range of collection types in model binding #2946

Closed
wants to merge 2 commits into from

Conversation

dougbu
Copy link
Member

@dougbu dougbu commented Aug 10, 2015

  • Concrete Collection types don't work with GenericModelBinder #2793
  • add ICollectionModelBinder, allowing GenericModelBinder to call CreateEmptyCollection()
  • adjust CollectionModelBinder and DictionaryModelBinder to activate model if default types are incompatible
    • do not create default (empty) top-level collection in fallback case if Model already non-null
  • change type checks in GenericModelBinder to align with CollectionModelBinder capabilities
    • add special case for IEnumerable<T>
  • correct ModelMetadata of a few tests that previously did not need that information

@dougbu
Copy link
Member Author

dougbu commented Aug 10, 2015

/cc @rynowak

{
return newCollection?.ToArray();
return collection?.ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the default case here be that the model is already an array? What are the cases we might get here and have it not be an array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll never be an array since the base CollectionModelBinder uses a List<TElement> until it needs to copy everything into the final model. The value should not be null but this was written defensively originally...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Can you add that info to a comment here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@rynowak
Copy link
Member

rynowak commented Aug 10, 2015

⌚ overall looks good, tests look good.

dougbu added 2 commits August 10, 2015 16:13
- #2793
- add `ICollectionModelBinder`, allowing `GenericModelBinder` to call `CreateEmptyCollection()`
- adjust `CollectionModelBinder` and `DictionaryModelBinder` to activate model if default types are incompatible
 - do not create default (empty) top-level collection in fallback case if Model already non-`null`
- change type checks in `GenericModelBinder` to align with `CollectionModelBinder` capabilities
 - add special case for `IEnumerable<T>`
- correct `ModelMetadata` of a few tests that previously did not need that information
- did stuff
@dougbu dougbu force-pushed the dougbu/concrete.collections.2793 branch from 2033030 to 4311c87 Compare August 11, 2015 06:04
@dougbu
Copy link
Member Author

dougbu commented Aug 11, 2015

Updated

{
Debug.Assert(targetType == typeof(TElement[]), "GenericModelBinder only creates this binder for arrays.");

return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just do the thing you put in the assert? You could say with like 20 characters what all of this does, and technically be more correct 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@rynowak
Copy link
Member

rynowak commented Aug 11, 2015

:shipit:

@dougbu
Copy link
Member Author

dougbu commented Aug 11, 2015

d45e2ee

@dougbu dougbu closed this Aug 11, 2015
@dougbu dougbu deleted the dougbu/concrete.collections.2793 branch August 11, 2015 15:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants