-
Notifications
You must be signed in to change notification settings - Fork 0
V3.10 use dapper #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
V3.10 use dapper #113
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
67f3389
Add DapperSqlGenerator helper
lakatoshv ae26bbb
Create dapper repository
lakatoshv 38127bf
Add general dapper service
lakatoshv b65b1b1
Create Comments dapper service
lakatoshv 00a381b
Create Messages dapper service
lakatoshv 0bd17bd
Create posts dapper service
lakatoshv e7d3602
Create Posts Tags relation dapper service
lakatoshv 5c214ae
Create Profile dapper service
lakatoshv d114bf0
Create Tags dapper service
lakatoshv 3887c5e
Setup dapper repositories
lakatoshv d3d5834
Setup dapper services
lakatoshv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // <copyright file="DapperRepository.cs" company="Blog"> | ||
| // Copyright (c) Blog. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| using Blog.Core; | ||
| using Blog.Core.Infrastructure.Pagination; | ||
| using Blog.Data.Extensions; | ||
| using Blog.Data.Repository; | ||
| using Dapper; | ||
| using System.Collections.Generic; | ||
| using System.Data; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Blog.Data; | ||
|
|
||
| /// <summary> | ||
| /// Dapper repository. | ||
| /// </summary> | ||
| /// <typeparam name="T">IEntity.</typeparam> | ||
| public class DapperRepository<T>(IDbConnection connection) | ||
| : IDapperRepository<T> | ||
| where T : class, IEntity | ||
| { | ||
| /// <summary> | ||
| /// The table name. | ||
| /// </summary> | ||
| private readonly string _tableName = typeof(T).Name; | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<IEnumerable<T>> GetAllAsync() | ||
| => await connection.QueryAsync<T>($"SELECT * FROM {_tableName}"); | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<T?> GetByIdAsync(object id) | ||
| => await connection.QueryFirstOrDefaultAsync<T>( | ||
| $"SELECT * FROM {_tableName} WHERE Id = @Id", new { Id = id }); | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<int> InsertAsync(T entity) | ||
| { | ||
| var insertQuery = DapperSqlGenerator.GenerateInsert(entity, _tableName); | ||
|
|
||
| return await connection.ExecuteAsync(insertQuery.Sql, insertQuery.Params); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<int> UpdateAsync(T entity) | ||
| { | ||
| var updateQuery = DapperSqlGenerator.GenerateUpdate(entity, _tableName); | ||
|
|
||
| return await connection.ExecuteAsync(updateQuery.Sql, updateQuery.Params); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<int> DeleteAsync(object id) | ||
| => await connection.ExecuteAsync( | ||
| $"DELETE FROM {_tableName} WHERE Id = @Id", new { Id = id }); | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<bool> AnyAsync(string whereClause, object? param = null) | ||
| => await connection.ExecuteScalarAsync<int>( | ||
| $"SELECT COUNT(1) FROM {_tableName} WHERE {whereClause}", param) > 0; | ||
|
|
||
| /// <inheritdoc cref="IRepository{TEntity}"/> | ||
| public async Task<PagedListResult<T>> SearchAsync(DapperSearchQuery query) | ||
| { | ||
| var where = string.IsNullOrWhiteSpace(query.WhereClause) ? string.Empty : $"WHERE {query.WhereClause}"; | ||
| var order = string.IsNullOrWhiteSpace(query.OrderBy) ? string.Empty : $"ORDER BY {query.OrderBy}"; | ||
|
|
||
| var sqlCount = $"SELECT COUNT(1) FROM {_tableName} {where}"; | ||
| var total = await connection.ExecuteScalarAsync<int>(sqlCount, query.Parameters); | ||
|
|
||
| var sql = $""" | ||
| SELECT * FROM {_tableName} | ||
| {where} | ||
| {order} | ||
| OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY | ||
| """; | ||
|
|
||
| var data = await connection.QueryAsync<T>(sql, | ||
| new { query.Skip, query.Take, query.Parameters }); | ||
|
|
||
| return new PagedListResult<T> | ||
| { | ||
| Entities = data.ToList(), | ||
| Count = total, | ||
| HasNext = query.Skip + query.Take < total, | ||
| HasPrevious = query.Skip > 0 | ||
| }; | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
BlogWebApp/Data/Blog.Data/Extensions/DapperSqlGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // <copyright file="DapperSqlGenerator.cs" company="Blog"> | ||
| // Copyright (c) Blog. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| using System.Linq; | ||
|
|
||
| namespace Blog.Data.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Dapper Sql generator. | ||
| /// </summary>s | ||
| public static class DapperSqlGenerator | ||
| { | ||
| /// <summary> | ||
| /// Generate insert. | ||
| /// </summary> | ||
| /// <param name="entity">The entity.</param> | ||
| /// <param name="table">The table.</param> | ||
| /// <returns>The Sql string.</returns> | ||
| /// <returns>The params.</returns> | ||
| public static (string Sql, object Params) GenerateInsert<T>(T entity, string table) | ||
| { | ||
| var props = typeof(T).GetProperties() | ||
| .Where(p => p.Name != "Id"); | ||
|
|
||
| var columns = string.Join(",", props.Select(p => p.Name)); | ||
| var values = string.Join(",", props.Select(p => "@" + p.Name)); | ||
|
|
||
| var sql = $"INSERT INTO {table} ({columns}) VALUES ({values})"; | ||
|
|
||
| return (sql, entity); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Generate update. | ||
| /// </summary> | ||
| /// <param name="entity">The entity.</param> | ||
| /// <param name="table">The table.</param> | ||
| /// <returns>The Sql string.</returns> | ||
| /// <returns>The params.</returns> | ||
| public static (string Sql, object Params) GenerateUpdate<T>(T entity, string table) | ||
| { | ||
| var props = typeof(T).GetProperties() | ||
| .Where(p => p.Name != "Id"); | ||
|
|
||
| var setClause = string.Join(",", props.Select(p => $"{p.Name} = @{p.Name}")); | ||
|
|
||
| var sql = $"UPDATE {table} SET {setClause} WHERE Id = @Id"; | ||
|
|
||
| return (sql, entity); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // <copyright file="IDapperRepository.cs" company="PlaceholderCompany"> | ||
| // Copyright (c) PlaceholderCompany. All rights reserved. | ||
| // </copyright> | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Blog.Core; | ||
| using Blog.Core.Infrastructure.Pagination; | ||
|
|
||
| namespace Blog.Data.Repository; | ||
|
|
||
| /// <summary> | ||
| /// Dapper repository interface. | ||
| /// </summary> | ||
| /// <typeparam name="T">Type.</typeparam> | ||
| public interface IDapperRepository<T> | ||
| where T : IEntity | ||
| { | ||
| /// <summary> | ||
| /// Gets all asynchronous. | ||
| /// </summary> | ||
| /// <returns>Type.</returns> | ||
| Task<IEnumerable<T>> GetAllAsync(); | ||
|
|
||
| /// <summary> | ||
| /// Async get item by id async. | ||
| /// </summary> | ||
| /// <param name="id">id.</param> | ||
| /// <returns>Task.</returns> | ||
| Task<T?> GetByIdAsync(object id); | ||
|
|
||
| /// <summary> | ||
| /// Inserts the asynchronous. | ||
| /// </summary> | ||
| /// <param name="entity">The entity.</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| Task<int> InsertAsync(T entity); | ||
|
|
||
| /// <summary> | ||
| /// Updates the asynchronous. | ||
| /// </summary> | ||
| /// <param name="entity">The entity.</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| Task<int> UpdateAsync(T entity); | ||
|
|
||
| /// <summary> | ||
| /// Deletes the asynchronous. | ||
| /// </summary> | ||
| /// <param name="id">The identifier.</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| Task<int> DeleteAsync(object id); | ||
|
|
||
| /// <summary> | ||
| /// Asynchronous check on any. | ||
| /// </summary> | ||
| /// <param name="whereClause">The where clause.</param> | ||
| /// <param name="param">The parameter.</param> | ||
| /// <returns>Task.</returns> | ||
| Task<bool> AnyAsync(string whereClause, object? param = null); | ||
|
|
||
| /// <summary> | ||
| /// Async search. | ||
| /// </summary> | ||
| /// <param name="query">The query.</param> | ||
| /// <returns>Task.</returns> | ||
| Task<PagedListResult<T>> SearchAsync(DapperSearchQuery query); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test