private void InitializeDocument() { AchieversDoc result = GetDbDoc() ?? new AchieversDoc(); this.currentAchieversDoc = result; AchieversDoc GetDbDoc() { PeopleStatisticsDocumentRow achieversDocRow = this.rdbc.CurrentPeopleStatisticsDocuments .Where(sd => sd.DocumentName == AchieversDoc.Name) .FirstOrDefault(); if (achieversDocRow == null) { return(null); } var mongoObjectId = new ObjectId(achieversDocRow.DocumentId); return(this.mongoCollection.AsQueryable() .Where(ad => ad.Id == mongoObjectId) .FirstOrDefault()); } }
protected virtual void OnDebtDealReceived( object o_debtDealsRegister, DebtDealReceivedEventData evData ) { var newDoc = new AchieversDoc() { Id = ObjectId.GenerateNewId() }; this.SetIdsOfPeopleWithMaxTotalDueDebts(newDoc, evData); this.SetIdsOfPeopleWhoCreditedMaxHistoricalTotal(newDoc, evData); this.SetIdsOfBestDebtorsByRepaidFractionThenTotal(newDoc, evData); if (this.IsDocumentContentDifferent(newDoc, this.currentAchieversDoc)) { this.SaveDocumentInDB(newDoc); PeopleStatisticsDocumentRow achieversDocRow = this.dbc.CurrentPeopleStatisticsDocuments .Where(sd => sd.DocumentName == AchieversDoc.Name) .FirstOrDefault(); bool shouldAddNewRow = false; if (achieversDocRow == null) { shouldAddNewRow = true; achieversDocRow = new PeopleStatisticsDocumentRow(); } achieversDocRow.DocumentName = AchieversDoc.Name; achieversDocRow.DocumentId = newDoc.Id.ToString(); if (shouldAddNewRow) { this.dbc.CurrentPeopleStatisticsDocuments .Add(achieversDocRow); } this.dbc.SaveChanges(); this.currentAchieversDoc = newDoc; } // keep execution serialized until DebtDealAdded event is handled. }