---
title: MedicareService
summary: API reference for MedicareService.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# MedicareService

Implements `IMedicareService`, the batch job that reconciles Medicare-beneficiary query results against the `MedicareQueryHistory` table for each submission batch, inserting new history entries and updating stale ones, then deleting the now-processed beneficiary batch. It depends on `IUnitOfWork` for persistence, `IMedicareQueryProvider` for reading beneficiary/batch data, and `IUserProfileService` to stamp history records with the current user. It accumulates run totals in an internally held `MedicareHistoryResults` instance created in the constructor and mutated across calls within a single service lifetime.

## Purpose
- Processes the next available minimum-submission batch of Medicare beneficiary data: deletes the prior batch if it has already been fully reconciled, then inserts/updates history for the new batch.
- Determines whether a submission batch has been fully processed by comparing the beneficiary count against the matching history count for that submission date.
- Inserts new `MedicareQueryHistory` records for beneficiaries with no existing history match, and updates the `LastSubmissionDate` on records whose match is stale.
- Deletes the processed `MedicareBeneficiaryQuery` records and their batch marker once history has been reconciled.

## Context
- Namespace: `SAIF.Services.Claims.Medicare`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| MedicareService | `public MedicareService(IUnitOfWork uow, IUserProfileService prof, IMedicareQueryProvider qu)` | Wires the unit-of-work, user-profile, and Medicare query-provider collaborators, and initializes the internal `MedicareHistoryResults` accumulator. |
| ProcessNextBatch | `public MedicareHistoryResults ProcessNextBatch(ILogWriter controlReport)` | Deletes the previous batch's beneficiary records if already reconciled, processes the next minimum-submission batch into history, throws if the post-processing counts still don't match, and returns the accumulated result totals. |
| HasPreviousBatchBeenProcessed | `public bool HasPreviousBatchBeenProcessed(DateTime submissionDate)` | Returns `true` only when the beneficiary count and history count for the given submission date are equal, indicating the batch has been fully reconciled. |
| ProcessMedicareBeneficiarysStatelessMode | `public bool ProcessMedicareBeneficiarysStatelessMode(DateTime submissionDate)` | Within a unit-of-work transaction, inserts history for beneficiaries with no existing match and updates the submission date on stale matches; always returns `true`. |
| InsertMedicareQueryHistory | `public void InsertMedicareQueryHistory(MedicareBeneficiaryQuery medicareBeneficiary, string currentUser, DateTime submissionDate)` | Creates and adds a new `MedicareQueryHistory` record copying the beneficiary's identity/response fields and stamping first/last submission dates and audit fields. |
| ProcessDeleteMedicareBeneficiarysForBatch | `public bool ProcessDeleteMedicareBeneficiarysForBatch()` | Within a unit-of-work transaction, deletes all beneficiary records for the current minimum-submission batch along with the batch marker itself; always returns `true`. |

## Behavioral notes
- `ProcessNextBatch` throws a generic `Exception("Counts Don't match")` if, after processing, the new batch is still not fully reconciled — a coarse-grained integrity check rather than a typed exception.*
- `ProcessMedicareBeneficiarysStatelessMode` and `ProcessDeleteMedicareBeneficiarysForBatch` each wrap their writes in `IUnitOfWork.Begin()`/`Commit()`.
- Result counters (`MedicareBeneRead`, `HistoryUpdated`, `HistoryInserted`, `MedicareBeneDeleted`) accumulate on the single `_historyResult` instance created at construction, so a `MedicareService` instance is intended for one batch-processing run rather than reuse across independent runs.*

> **Note:** Statements marked with * were inferred from code structure, naming conventions, or
> contextual evidence where direct confirmation was not available in source artifacts. Statements
> marked with ** could not be confirmed; treat as unknown until verified.

## Sources
- Services/SAIF.Services/Claims/Medicare/MedicareService.cs
