concept Review in category entity framework

This is an excerpt from Manning's book Entity Framework Core in Action.
Table 13.4 The test data used for running the sorting and filtering tests
Table names -> Books Review BookAuthor Authors PriceOffers Number of rows 100,000 549,984 188,235 580 14,286
EF Core has a great feature called backing fields (see section 6.15), which allows you to better control access to an entity’s property. In this case, you want to control access to the
Reviewsnavigational property and, because it’s a collection, you want to remove the capability to add or remove instances from the collection. This you can do by using theIEnumerable<Review>type for yourReviewsproperty, with a backing field behind it. Figure 13.6 shows the various parts in theBookentity class and theReviewentity class, with theAddReviewmethod that does all the work.Figure 13.6 The
Bookentity class (top left) and theReviewentity class (top right) are “locked down.” The properties that the class doesn’t want anyone else to change have private setters, and theReviewsnavigational collection is of typeIEnumerable<T>to stop adding or removing reviews from the collection. TheAddReviewmethod (bottom) in theBookentity class is the only way a developer can add a newReview. When a newReviewis added, the method also recalculates the cache properties,AverageVotesandReviewsCount.![]()
Listing 13.7 The
Bookentity class focusing on the newReview-handling codepublic class Book { private readonly List<Review> _reviews = new List<Review>(); #1 //… other properties removed for clarity [ConcurrencyCheck] #2 public int ReviewsCount { get; private set; } #3 [ConcurrencyCheck] #2 public double? AverageVotes { get; private set; } #3 public IEnumerable<Review> Reviews => _reviews.ToList(); #4 //… other ctors and methods removed for clarity public void AddReview(DbContext context, #5 int numStars, string comment, string voterName) { context.Entry(this) .Collection(c => c.Reviews).Load(); #6 var review = new Review(numStars, comment, voterName); #7 _reviews.Add(review); #7 AverageVotes = _reviews .Average(x => x.NumStars); #8 ReviewsCount = _reviews.Count; #8 } public void RemoveReview(DbContext context, #9 Review review) #9 { context.Entry(this) #10 .Collection(c => c.Reviews).Load(); #10 _reviews.Remove(review); #11 AverageVotes = _reviews.Any() ? _reviews.Average(x => x.NumStars) #12 : (double?)null; #13 ReviewsCount = _reviews.Count; #14 } } #1 Adds a backing field, which is a list. You then tell EF Core to use this for all reads and writes. #2 Adds a [ConcurrencyCheck] attribute to this property #3 Holds a precalculated average of the reviews and the number of reviews for this book. It’s read-only so it can’t be changed outside this class. #4 Returns a copy of the reviews that were loaded. By taking a copy, no one can alter the list by casting IEnumerable<T> to List<T>. #5 Adds a method to allow a new review to be added to the _reviews collection #6 Makes sure the backing field, _reviews, has the reviews for this book loaded #7 Creates a review using the data given and then adds the new review to the backing field _reviews. This updates the database on the call to SaveChanges. #8 Recalculates the average votes and number of reviews for this book #9 Adds a method to remove a review from the _reviews collection #10 Makes sure the backing field, _reviews, has the reviews for this book loaded #11 Removes the review from the list. This updates the database on the call to SaveChanges #12 If there are reviews, you recalculate the average votes for the book #13 If there are no reviews, you set the value to null #14 Recalculates the number of reviews in this book