-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Conversation
|
||
foreach (var propInfo in properties) | ||
{ | ||
if (propInfo.Name.StartsWith("Int")) |
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.
Add curly braces.
|
/// <param name="newValue">The value to assign in the specified object to this BindingPropertyInfo's property or field.</param> | ||
public void SetValue(object destinationObject, object newValue) | ||
{ | ||
if (_propertyInfo != 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.
Add curly braces please 😄
Maybe you should read the contributing guidelines and read some code around the codebase to get familiar with the existing style guidelines etc. 😄 |
Wow, super fast response! :-D I thought I'd seen some inconsistency here and there in the codebase, but you're right, I could have done a lot better at being consistent with adjacent files (though I was super careful not to mix spaces and tabs in existing files! :-) ). I'll fix it ASAP. Sorry about that! |
I've done a commit that makes the necessary changes to bring the files in line with the style guidelines, as well as requests on this discussion. I also applied the new method names I proposed in the thread about |
Wait, what? double take I swear I changed that file. Something must have changed it back before I committed. How embarrassing :-P |
Okay, sorry about that. Try now! |
/// <summary> | ||
/// Represents a bindable member of a type, which can be a property or a field. | ||
/// </summary> | ||
public class BindingPropertyInfo |
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 think this should also be renamed. I don't know what exactly maybe something along the lines of GenericValueBindingInfo
or GenericMemberInfo
.
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 think BindingMemberInfo
should be OK, even though it's not used on all types of members.
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.
Okay, for sure. Sorry, I only just saw these comments. I'll aim to have that committed within a day or two.
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 think that ought to do it. :-) Let me know if any other tweaks are required.
Okay, 'sall fixed up and good to go. :-) The build error was caused by a test that had been commented out at the time of my last commit. It contained a reference to one of the classes this pull request changes, and it got reinstated upstream since that last commit. All sorted out. Let me know if any further tweaks are needed. :-) |
Looks good to me |
Thanks for reviewing, @albertjan ❤️ @logiclrd I'd like to ask one last thing; could you manage to squash this into the first commit? There's a lot of "oh shit" commits in here right now 😉 I'd do it myself while merging, but I'm on vacation and can't do interactive rebases with my phone 😝 After that, I think this is ready to 🚢 |
Okay, I think I got it all figured out. I've semantically merged the commit messages (e.g. it now refers directly to You may merge at your leisure. :-) |
Other commits upstream have produced merge conflicts. I will rebase. |
Thanks Sent from my iPhone
|
Okay, all sorted out. A new property was added to the model in |
I have discovered that a unit test has been added upstream based on the old Should I apply a fix in this branch again? Many thanks :-) |
Do you mean apply the fix to the test upstream that you discover? Yes, I think it would be nice you did. |
Build and tests pass once again. :-) |
Woah. It seems you forgot to edit the commit message when you rebased. It would be great to have a nice, short, descriptive commit message before we merge this 😄 (nitpicking here 😉) |
...can't tell if you're being serious or not. :-) It's true that the commit message is unchange from the previous configuration of the branch (I deleted the message details from the follow-up commit in the squash), but only because the details of the follow-up commit are already covered by existing text in the commit message. :-) |
Hmm, okay :-) Well, the first line is nice, short and describes the entire change to a T, but I can condense the remainder of the text by removing all the actual details and leaving only the conceptual change. I am in the habit of writing commit messages like you see there so that they might be searchable in an index of commit messages, but if it's not appropriate for this project then I will change it. :-) |
How does this look? :-) |
Er, okay, not sure why that build failed. I only edited the commit message, I swear! :-P Looking at the details now... |
Okay, the first error I see in the build log is complaining that On my machine, in Visual Studio 2012, the project builds without issues. |
@@ -37,7 +37,7 @@ public class BindingContext | |||
/// <summary> | |||
/// DestinationType properties that are not black listed | |||
/// </summary> | |||
public IEnumerable<PropertyInfo> ValidModelProperties { get; set; } | |||
public IEnumerable<BindingMemberInfo> ValidModelProperties { get; set; } |
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.
Just for completeness, should this be called ValidModelMembers
since it's not only properties anymore?
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.
Can be done. :-)
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.
How about ValidModelBindingMembers
instead, since it's an IEnumerable<BindingMemberInfo>
, not an IEnumerable<MemberInfo>
?
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.
Doesn't matter to me, though it sounds like smurf naming... It's all in a binding context, I'm not sure we need to prefix everything with Binding* 😉
Replaced references to PropertyInfo in the binding code with a new class BindingMemberInfo, which can refer to either PropertyInfo or FieldInfo objects. Turned the reflection code into a utility method Collect() on the new class. Updated existing unit tests and added new ones.
Build passed :-) Did you see the previous one failing? I'm curious as to why, if you know. I came back today to find it magically transmuted to passing. :-) |
I restarted the build. It's just randomly failing. Just some Mono issues or something. |
Ah, cool. :-) |
Are there any remaining issues to resolve in this pull request? If not, is it on a timeline to be merged in? I've just verified that it still merges cleanly with all tests passing. :-) |
Model binding support for fields
Thanks @logiclrd! ❤️ |
Not at all, thank you! :-D |
I have updated the documentation of model binding to include a section on fields vs. properties. It indicates that Nancy supports both, gives an example of two classes that will both bind in the same manner, and includes a discussion of why one would choose one implementation style over the other. :-) |
In case anyone follows a link to this pull request from the "Breaking Changes" section of the documentation, the specific way in which this change is breaking is that in the past, fields within models would simply be ignored by Nancy's model binding. Thus, if you had a mix of properties and fields, assuming that the properties would receive bound values and the fields would be ignored, starting with the release including this pull request that is no longer the case. |
This patch is breaking https://github.com/NancyFx/Nancy.Serialization.JsonNet. Can someone have a look ? Should I create a new issue on either project ? |
@ClemPi It'd be a good idea to open up a new issue and add a description on how you can reproduce the error you're seeing. A stacktrace'd be nice too. |
Thanks, done here : NancyFx/Nancy.Serialization.JsonNet#15. |
This commit adds support to the
ModelBinding
code for binding fields, not just properties, in models. This allows the POD types for a project to look like:There is a good reason to write POD types this way, rather than using implicit property implementations. The last paragraph below explains it, though it is not strictly pertinent to this pull request.
This commit includes full unit testing of this change, including all of the necessary changes to existing tests.
Implicit property implementations create backing fields that include
<
and>
characters, in order to ensure that you cannot, in your own C# code, ever create another member with the same name. This, in turn, causes performance issues ifDataContractSerializer
is used on the same type.DataContractSerializer
works like a traditional serializer, enumerating the private members of the type. It then tries to create XML elements named after these members. If the members contain<
and>
characters, anXmlException
is produced. This exception can be seen by running code in a debugger set to break on all thrown exceptions, even if they're handled. TheDataContractSerializer
code is aware of this problem and handles the exception internally by doing additional escaping on the member name, but by then the performance hit of the exception has already been incurred. The only ways to avoid this exception are to either expand out the property definitions so that you control the name of the backing field, or to not use properties at all, and the latter solution is much more concise and has no downsides when the type really is just a POD type.# Breaking Change
The specific way in which this change is breaking is that in the past, fields within models would simply be ignored by Nancy's model binding. Thus, if you had a mix of properties and fields, assuming that the properties would receive bound values and the fields would be ignored, starting with the release including this pull request that is no longer the case.