I have a need to save a computed property using a placeholder. The calculation is based on subsidiaries. I use root to add / remove children using domain methods, and these methods update the calculation property.
A child object can be added to a specific root by several users of the system. For example, UserA can add a child to Root123, and UserB can also add a child to Root123.
How can I make sure that this computed property is saved exactly when more than one user can add children to the same root in different transactions? In my particular case, the calculated property is used to ensure that a certain limit is not exceeded, as set by another property in the root.
Here is a more specific example of the problem:
public class RequestForProposal : AggregateRoot {
...
private ISet<Proposal> _proposals = new HashedSet<Proposal>();
public virtual int ProposalLimit { get; set; }
public virtual int ProposalCount { get; protected set; }
public virtual IEnumerable<Proposal> Proposals {
get { return _proposals; }
}
...
public virtual void AddProposal(User user, Content proposalContent) {
if (ProposalCount >= ProposalLimit) {
throw new ProposalLimitException("No more proposals are being accepted.");
}
var proposal = new Proposal(user, proposalContent);
_proposals.Add(proposal);
ProposalCount++;
}
public virtual void RemoveProposal(Proposal proposalToRemove) {
_proposals.Remove(proposalToRemove);
ProposalCount--;
}
}
What should I do if 2 users submit their offers at about the same time? The user interface sees that the limit has not yet been reached and displays a web page for submitting offers for both users. When the first user submits, everything is fine. Now the second user will be fine if the first user is sent before the second, so when the second user sends data, the data is retrieved from the database, and the limit will be exact.
? (ProposalLimit >= ProposalCount) , ?