-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Adding support for ModelBinderAttribute in core. #1633
Conversation
5ee8068
to
89b319a
Compare
27b92e1
to
b01b057
Compare
would appreciate a high-level description of the change, preferably in the PR comment above. right now I see some refactoring, a new property or two, and lots of "custom" things. couldn't tell you what's being customized. |
@dougbu, I thought the code (with its detailed comments/summaries) was descriptive :) added a few comments in PR description. If this does not help lets sync offline. |
/// Represents an <see cref="IModelBinder"/> which understands <see cref="ICustomModelBinderMetadata"/> and uses | ||
/// the supplied <see cref="IModelBinder"/> bind the model. | ||
/// </summary> | ||
public class CustomModelBinder : MetadataAwareBinder<ICustomModelBinderMetadata> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm with @dougbu - not liking the naming. However this is all internal to the framework so it can be cleaned up whenever.
⌚ |
c22f3e7
to
91549e1
Compare
5dd3c34
to
a9f0661
Compare
@pranavkm updated |
@@ -1,17 +1,17 @@ | |||
<?xml version="1.0" encoding="utf-8"?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert changes to this file.
⌚ |
|
||
public async Task<bool> BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if (bindingContext.ModelMetadata.BinderType == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing this should this model binder derive from MetadataAwareModelBinder
and follow the pattern which is already there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check would still be necessary though, to support a no op ModelBinderAttribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of doing this should this model binder derive from MetadataAwareModelBinder and follow the pattern which is already there?
This should work one of two ways (convince me that I'm wrong):
- The binder type is not part of model metadata and is instead part of some
IBinderTypeBinderMetadata
interface. This class only looks for a binder metadata and looks at the property on it. - The binder type is part of model metadata. This class only looks at model metadata (my implementation)
The previous code in this PR mixed these two, and it's unnecessary coupling. It also breaks scenarios where a custom MMP sets a binder type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose 2. above because I think it results in the best layering based on the current design.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only problem that I see in the current code is IBinderTypeProviderMetadata
derives from IBinderMetadata
which means that we are mixing 1 and 2. In the previous implementation ( my implementation ) there were two separate interfaces one for the type provider and one which inherited for IBinderMetadata
.
Not sure I understand the reason for IBinderTypeProviderMetadata
implementing IBinderMetadata
when we are implementing option2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will remove IBinderMetadata
from it then.
Actually, this is needed too. The problem is that without this marker being an IBinderMetadata
than webapi shim will do wierd things - like override the behavior you set via the attribute.
This is just a bigger issue of having all of the binding concerns be based on ModelMetadata. It's the design we have for now, but it's not great. We should revisit in the future.
9aa9256
to
60ff84e
Compare
60ff84e
to
acb1710
Compare
@pranavkm updated please take a look. Enabled |
acb1710
to
9eb59f8
Compare
AttributeTargets.Parameter | | ||
|
||
// Support properties on model DTOs. | ||
AttributeTargets.Property | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yaay!
Looks good apart from comments. |
9eb59f8
to
b818cc0
Compare
b818cc0
to
e9bcc3f
Compare
@@ -36,6 +37,12 @@ public CachedDataAnnotationsMetadataAttributes(IEnumerable<object> attributes) | |||
} | |||
|
|||
/// <summary> | |||
/// Gets (or sets in subclasses) <see cref="IEnumerable{IBinderTypeProviderMetadata}"/> found in collection passed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
teensy nit; is this a long line?
⌚ needs another PR adding the appropriate tests for new properties etc. in |
This change introduces ModelBinderAttribute which can be used to specify a specific IModelBinder type or a specific IModelBinderProvider type. The attribute can also be used to provide a ModelNamePrefix. The attribute is allowed on types as well as parameters.
The "new" ModelBinderAttribute behaves in all respects like webapi except one.
SuppressPrefixCheck
property on the attribute in the current implementation. The rationale for this is as follows :SuppressPrefixCheck
is Only applicable for the following case ( no binderType specified on parameter but a binder type of ModelBinder on the type.What this results is before calling the
TestModelBinder
it will skip the check if there is a value provider which can provide a value.IMHO this is a entirely unnecessary. @yishaigalatzer please share your thoughts.