Вытащил солюшен на уровень выше, чтобы прощё было дотнетить
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		
							
								
								
									
										48
									
								
								Infrastructure/Database/Generic/GenericRepository.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								Infrastructure/Database/Generic/GenericRepository.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| using ApplicationLayer.GeneralExceptions; | ||||
| using ApplicationLayer.InfrastructureServicesInterfaces; | ||||
| using Domains; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic; | ||||
|  | ||||
| /// Generic repository pattern | ||||
| /// <param name="writer"><inheritdoc cref="IGenericWriter"/></param> | ||||
| /// <typeparam name="T">Type of entity</typeparam> | ||||
| /// <remarks>Should be inherited to create typed repositories</remarks> | ||||
| public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter writer) : IGenericRepository<T> | ||||
|     where T : class, IEntity | ||||
| { | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.GetAllAsync"/> | ||||
|     public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken) | ||||
|         => await LoadDomain().ToListAsync(cancellationToken); | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.GetByIdAsync"/> | ||||
|     public async Task<T> GetByIdAsync(Guid id, CancellationToken cancellationToken) | ||||
|     { | ||||
|         var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); | ||||
|         return result ?? throw new EntityNotFoundByIdException<T>(id); | ||||
|     } | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.AddAsync"/> | ||||
|     public async Task AddAsync(T entity, CancellationToken cancellationToken) | ||||
|         => await writer.AddAsync(entity, cancellationToken); | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.UpdateAsync"/> | ||||
|     public async Task UpdateAsync(T entity, CancellationToken cancellationToken) | ||||
|     { | ||||
|         await GetByIdAsync(entity.Id, cancellationToken); | ||||
|         writer.Update(entity); | ||||
|     } | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.Remove"/> | ||||
|     public void Remove(T entity) | ||||
|     { | ||||
|         writer.Remove(entity); | ||||
|     } | ||||
|  | ||||
|     /// Should be overriden to load navigation properties of entity | ||||
|     protected virtual IQueryable<T> LoadDomain() | ||||
|     { | ||||
|         return reader.GetAll<T>(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Infrastructure/Database/Generic/IGenericReader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Infrastructure/Database/Generic/IGenericReader.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic; | ||||
|  | ||||
| /// Reads from data storage | ||||
| public interface IGenericReader | ||||
| { | ||||
|     /// Get all entities of type T stored in storage | ||||
|     /// <typeparam name="T">Entity type to seek in storage</typeparam> | ||||
|     IQueryable<T> GetAll<T>() where T : class, IEntity; | ||||
| } | ||||
							
								
								
									
										25
									
								
								Infrastructure/Database/Generic/IGenericWriter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Infrastructure/Database/Generic/IGenericWriter.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| using ApplicationLayer.InfrastructureServicesInterfaces; | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic; | ||||
|  | ||||
| /// Writes data to data storage | ||||
| /// <remarks><see cref="IUnitOfWork"/> should be used to save changes</remarks> | ||||
| public interface IGenericWriter | ||||
| { | ||||
|     /// Add entity to data storage | ||||
|     /// <param name="entity">Entity to add</param> | ||||
|     /// <param name="cancellationToken">Cancellation Token</param> | ||||
|     /// <typeparam name="T">Entity type</typeparam> | ||||
|     Task AddAsync<T>(T entity, CancellationToken cancellationToken) where T : class, IEntity; | ||||
|  | ||||
|     /// Update entity in data storage | ||||
|     /// <param name="entity">Entity to update</param> | ||||
|     /// <typeparam name="T">Entity type</typeparam> | ||||
|     void Update<T>(T entity) where T : class, IEntity; | ||||
|  | ||||
|     /// Remove entity from data storage | ||||
|     /// <param name="entity">Entity to remove</param> | ||||
|     /// <typeparam name="T">Entity type</typeparam> | ||||
|     void Remove<T>(T entity) where T : class, IEntity; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user