Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lazurdit/fluentorm

An easy-to-use object-relational mapping (ORM)
https://github.com/lazurdit/fluentorm

csharp-code dotnet fluent orm

Last synced: 30 days ago
JSON representation

An easy-to-use object-relational mapping (ORM)

Awesome Lists containing this project

README

        

# LazurdIT.FluentOrm

**FluentOrm** is an easy-to-use object-relational mapping (ORM) framework that simplifies database operations with a readable and fluent interface. It allows developers to interact with their databases using simple, chainable methods, With **FluentORM**, you can easily manage your database operations using a fluent interface. From simple CRUD operations to complex queries, it streamlines the process and improves efficiency in data management.

## License

This project is licensed under the MIT (With Attribution) License, with required attribution to the original repository: [FluentORM Repository](https://github.com/lazurdit/FluentOrm).

## Features

- **Simplified ORM:** Streamline your interactions with relational databases.
- **CRUD Operations:** Perform Create, Read, Update, and Delete operations with a fluent interface.
- **Multi-Database Support:** Compatible with PostgreSQL, MSSQL, MySQL, SQLite, and Oracle.
- **Advanced Capabilities:** Includes filtering, sorting, and pagination to enhance data retrieval.
- **Efficient Data Handling:** Supports batch inserts and updates for large datasets.

---

## Table of Contents

1. [Getting Started](#getting-started)
2. [How to Install](#how-to-install)
3. [How to Set Up](#how-to-set-up)
4. [How to Use](#how-to-use)
- [Insert Operation](#1-insert-operation)
- [List Operation](#2-list-operation)
- [Update Operation](#3-update-operation)
- [Delete Operation](#4-delete-operation)

---

## Getting Started

### Prerequisites

Before using **FluentOrm**, ensure you have the following installed:

- **.NET SDK 6, 7, or 8**
- A properly configured database connection.

---

## How to Install

To install **FluentORM**, use either the .NET CLI or NuGet Package Manager:

### 1. .NET CLI

Run the following command in your terminal to install the package:

```bash
dotnet add package Lazurd.FluentORM
```

### 2. NuGet Package Manager

Open the NuGet Package Manager Console and run the following command:

```bash
Install-Package Lazurd.FluentORM
```

---

## How to Set Up

Once installed, follow these steps to set up **FluentORM** in your project:

### 1. Database Prerequisites

Ensure you have a working database setup, and install the necessary drivers based on your database system. **FluentORM** supports multiple databases, including PostgreSQL, MSSQL, MySQL, SQLite, and Oracle.

**Example for MsSql:**

```csharp
string connectionString = "Server=(localdb)\\MSSQLLocalDB;Database=StudentDB;Trusted_Connection=true;TrustServerCertificate=True;MultipleActiveResultSets=true";
```

### 2. Repository Creation

Create a repository class that inherits from the provided repository interface. For example, for managing students:

```csharp
internal class StudentContext : MsSqlFluentRepository
{
// Custom repository logic if needed
}
```

### 3. Model Class Definition

Define a model class that corresponds to your database table. For instance, here’s a `Student` model:

```csharp
internal class Student : IFluentModel
{
[FluentField(name: "StudentID", isPrimary: true, autoGenerated: true, allowNull: true)]
public int Id { get; set; }

[FluentField(name: "StudentName", allowNull: false)]
public string Name { get; set; } = string.Empty;

[FluentField(name: "StudentAddress")]
public string? Address { get; set; }
}
```

---

## How to Use

Once everything is set up, you can start performing CRUD operations using **FluentOrm**. Below are examples of basic operations.

### 1. Insert Operation

```csharp
private static void AddStudent(string connectionString)
{
using SqlConnection connection = new(connectionString);
connection.Open();
StudentRepository studentRepository = new();

var insertQuery = studentRepository.Insert()
.WithConnection(connection)
.WithFields(f => f.Exclude(s => s.Id));

var result = insertQuery.Execute(new Student
{
Id = 10000,
Name = "John Doe",
Address = "Jordan-Amman",
}, returnNewRecord: true);

Console.WriteLine($"Student inserted with record Id: {result?.Id}");
}
```

**Output:**

```console
Student inserted with record Id: 2008
```

### 2. List Operation

```csharp
private static void ListStudents(string connectionString)
{
using SqlConnection connection = new(connectionString);
connection.Open();
StudentRepository studentRepository = new();

var selectQuery = studentRepository.Select()
.WithConnection(connection)
.OrderBy(o => o.FromField(f => f.Name, OrderDirections.Ascending));

var students = selectQuery.Execute();

foreach (var student in students)
{
Console.WriteLine($"Student {student.Id}: {student.Name}, {student.Address}");
}
}
```

**Output:**

```console
Student 2003: Ahamd Akram, Amman
Student 2004: John Doe, Amman
...
Student 1005: John Doe, Amman
```

### 3. Update Operation

Updating an existing student record.

```csharp
private static void UpdateStudent(string connectionString, int studentId, string newName)
{
Console.WriteLine($"{DateTime.Now:HH:mm:ss fffff}: Updating Student");

using SqlConnection connection = new(connectionString);
connection.Open();
StudentRepository studentRepository = new();

var updateQuery = studentRepository.Update()
.WithConnection(connection)
.Where(m => m.Eq(f => f.Id, studentId))
.WithFields(m => m.ExcludeAll().FromField(f => f.Name));

var result = updateQuery.Execute(new Student
{
Name = newName
});

Console.WriteLine(result > 0
? $"{DateTime.Now:HH:mm:ss fffff}: Student with Id {studentId} updated with new name: {newName}"
: $"{DateTime.Now:HH:mm:ss fffff}: No student found with Id {studentId}.");
}
```

**Output:**

```console
01:22:55 58670: Updating Student
01:22:55 93239: Student with Id 2004 updated with new name: Joe Ahmad
```

### 4. Delete Operation

Deleting a student record by ID.

```csharp
private static void DeleteStudent(string connectionString, int studentId)
{
using SqlConnection connection = new(connectionString);
connection.Open();
StudentRepository studentRepository = new();

var deleteQuery = studentRepository.Delete()
.WithConnection(connection)
.Where(m => m.Eq(f => f.Id, studentId));

var result = deleteQuery.Execute();

Console.WriteLine(result > 0
? $"Student with Id {studentId} deleted."
: $"No student found with Id {studentId}.");
}
```

**Output:**

```console
Student with Id 2005 deleted.
```