Created
September 29, 2023 06:43
-
-
Save alemik/9a5a1530c2e71cf22de5ca04656daca8 to your computer and use it in GitHub Desktop.
Generic CRUD VM
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
| using AutoMapper; | |
| using Dapper; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Data; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| namespace YourNamespace | |
| { | |
| public class GenericCrudViewModel<TEntity, TDto> | |
| { | |
| private readonly IDbConnection _dbConnection; | |
| private readonly IMapper _mapper; | |
| public GenericCrudViewModel(IDbConnection dbConnection, IMapper mapper) | |
| { | |
| _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); | |
| _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); | |
| } | |
| public async Task<IEnumerable<TDto>> GetAllAsync() | |
| { | |
| var query = $"SELECT * FROM {GetTableName()}"; | |
| var entities = await _dbConnection.QueryAsync<TEntity>(query); | |
| return _mapper.Map<IEnumerable<TDto>>(entities); | |
| } | |
| public async Task<TDto> GetByIdAsync(int id) | |
| { | |
| var query = $"SELECT * FROM {GetTableName()} WHERE Id = @Id"; | |
| var entity = await _dbConnection.QuerySingleOrDefaultAsync<TEntity>(query, new { Id = id }); | |
| if (entity == null) | |
| return null; | |
| return _mapper.Map<TDto>(entity); | |
| } | |
| public async Task<int> CreateAsync(TDto dto) | |
| { | |
| var entity = _mapper.Map<TEntity>(dto); | |
| var query = $"INSERT INTO {GetTableName()} ({GetColumnNames()}) VALUES ({GetParamNames()})"; | |
| return await _dbConnection.ExecuteAsync(query, entity); | |
| } | |
| public async Task<int> UpdateAsync(int id, TDto dto) | |
| { | |
| var entity = _mapper.Map<TEntity>(dto); | |
| var query = $"UPDATE {GetTableName()} SET {GetUpdateColumns()} WHERE Id = @Id"; | |
| entity.GetType().GetProperty("Id")?.SetValue(entity, id); | |
| return await _dbConnection.ExecuteAsync(query, entity); | |
| } | |
| public async Task<int> DeleteAsync(int id) | |
| { | |
| var query = $"DELETE FROM {GetTableName()} WHERE Id = @Id"; | |
| return await _dbConnection.ExecuteAsync(query, new { Id = id }); | |
| } | |
| private string GetTableName() | |
| { | |
| // Implementa la logica per ottenere il nome della tabella dal tipo TEntity | |
| // Ad esempio, supponiamo che il nome della tabella sia lo stesso del nome della classe in lowercase | |
| return typeof(TEntity).Name.ToLower(); | |
| } | |
| private string GetColumnNames() | |
| { | |
| // Implementa la logica per ottenere i nomi delle colonne della tabella | |
| // Ad esempio, supponiamo che tutte le proprietà pubbliche dell'entità siano colonne | |
| var propertyNames = typeof(TEntity).GetProperties().Select(p => p.Name); | |
| return string.Join(", ", propertyNames); | |
| } | |
| private string GetParamNames() | |
| { | |
| // Implementa la logica per ottenere i nomi dei parametri per l'inserimento | |
| // Ad esempio, supponiamo che tutti i parametri siano @NomeProprietà | |
| var propertyNames = typeof(TEntity).GetProperties().Select(p => "@" + p.Name); | |
| return string.Join(", ", propertyNames); | |
| } | |
| private string GetUpdateColumns() | |
| { | |
| // Implementa la logica per ottenere la lista di colonne da aggiornare | |
| // Ad esempio, supponiamo che tutte le proprietà pubbliche dell'entità siano colonne tranne "Id" | |
| var propertyNames = typeof(TEntity).GetProperties().Where(p => p.Name != "Id").Select(p => $"{p.Name} = @{p.Name}"); | |
| return string.Join(", ", propertyNames); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment