Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always write to and read from backing fields by default #12430

Closed
ajcvickers opened this issue Jun 20, 2018 · 13 comments
Closed

Always write to and read from backing fields by default #12430

ajcvickers opened this issue Jun 20, 2018 · 13 comments
Assignees
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

However, also consider notification entities where setting the property may be needed to trigger the notification.

@sjb-sjb
Copy link

sjb-sjb commented Oct 14, 2018

The advantage to backing fields cited in https://docs.microsoft.com/en-us/ef/core/modeling/backing-field is that one can then have private property setters.

The programmer's decision to have property setters used for materialization (or, for example, for reversion when State is set to Unchanged) is likely based on very different considerations than the decision of whether a given property should be public or private. Therefore, it would be very useful to be able to have EF use the property setter even though it is private. This would not be a breaking change.

@ajcvickers
Copy link
Member Author

@sjb-sjb EF Core has always been able to map private (protected, internal, etc.) properties. I'm curious what the text is in that link that made you think otherwise.

@sjb-sjb
Copy link

sjb-sjb commented Oct 15, 2018 via email

@ajcvickers
Copy link
Member Author

@sjb-sjb EF maps to, writes to, and reads from private getters and setters, as well as fields, depending on our the property is configured.

@sjb-sjb
Copy link

sjb-sjb commented Oct 15, 2018 via email

@sjb-sjb
Copy link

sjb-sjb commented Oct 27, 2018

@ajcvickers responding to your question "I'm curious what the text is in that link that made you think otherwise", I was taken in by the sentence that says "If the property is read-only, then it will write to the field."

I read this a bit too quickly perhaps and I thought that 'read-only' referred to the perspective from outside the class, i.e. public getter, private setter would mean read-only. I suppose it meant no setter at all.

@ajcvickers
Copy link
Member Author

@sjb-sjb Thanks for the feedback.

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 2, 2019
ajcvickers added a commit that referenced this issue Jan 3, 2019
ajcvickers added a commit that referenced this issue Jan 4, 2019
ajcvickers added a commit that referenced this issue Jan 4, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview2 Feb 6, 2019
@ajcvickers
Copy link
Member Author

Re-opening to ensure breaking change is announced.

@ajcvickers ajcvickers reopened this Feb 6, 2019
@ajcvickers ajcvickers changed the title Consider always write to backing fields by default Always write to and read from backing fields by default Mar 1, 2019
@vanillajonathan
Copy link
Contributor

With this you will be able to expose a navigational property as a IReadOnlyCollection.

Example:

public class Blog
{
    private List<Post> _posts = new List<Post>();

    public int BlogId { get; set; }
    public string Url { get; set; }

    public IReadOnlyCollection<Post> Posts => _posts.AsReadOnly();
}

@ajcvickers
Copy link
Member Author

@vanillajonathan That is already possible. This change means that it should work without additional configuration to tell EF to map to the field.

@ajcvickers
Copy link
Member Author

@sjb-sjb But it's not read-only...

@bigworld12
Copy link

how does this stack with #2968 ? if you force all read/writes to fields, how are you going to implement custom getters/setters for properties ?

@sjb-sjb
Copy link

sjb-sjb commented May 26, 2019

@bigworld12, for my purposes the point is that EF will use direct field access during materialization while my application code will use the setters/getters. I want different things to happen in the two cases.

@ajcvickers ajcvickers modified the milestones: 3.0.0-preview2, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants