Unit Of Work implementado
This commit is contained in:
@@ -21,5 +21,9 @@ namespace CleanArchitecture.Application.Contracts.Persistence
|
|||||||
Task<T> UpdateAsync(T entity);
|
Task<T> UpdateAsync(T entity);
|
||||||
Task DeleteAsync(T entity);
|
Task DeleteAsync(T entity);
|
||||||
|
|
||||||
|
void AddEntity(T entity);
|
||||||
|
void UpdateEntity(T entity);
|
||||||
|
void DeleteEntity(T entity);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using CleanArchitecture.Domain.Common;
|
||||||
|
|
||||||
|
namespace CleanArchitecture.Application.Contracts.Persistence
|
||||||
|
{
|
||||||
|
public interface IUnitOfWork: IDisposable
|
||||||
|
{
|
||||||
|
IAsyncRepository<T> Repository<T>() where T : BaseDomainModel;
|
||||||
|
|
||||||
|
Task<int> Complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using CleanArchitecture.Application.Contracts.Persistence;
|
||||||
|
using CleanArchitecture.Domain.Common;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace CleanArchitecture.Infrastructure.Persistence
|
||||||
|
{
|
||||||
|
public class UnitOfWork : IUnitOfWork
|
||||||
|
{
|
||||||
|
private Hashtable repositories;
|
||||||
|
private readonly StreamerDbContext context;
|
||||||
|
|
||||||
|
public UnitOfWork(StreamerDbContext _context)
|
||||||
|
{
|
||||||
|
context = _context;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<int> Complete()
|
||||||
|
{
|
||||||
|
return await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
context.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IAsyncRepository<T> Repository<T>() where T : BaseDomainModel
|
||||||
|
{
|
||||||
|
if(repositories == null)
|
||||||
|
{
|
||||||
|
repositories = new Hashtable();
|
||||||
|
}
|
||||||
|
var type = typeof(T).Name;
|
||||||
|
if(!repositories.ContainsKey(type))
|
||||||
|
{
|
||||||
|
var repositoryType = typeof(IAsyncRepository<>);
|
||||||
|
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), context);
|
||||||
|
repositories.Add(type, repositoryInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (IAsyncRepository<T>)repositories[type];
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,6 +69,7 @@ namespace CleanArchitecture.Infrastructure.Repositories
|
|||||||
|
|
||||||
public async Task<T> UpdateAsync(T entity)
|
public async Task<T> UpdateAsync(T entity)
|
||||||
{
|
{
|
||||||
|
context.Set<T>().Attach(entity);
|
||||||
context.Entry(entity).State = EntityState.Modified;
|
context.Entry(entity).State = EntityState.Modified;
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
return entity;
|
return entity;
|
||||||
@@ -80,5 +81,20 @@ namespace CleanArchitecture.Infrastructure.Repositories
|
|||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddEntity(T entity)
|
||||||
|
{
|
||||||
|
context.Set<T>().Add(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateEntity(T entity)
|
||||||
|
{
|
||||||
|
context.Set<T>().Attach(entity);
|
||||||
|
context.Entry(entity).State = EntityState.Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteEntity(T entity)
|
||||||
|
{
|
||||||
|
context.Set<T>().Remove(entity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
CleanArchitecture/EliminarBinObj.ps1
Normal file
27
CleanArchitecture/EliminarBinObj.ps1
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Este script de PowerShell busca y elimina los directorios 'bin' y 'obj' recursivamente desde la ubicación actual.
|
||||||
|
|
||||||
|
# Cambia el directorio de trabajo al directorio donde se ejecuta el script
|
||||||
|
Set-Location -Path $PSScriptRoot
|
||||||
|
|
||||||
|
# Función para buscar y eliminar directorios
|
||||||
|
function Remove-SpecifiedDirectories {
|
||||||
|
param (
|
||||||
|
[string]$startingDirectory,
|
||||||
|
[string[]]$directoryNamesToRemove
|
||||||
|
)
|
||||||
|
|
||||||
|
# Buscar todos los directorios que coincidan con los nombres especificados
|
||||||
|
$directories = Get-ChildItem -Path $startingDirectory -Recurse -Directory | Where-Object { $_.Name -in $directoryNamesToRemove }
|
||||||
|
foreach ($dir in $directories) {
|
||||||
|
# Eliminar el directorio y todos sus contenidos
|
||||||
|
Remove-Item -Path $dir.FullName -Recurse -Force -ErrorAction SilentlyContinue
|
||||||
|
if (!$?) {
|
||||||
|
Write-Warning "No se pudo eliminar: $($dir.FullName)"
|
||||||
|
} else {
|
||||||
|
Write-Output "Eliminado: $($dir.FullName)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Llama a la función con el directorio actual y los nombres de los directorios a eliminar
|
||||||
|
Remove-SpecifiedDirectories -startingDirectory (Get-Location) -directoryNamesToRemove @('bin', 'obj')
|
||||||
Reference in New Issue
Block a user