Extract shared JSON persistence logic into JsonPersistence helper#157
Extract shared JSON persistence logic into JsonPersistence helper#157Copilot wants to merge 5 commits into
Conversation
…class Co-authored-by: sunzhuoshi <592211+sunzhuoshi@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors duplicated JSON file persistence logic used by NotificationHistory and ReviewRequestHistory into a shared JsonPersistence helper to centralize load/save behavior and logging.
Changes:
- Added
src/JsonPersistence.cswith shared genericLoad<T>/Save<T>helpers. - Refactored
NotificationHistoryto delegate JSON persistence toJsonPersistence. - Refactored
ReviewRequestHistoryto delegate JSON persistence toJsonPersistence.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/JsonPersistence.cs | Introduces shared JSON load/save helpers with centralized serialization options and error logging. |
| src/NotificationHistory.cs | Removes in-class JSON persistence and uses JsonPersistence for loading/saving entries. |
| src/ReviewRequestHistory.cs | Removes in-class JSON persistence and uses JsonPersistence for loading/saving request IDs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
…ll-coalesce Co-authored-by: sunzhuoshi <592211+sunzhuoshi@users.noreply.github.com>
Applied both review changes in commit 2fd9063:
|
There was a problem hiding this comment.
Pull request overview
This PR removes duplicated JSON file load/save logic from NotificationHistory and ReviewRequestHistory by introducing a shared JsonPersistence helper, keeping persistence behavior consistent across both history stores.
Changes:
- Added
JsonPersistencestatic helper with genericLoad<T>/Save<T>wrappers aroundSystem.Text.Json. - Refactored
NotificationHistoryto delegate JSON persistence toJsonPersistence. - Refactored
ReviewRequestHistoryto delegate JSON persistence toJsonPersistence.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/JsonPersistence.cs |
Adds shared JSON load/save implementation and logging context. |
src/NotificationHistory.cs |
Replaces inline JSON persistence with JsonPersistence calls. |
src/ReviewRequestHistory.cs |
Replaces inline JSON persistence with JsonPersistence calls. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
…uestHistory Co-authored-by: sunzhuoshi <592211+sunzhuoshi@users.noreply.github.com>
Applied both changes in commit ccf4371:
|
There was a problem hiding this comment.
Pull request overview
This PR reduces duplication in the history persistence layer by extracting shared JSON load/save behavior into a reusable JsonPersistence helper and refactoring history classes to delegate persistence to it.
Changes:
- Added
JsonPersistencehelper with genericLoad<T>and atomicSave<T>(write.tmpthen replace). - Refactored
NotificationHistoryto useJsonPersistencefor load/save. - Refactored
ReviewRequestHistoryto useJsonPersistenceand removed its internal save lock (documenting lock expectations).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/JsonPersistence.cs | Introduces shared JSON load/save helpers and centralizes logging and atomic write behavior. |
| src/NotificationHistory.cs | Replaces inline JSON persistence with JsonPersistence calls. |
| src/ReviewRequestHistory.cs | Replaces inline JSON persistence with JsonPersistence calls and adjusts locking approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot apply changes based on the comments in this thread |
…NotificationHistory Co-authored-by: sunzhuoshi <592211+sunzhuoshi@users.noreply.github.com>
Applied all three changes in commit 6962c67:
|
There was a problem hiding this comment.
Pull request overview
This PR reduces duplicated JSON file persistence code by introducing a shared JsonPersistence helper and refactoring history classes to use it, including atomic writes for safer on-disk updates.
Changes:
- Added
JsonPersistencehelper with sharedLoad<T>/Save<T>methods and atomic save via.tmp+ overwrite move. - Refactored
NotificationHistoryto delegate JSON load/save toJsonPersistenceand avoid redundant/re-entrant locking duringAdd(). - Refactored
ReviewRequestHistoryto delegate JSON load/save toJsonPersistenceand rely on the existing external lock.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/JsonPersistence.cs | Introduces centralized JSON load/save with shared serializer options and atomic save semantics. |
| src/NotificationHistory.cs | Switches persistence to JsonPersistence and splits Save()/SaveUnlocked() to prevent re-entrant locking from Add(). |
| src/ReviewRequestHistory.cs | Switches persistence to JsonPersistence and removes internal locking from Save() (callers already hold the lock). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/JsonPersistence.csstatic helper class with sharedLoad<T>andSave<T>methodsNotificationHistory.csto useJsonPersistenceReviewRequestHistory.csto useJsonPersistenceJsonPersistencethread-safety docReviewRequestHistory.Load()JsonPersistence.Saveatomic (write to.tmp, thenFile.Movewith overwrite)lockfromReviewRequestHistory.Save().tmpfile infinallyblock ifFile.MovefailsNotificationHistory.Save()into privateSaveUnlocked()+ publicSave()wrapper to eliminate redundant re-entrant lock fromAdd()Original prompt
This section details on the original issue you should resolve
<issue_title>Duplicate Code: JSON Persistence Pattern in NotificationHistory and ReviewRequestHistory</issue_title>
<issue_description>Analysis of commit ebf44dd
Assignee:
@copilotSummary
NotificationHistoryandReviewRequestHistoryboth implement nearly identical thread-safe JSON file persistence logic, including a_lockObject, aLoad()method, and aSave()method. This structural duplication (~25 lines each) violates DRY and means any fix to the persistence layer must be applied twice.Duplication Details
Pattern: Thread-safe JSON file load/save with lock and error handling
src/NotificationHistory.cs(lines 10, 19–44)src/ReviewRequestHistory.cs(lines 8, 12–43)Code Sample — NotificationHistory.cs:
Code Sample — ReviewRequestHistory.cs (structurally identical):
Impact Analysis
Save()implementations are already inconsistent:NotificationHistorydoes not includeex.Messagein the log message string, whileReviewRequestHistorydoes — a symptom of copy-paste drift.Refactoring Recommendations
Extract a generic
JsonFileStore(T)base class or utilitysrc/JsonFileStore.csLoad()/Save()with locking and error handling.NotificationHistoryandReviewRequestHistorywould delegate to it.Alternatively: extract a static
JsonPersistencehelperTryLoad(T)(string path, out T value)andTrySave(T)(string path, T value)statics.Implementation Checklist
JsonFileStore(T)or equivalentNotificationHistoryto use itReviewRequestHistoryto use itAnalysis Metadata
src/NotificationHistory.cs,src/ReviewRequestHistory.cs🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.