نمونه ریپازیتوری برای ADO .Net (ADO .Net Repository Sample)
انجام عملیات روی دیتابیس با استفاده از Stored Procedure در SQL Server
پیشنیازها و وابستگیها (Dependencies)
EntityFramework.Data.SqlClient// Project Context Name => SqlDataContext
public class CategoryRepository(ISqlDataContext dataContext) : ICategoryRepository
{
private CategoryDto Map(SqlDataReader reader) => new CategoryDto
{
Id = reader.GetInt32(reader.GetOrdinal("Id")),
Title = reader.GetString(reader.GetOrdinal("Title"))
};
public async Task<List<CategoryDto>> GetAllCategoryAsync()
{
return await dataContext.ExecuteReaderAsync("sp_GetAllCategories", CommandType.StoredProcedure, Map);
}
public async Task<CategoryDto?> GetCategoryByIdAsync(int id)
{
var category = await dataContext.ExecuteReaderAsync("sp_GetAllCategoriesById", CommandType.StoredProcedure, Map, new SqlParameter("@Id", id));
return category.FirstOrDefault();
}
public async Task<int> AddCategoryAsync(CategoryDto dto)
{
var category = await dataContext.ExecuteScalarAsync("sp_AddCategory", CommandType.StoredProcedure, new SqlParameter("@Title", dto.Title));
return Convert.ToInt32(category);
}
public async Task<int> UpdateCategoryAsync(CategoryDto dto)
{
return await dataContext.ExecuteNonQueryAsync("sp_UpdateCategory", CommandType.StoredProcedure, new SqlParameter("@Id", dto.Id), new SqlParameter("@Title", dto.Title));
}
public async Task<int> DeleteCategoryAsync(CategoryDto dto)
{
return await dataContext.ExecuteNonQueryAsync("sp_DeleteCategory", CommandType.StoredProcedure, new SqlParameter("@Id", dto.Id));
}
}
پیشنیازها و کدهای پایه:
1 مورد
مراحل بعد و کدهای تکمیلی:
1 مورد