This repository contains code examples and implementations from the Dapper in ASP.NET Core course. The course demonstrates how to integrate Dapper, a lightweight ORM, into .NET Core 8 applications to interact with SQL Server databases efficiently.
You can find the course here:
ุขู
ูุฒุด ุงุณุชูุงุฏู ุงุฒ Dapper ุฏุฑ AspNet Core
- .NET Core: 8.0
- Dapper: 2.1.35
- SQL Server Management Studio (SSMS): 18.12.2
Run the following SQL commands in SQL Server Management Studio:
CREATE DATABASE [Dapper]
USE [Dapper]
CREATE TABLE [dbo].[Product] (
[Id] [int] IDENTITY(1,1) NOT NULL,
[float] NULL,
[CreateDate] [datetime] NULL
)
INSERT INTO [dbo].[Product]
(Name, Cost, CreateDate)
VALUES
('Dapper Test', 20, '2024-2-2')
- Use the .NET CLI or Visual Studio to create a new ASP.NET Core Web API project targeting .NET Core 8.
Add the Dapper NuGet package to your project:
dotnet add package Dapper --version 2.1.35The project follows a modular structure with the following key components:
Located in the Models folder, the Product class maps to the Product table in the database:
// Models/Product.cs
public class Product
{
public int Id { get; set; } // Primary key of the Product table
public string Name { get; set; } // Name of the product
public float? Cost { get; set; } // Cost of the product
public DateTime? CreateDate { get; set; } // Date the product was created
}The ICommandText interface provides SQL commands for Dapper operations:
// Interfaces/ICommandText.cs
public interface ICommandText
{
string GetProducts { get; } // SQL command to retrieve all products
string GetProductById { get; } // SQL command to retrieve a product by ID
string AddProduct { get; } // SQL command to add a new product
string UpdateProduct { get; } // SQL command to update an existing product
string RemoveProduct { get; } // SQL command to remove a product by ID
}The IProductRepository interface is implemented to interact with the database using Dapper.
// Repositories/IProductRepository.cs
public interface IProductRepository
{
List<Product> GetAllProduct();
Product GetProductById(int id);
void AddProduct(Product product);
void UpdateProduct(Product product);
void RemoveProduct(int id);
}##Repository Implementation
You will implement the IProductRepository interface to interact with the database using Dapper.
Here are the endpoints exposed by the application:
- Description: Retrieve all products.
- Method:
GET
- Description: Retrieve a single product by ID.
- Method:
GET
- Description: Add a new product.
- Method:
POST
- Description: Update an existing product.
- Method:
PUT
- Description: Remove a product by ID.
- Method:
DELETE
Add the connection string in appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER_NAME;Database=Dapper;Trusted_Connection=True;"
}Add the connection string in appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER_NAME;Database=Dapper;Trusted_Connection=True;"
}
}In the Program.cs file, register your services and configure the database connection:
// Program.cs
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<ICommandText, CommandText>();- Clone this repository:
git clone [https://github.com/MohammadEbrahimi-dev-Course/Dapper.git](https://github.com/MohammadEbrahimi-dev-Course/Dapper.git)-
Set up the database using the provided SQL script.
-
Configure the connection string in
appsettings.json:
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER_NAME;Database=Dapper;Trusted_Connection=True;"
}4.Build and run the project using the following command:
dotnet run5.Use tools like Postman or Swagger to test the API endpoints.
This repository is created as part of the Dapper in ASP.NET Core course on TopLearn.
Happy coding! ๐