-
Notifications
You must be signed in to change notification settings - Fork 5
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; }
}
RepoDBExample.cs
public RepoDBExample(string connectionString)
{
_connectionString = connectionString;
}
Summary: Initializes the class with the database connection string.
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.
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.
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.
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.
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.
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.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.