Skip to content
Sann Lynn Htun edited this page Nov 22, 2024 · 4 revisions

RepoDB

RepoDB is a hybrid .NET ORM library that provides a high-level abstraction for database operations, combining the ease of use of micro-ORMs like Dapper with some of the advanced features of full ORMs like Entity Framework.

BlogModel.cs

public class BlogModel
{
    public int BlogId { get; set; }
    public string BlogTitle { get; set; }
    public string BlogAuthor { get; set; }
    public string BlogContent { get; set; }
}

1. Constructor

RepoDBExample.cs

public RepoDBExample(string connectionString)
{
    _connectionString = connectionString;
}

Summary: Initializes the class with the database connection string.

2. Run Method

public async Task Run()
{
    await Read();
    await Edit(1);
    await Edit(100);
    await Create("title", "author", "content");
    await Update(1, "updated title", "updated author", "updated content");
    await Delete(1);
}

Summary: This method serves as an entry point to test various database operations. Uncomment the desired operation to execute it.

3. Read Method

public async Task Read()
{
    using var connection = new SqlConnection(_connectionString);
    var blogs = await connection.QueryAllAsync<BlogModel>();

    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.BlogId);
        Console.WriteLine(blog.BlogTitle);
        Console.WriteLine(blog.BlogAuthor);
        Console.WriteLine(blog.BlogContent);
        Console.WriteLine("----------------------------");
    }
}

Summary: This method retrieves all rows from the Blogs table and prints each row's data to the console.

4. Edit Method

public async Task Edit(int id)
{
    using var connection = new SqlConnection(_connectionString);
    var blog = await connection.QueryAsync<BlogModel>(id);

    if (blog is null)
    {
        Console.WriteLine("No data found.");
        return;
    }

    Console.WriteLine(blog.BlogId);
    Console.WriteLine(blog.BlogTitle);
    Console.WriteLine(blog.BlogAuthor);
    Console.WriteLine(blog.BlogContent);
}

Summary: This method retrieves a specific blog entry based on the provided id and prints the details if found.

5. Create Method

public async Task Create(string title, string author, string content)
{
    var blog = new BlogModel
    {
        BlogTitle = title,
        BlogAuthor = author,
        BlogContent = content
    };

    using var connection = new SqlConnection(_connectionString);
    var result = await connection.InsertAsync(blog);

    string message = result > 0 ? "Saving Successful." : "Saving Failed.";
    Console.WriteLine(message);
}

Summary: This method inserts a new blog entry into the Blogs table with the provided title, author, and content. It indicates whether the save operation was successful.

6. Update Method

public async Task Update(int id, string title, string author, string content)
{
    var blog = new BlogModel
    {
        BlogId = id,
        BlogTitle = title,
        BlogAuthor = author,
        BlogContent = content
    };

    using var connection = new SqlConnection(_connectionString);
    var result = await connection.UpdateAsync(blog);

    string message = result > 0 ? "Updating Successful." : "Updating Failed.";
    Console.WriteLine(message);
}

Summary: This method updates an existing blog entry in the Blogs table based on the provided id, title, author, and content. It indicates whether the update operation was successful.

7. Delete Method

public async Task Delete(int id)
{
    using var connection = new SqlConnection(_connectionString);
    var result = await connection.DeleteAsync<BlogModel>(id);

    string message = result > 0 ? "Deleting Successful." : "Deleting Failed.";
    Console.WriteLine(message);
}

Summary: This method deletes a blog entry from the Blogs table based on the provided id. It indicates whether the delete operation was successful.

Each method in the RepoDBExample class is designed to perform CRUD operations on the Blogs table using RepoDB. The use of RepoDB simplifies the interaction with the database and ensures that changes are tracked and saved efficiently.

C# Basics Wiki

Core Concepts

Object-Oriented Programming (OOP)

Advanced Topics

Miscellaneous

Tools and Resources

Clone this wiki locally