refactor: Implement Singleton pattern for DatabaseManager
Refactor the database handling to use a Singleton pattern for `DatabaseManager`. - A single, module-level instance `db_manager` is created in `core/database.py` to ensure one database connection is used throughout the application. - `MediaRepository` and `MainController` are updated to use this shared instance instead of creating their own. - This simplifies dependency injection and prevents potential issues with multiple database connections. - Also, update `review.md` to reflect the progress.
This commit is contained in:
@@ -154,3 +154,6 @@ class DatabaseManager:
|
||||
cur = self.conn.cursor()
|
||||
cur.execute("DELETE FROM media WHERE color_id = ?", (color_id,))
|
||||
self.conn.commit()
|
||||
|
||||
|
||||
db_manager = DatabaseManager()
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from core.database import DatabaseManager
|
||||
from core.database import db_manager
|
||||
|
||||
MEDIA_DIR = Path("media")
|
||||
DEFAULT_ICON = Path("media/default_icon.jpg")
|
||||
|
||||
class MediaRepository:
|
||||
def __init__(self, db: DatabaseManager):
|
||||
self.db = db
|
||||
def __init__(self):
|
||||
self.db = db_manager
|
||||
|
||||
def sync_media(self):
|
||||
disk_colors = {d.name for d in MEDIA_DIR.iterdir() if d.is_dir()}
|
||||
|
||||
Reference in New Issue
Block a user