added postgresql integration for user permissions
This commit is contained in:
@@ -43,7 +43,8 @@ Here are the key files created for this project in [UserPermissionTest_CS_WinFor
|
||||
|
||||
* 📄 **[UserPermissionTest_CS_WinForms.csproj](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/UserPermissionTest_CS_WinForms.csproj)**: SDK-style project file configured for `net481` with Windows Forms enabled and `Newtonsoft.Json` package installed.
|
||||
* 📄 **[Program.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/Program.cs)**: Main execution entry point.
|
||||
* 📄 **[User.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/User.cs)**: Object model for users holding Username, Full Name, Password (hashed), and Permissions list.
|
||||
* 📄 **[User.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/User.cs)**: Object model for users holding Username, Full Name, Password (hashed), and Permissions list (ID-based, List<int>).
|
||||
* 📄 **[Permission.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/Permission.cs)**: Object model for system permissions holding Id (int) and Name (string).
|
||||
* 📄 **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)**: Dynamic state and session service utilizing cross-form event notifications (`SessionStateChanged`, `UsersUpdated`).
|
||||
* 📄 **[PasswordHasher.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/PasswordHasher.cs)**: Cryptographic utility providing one-way SHA-256 password hashing and validation.
|
||||
* 🖥 **MainForm**:
|
||||
@@ -58,9 +59,9 @@ Here are the key files created for this project in [UserPermissionTest_CS_WinFor
|
||||
|
||||
---
|
||||
|
||||
## 🗄️ Repository Pattern & PostgreSQL Migration Path
|
||||
## 🗄️ Repository Pattern & PostgreSQL Database Integration
|
||||
|
||||
To address future scalability and facilitate a seamless transition to a relational database like **PostgreSQL**, the data layer has been decoupled using the **Repository Pattern**:
|
||||
To address enterprise-level data persistence, the data layer has been decoupled using the **Repository Pattern** and is **fully integrated with PostgreSQL** using the `Npgsql` database driver:
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
@@ -69,37 +70,21 @@ graph LR
|
||||
IUserRepository <|.. PostgreSqlUserRepository
|
||||
|
||||
style IUserRepository fill:#1E293B,stroke:#F59E0B,stroke-width:2px,color:#fff
|
||||
style JsonUserRepository fill:#1E293B,stroke:#10B981,stroke-width:2px
|
||||
style PostgreSqlUserRepository fill:#1E293B,stroke:#0EA5E9,stroke-width:2px,stroke-dasharray: 5 5
|
||||
style JsonUserRepository fill:#1E293B,stroke:#10B981,stroke-width:2px,stroke-dasharray: 5 5
|
||||
style PostgreSqlUserRepository fill:#1E293B,stroke:#0EA5E9,stroke-width:2px
|
||||
```
|
||||
|
||||
### Decoupled Interfaces:
|
||||
### Decoupled Implementations:
|
||||
* **[IUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/IUserRepository.cs)**: Contract layer. Declares `LoadUsers`, `SaveUsers`, `LoadPermissions`, and `SavePermissions`.
|
||||
* **[JsonUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/JsonUserRepository.cs)**: Local file-based (JSON) storage provider using `Newtonsoft.Json`. Writes/reads database parameters to local files `users.json` / `permissions.json` inside output build directories.
|
||||
* **[PostgreSqlUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/PostgreSqlUserRepository.cs)**: **Active Production Provider**. Connects to a local PostgreSQL instance, handles automatic schema initialization (tables `users`, `user_permissions`, `available_permissions`), and uses robust atomic transactions for synchronization.
|
||||
* **[JsonUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/JsonUserRepository.cs)**: Alternative file-based (JSON) storage provider using `Newtonsoft.Json`. Writes/reads database parameters to local files `users.json` / `permissions.json` inside output build directories.
|
||||
|
||||
### 🐘 PostgreSQL Migration Steps:
|
||||
When you decide to migrate to PostgreSQL, you **only** need to follow these simple steps:
|
||||
1. Install `Npgsql` (PostgreSQL Client) and a mapper like `EntityFramework` or `Dapper` via NuGet.
|
||||
2. Implement a new class adhering to `IUserRepository`:
|
||||
```csharp
|
||||
public class PostgreSqlUserRepository : IUserRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
public PostgreSqlUserRepository(string connString) => _connectionString = connString;
|
||||
|
||||
public List<User> LoadUsers() { /* DB query */ }
|
||||
public void SaveUsers(List<User> users) { /* DB save */ }
|
||||
public List<string> LoadPermissions() { /* DB query */ }
|
||||
public void SavePermissions(List<string> permissions) { /* DB save */ }
|
||||
}
|
||||
```
|
||||
3. Swap a single line in **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)**:
|
||||
```csharp
|
||||
// Old: private static readonly IUserRepository UserRepository = new JsonUserRepository();
|
||||
// New:
|
||||
private static readonly IUserRepository UserRepository = new PostgreSqlUserRepository("Host=localhost;Database=mydb;Username=postgres;Password=pwd");
|
||||
```
|
||||
4. **Zero UI Code Modification**: None of the Windows Forms (`MainForm`, `LoginDialog`, `UserSettings`) will require any edits. The application will immediately pull from and write to PostgreSQL.
|
||||
### 🐘 Active PostgreSQL Configuration:
|
||||
The database connection is managed directly via Dependency Injection in **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)**:
|
||||
```csharp
|
||||
private static readonly IUserRepository UserRepository = new PostgreSqlUserRepository("Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=postgres");
|
||||
```
|
||||
* **Seamless Interchangeability**: Switching back to JSON file storage is as simple as commenting out the PostgreSQL line and uncommenting the JSON class. None of the Windows Forms (`MainForm`, `LoginDialog`, `UserSettings`) require any code edits.
|
||||
|
||||
---
|
||||
|
||||
@@ -203,7 +188,8 @@ graph TD
|
||||
|
||||
* 📄 **[UserPermissionTest_CS_WinForms.csproj](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/UserPermissionTest_CS_WinForms.csproj)**: `net481` hedefleyen, Windows Forms etkinleştirilmiş ve `Newtonsoft.Json` bağımlılığı yüklenmiş SDK stili modern proje dosyası.
|
||||
* 📄 **[Program.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/Program.cs)**: Uygulamanın ana giriş noktası.
|
||||
* 📄 **[User.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/User.cs)**: Kullanıcı adı, Ad Soyad, Parola (Hash formatında) ve İzin listesini barındıran veri sınıfı.
|
||||
* 📄 **[User.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/User.cs)**: Kullanıcı adı, Ad Soyad, Parola (Hash formatında) ve İzin listesini (ID tabanlı, List<int>) barındıran veri sınıfı.
|
||||
* 📄 **[Permission.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/Permission.cs)**: Sistemdeki yetkilerin benzersiz kimliğini (Id) ve metnini (Name) barındıran veri sınıfı.
|
||||
* 📄 **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)**: Formlar arasında gerçek zamanlı olay bildirimleri (`SessionStateChanged`, `UsersUpdated`) sunan merkezi statik oturum yöneticisi.
|
||||
* 📄 **[PasswordHasher.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/PasswordHasher.cs)**: SHA-256 tek yönlü parola kriptolama ve doğrulama yardımcı sınıfı.
|
||||
* 🖥 **MainForm**:
|
||||
@@ -218,9 +204,9 @@ graph TD
|
||||
|
||||
---
|
||||
|
||||
## 🗄️ Depo Tasarım Deseni (Repository Pattern) & PostgreSQL Geçiş Yolu
|
||||
## 🗄️ Depo Tasarım Deseni (Repository Pattern) & Aktif PostgreSQL Entegrasyonu
|
||||
|
||||
Gelecekte uygulamanın SQL veritabanına taşınmasını kolaylaştırmak ve ölçeklenebilirlik sağlamak adına veri erişim katmanı tamamen **Repository Deseni** ile soyutlaştırılmıştır:
|
||||
Veri erişim katmanı tamamen **Repository Deseni** ile soyutlaştırılmış ve `Npgsql` veritabanı sürücüsü kullanılarak **aktif olarak PostgreSQL veritabanına bağlanmıştır**:
|
||||
|
||||
```mermaid
|
||||
graph LR
|
||||
@@ -229,37 +215,21 @@ graph LR
|
||||
IUserRepository <|.. PostgreSqlUserRepository
|
||||
|
||||
style IUserRepository fill:#1E293B,stroke:#F59E0B,stroke-width:2px,color:#fff
|
||||
style JsonUserRepository fill:#1E293B,stroke:#10B981,stroke-width:2px
|
||||
style PostgreSqlUserRepository fill:#1E293B,stroke:#0EA5E9,stroke-width:2px,stroke-dasharray: 5 5
|
||||
style JsonUserRepository fill:#1E293B,stroke:#10B981,stroke-width:2px,stroke-dasharray: 5 5
|
||||
style PostgreSqlUserRepository fill:#1E293B,stroke:#0EA5E9,stroke-width:2px
|
||||
```
|
||||
|
||||
### Soyut Katmanlar:
|
||||
* **[IUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/IUserRepository.cs)**: Tanım arayüzüdür. `LoadUsers`, `SaveUsers`, `LoadPermissions` ve `SavePermissions` metot imzalarını içerir.
|
||||
* **[JsonUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/JsonUserRepository.cs)**: Dosya tabanlı (JSON) veri saklayıcıdır. Çıktı dizinindeki `users.json` ve `permissions.json` dosyalarını okur/yazar.
|
||||
### Depo Gerçekleştirmeleri:
|
||||
* **[IUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/IUserRepository.cs)**: Tanım arayüzüdür. `LoadUsers`, `SaveUsers`, `LoadPermissions` ve `SavePermissions` imzalarını içerir.
|
||||
* **[PostgreSqlUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/PostgreSqlUserRepository.cs)**: **Aktif Veritabanı Sağlayıcısı**. PostgreSQL servisine bağlanır, sistem tablolarını (`users`, `user_permissions`, `available_permissions`) başlangıçta otomatik olarak oluşturur ve güvenli veritabanı işlemleri için `Transaction` tabanlı veri senkronizasyonu yapar.
|
||||
* **[JsonUserRepository.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/JsonUserRepository.cs)**: Alternatif dosya tabanlı (JSON) veri saklayıcıdır. Çıktı dizinindeki `users.json` ve `permissions.json` dosyalarını okur/yazar.
|
||||
|
||||
### 🐘 PostgreSQL Geçiş Adımları:
|
||||
Veritabanı kurulumunuzu yaptığınızda **yalnızca** şu basit adımları izlemeniz yeterli olacaktır:
|
||||
1. `Npgsql` (PostgreSQL Client) ve `EntityFramework` veya `Dapper` kütüphanesini NuGet ile projenize yükleyin.
|
||||
2. `IUserRepository` arayüzünü uygulayan yeni bir repository sınıfı yazın:
|
||||
```csharp
|
||||
public class PostgreSqlUserRepository : IUserRepository
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
public PostgreSqlUserRepository(string connString) => _connectionString = connString;
|
||||
|
||||
public List<User> LoadUsers() { /* PostgreSQL SELECT sorgusu */ }
|
||||
public void SaveUsers(List<User> users) { /* PostgreSQL Kaydetme */ }
|
||||
public List<string> LoadPermissions() { /* DB yetki listesi sorgusu */ }
|
||||
public void SavePermissions(List<string> permissions) { /* DB yetki kaydı */ }
|
||||
}
|
||||
```
|
||||
3. **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)** dosyasındaki tek satırı güncelleyin:
|
||||
```csharp
|
||||
// Eski JSON: private static readonly IUserRepository UserRepository = new JsonUserRepository();
|
||||
// Yeni PostgreSQL:
|
||||
private static readonly IUserRepository UserRepository = new PostgreSqlUserRepository("Host=localhost;Database=mydb;Username=postgres;Password=pwd");
|
||||
```
|
||||
4. **Sıfır Arayüz Değişikliği**: `MainForm`, `LoginDialog` veya `UserSettings` formlarındaki tek bir satır kod dahi değiştirilmez. Uygulama verileri anında PostgreSQL üzerinden okuyup yazacaktır.
|
||||
### 🐘 Aktif PostgreSQL Yapılandırması:
|
||||
Veritabanı bağlantısı **[SessionManager.cs](file:///D:/02_Programming/CSharp/CodingSandbox/UserPermissionTest_CS_WinForms/SessionManager.cs)** dosyasında tek bir satır üzerinden Dependency Injection ile yönetilir:
|
||||
```csharp
|
||||
private static readonly IUserRepository UserRepository = new PostgreSqlUserRepository("Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=postgres");
|
||||
```
|
||||
* **Kusursuz Değiştirilebilirlik**: JSON tabanlı dosya saklama modülüne geri geçmek isterseniz, PostgreSQL satırını yorum satırı yapıp JSON satırını açmanız yeterlidir. Arayüz kodlarında (`MainForm`, `LoginDialog` veya `UserSettings`) tek bir satır dahi değişmez.
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user