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

# BillDiagnosisService

Manages the diagnosis codes attached to a bill, enforcing bill-type-specific rules (a diagnosis pointer code is required on `Standard` bills) and duplicate-code detection that differs by bill type (`Standard` bills forbid duplicate codes; `Inpatient`/`Outpatient` bills forbid duplicate code/type-code combinations). It resolves bills through `IBillQueryProvider`, validates diagnosis codes against `IDiagnosisService`, persists changes through `IUnitOfWork`, and validates contract objects through `IValidationService`, stamping every change with the current user (derived from `IUserProfileService`, truncated to the portion before a `-`).

## Purpose
- Adds, updates, and removes one or more diagnoses on a bill within a single transaction.
- Enforces bill-type-specific diagnosis rules: pointer codes required on standard bills, and duplicate detection rules that vary between standard and inpatient/outpatient bill types.
- Normalizes and validates incoming diagnosis codes before they are persisted, resolving each code against the canonical diagnosis reference data.
- Reports whether an attached code and description are recognized/valid so calling UI can flag unknown diagnosis codes.
- Exposes the diagnosis type code reference table for bill entry forms.

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

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| BillDiagnosisService (constructor) | `public BillDiagnosisService(IUnitOfWork uow, IUserProfileService uprof, IValidationService val, IBillQueryProvider billQueryProvider, IDiagnosisService diagnosisService)` | Wires the repository, current-user resolver, validator, bill query provider, and diagnosis lookup service used by all diagnosis operations; throws if any argument is null and pre-computes the current user ID. |
| CreateDiagnosis | `public BillDiagnosis CreateDiagnosis(long billId, BillDiagnosis newDiagnosis, IMessageContainer messageContainer)` | Adds a single diagnosis to the bill by delegating to `CreateDiagnoses` and returning the sole created result, or `null` on validation failure. |
| CreateDiagnoses | `public List<BillDiagnosis> CreateDiagnoses(long billId, List<BillDiagnosis> newDiagnoses, IMessageContainer messageContainer)` | Validates and adds the supplied diagnoses to the bill in one transaction, resolving each diagnosis code against `IDiagnosisService`, rejecting duplicates per bill-type rules, and returning the newly added diagnoses with validity/description populated; returns an empty list and rolls back if validation or duplicate checks fail. |
| GetDiagnoses | `public List<BillDiagnosis> GetDiagnoses(long billId, IMessageContainer messageContainer)` | Returns all diagnoses currently attached to the bill, each annotated with whether its code is recognized and its description. |
| UpdateDiagnosis | `public BillDiagnosis UpdateDiagnosis(long billId, BillDiagnosis updatedDiagnosis, IMessageContainer messageContainer)` | Updates a single diagnosis on the bill by delegating to `UpdateDiagnoses` and returning the sole modified result, or `null` on failure. |
| UpdateDiagnoses | `public List<BillDiagnosis> UpdateDiagnoses(long billId, List<BillDiagnosis> updatedDiagnoses, IMessageContainer messageContainer)` | Applies edits to existing diagnoses on the bill after validation and duplicate checks, rejecting any diagnosis whose `UpdateNumber` no longer matches the persisted record (adding a conflict error naming the last updating user) and aborting the whole batch on that conflict or on validation/duplicate failure. |
| DeleteDiagnosis | `public void DeleteDiagnosis(long billId, long diagnosisId, IMessageContainer messageContainer)` | Removes a single diagnosis from the bill by delegating to the multi-ID `DeleteDiagnoses` overload. |
| DeleteDiagnoses | `public void DeleteDiagnoses(long billId, long[] diagnosesIds, IMessageContainer messageContainer)` | Removes the diagnoses with the given IDs from the bill and commits the change. |
| DeleteDiagnoses | `public void DeleteDiagnoses(long billId, IMessageContainer messageContainer)` | Removes every diagnosis currently attached to the bill and commits the change. |
| GetDiagnosisTypeCodes | `public List<CbpDiagnosisTypeCode> GetDiagnosisTypeCodes()` | Returns the full diagnosis type code reference list. |

## Behavioral notes
- A static constructor calls `BillMappingHelper.MapBillService()` once per app domain to register the AutoMapper mappings this service relies on.
- All mutating operations (`CreateDiagnoses`, `UpdateDiagnoses`, `DeleteDiagnosis(es)`) wrap changes in `unitOfWork.Begin()`/`unitOfWork.Commit()`, and validation failures call `unitOfWork.Dispose()` to abandon the transaction rather than committing.
- Diagnosis codes and pointer/type codes are trimmed and upper-cased before validation via a private `CleanDiagnoses` step.
- Duplicate detection logic branches on `bill.BillType.Code`, throwing `ApplicationException` for any bill type code it does not recognize.*
- `UpdateDiagnoses` performs an optimistic-concurrency check per diagnosis via `UpdateNumber`, aborting the entire batch update on the first conflict found.

## Constraints and usage notes
- Standard bills require a non-empty `DiagnosisPointerCode` on every diagnosis; this is enforced only for bill type code `Standard`.
- Callers should treat an empty returned list from `CreateDiagnoses`/`UpdateDiagnoses` as a validation or conflict failure and inspect `messageContainer` for details, since no exception is thrown in that path.

> **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/Bills/BillDiagnosisService.cs`
