Conditions and Operands
Understand condition combinations and database-side expressions from moderator review and page views increment.
Conditions determine which records are operable, and operand determines how fields are updated. Both should describe business constraints to avoid concurrent coverage caused by reading the value first and then calculating it in the application and then writing it back.
Condition Combination: Only Moderators Can Review
Source: src/Services/ThreadService.cs (excerpt; see source for context).
public bool Approve(ulong threadId)
{
var criteria = Condition.Equal(nameof(Models.Thread.ThreadId), threadId) &
Condition.Equal(nameof(Models.Thread.Approved), false) &
GetIsModeratorCriteria();
return this.DataAccess.Update<Models.Thread>(new
{
Approved = true,
ApprovedTime = DateTime.Now,
Post = new
{
Approved = true,
}
}, criteria, "*,Post{Approved}") > 0;
}The update runs only when all three conditions hold: the thread ID matches, the thread has not yet been approved, and the caller is a moderator. The data schema explicitly includes Post’s approval flag. Hiding a UI button is not sufficient; the service must enforce the action’s constraints.
Existence Condition: Moderator Record in the Forum
Source: src/Services/ThreadService.cs (excerpt; see source for context).
Exists takes the Forum.Users relationship as the scope, and internally matches both the user number and the moderator logo. The navigation name comes from mapping, not any SQL table name. The associated conditions together with the current SiteId form the complete business scope.
Operand: the Number of Views Is Incremented on the Database Side
Source: src/Services/ThreadService.cs (excerpt; see source for context).
Operand.Field represents the current field value of the database. The addition operation is a write expression, which can reduce the contention window of reading first and then writing. Subsequent memory objects also increment the count to make this response consistent with the update that has already been performed; it is not a second database write.
Tenant Conditions Cannot Be Replaced by the Caller
Source: src/Data/DataValidator.cs (excerpt; see source for context).
When a Discussions identity exists and the entity has a SiteId, the validator appends the current site condition. This constraint cannot be canceled even if the caller explicitly passes in other SiteIds. The authentication initialization path without the Discussions identity has different prerequisites, and it cannot be claimed that the authenticator alone covers all anonymous entrances.
Continue reading Query, write and Certification.
Last updated