using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace PhoenixLib.DAL.EFCore.PGSQL
{
///
/// GenericAsyncMappedRepository is an asynchronous Data Access Object
///
///
///
///
[Obsolete("use GenericLongRepository instead")]
public sealed class GenericOldMappedIntRepository : IGenericAsyncIntRepository
where TDto : class, IIntDto, new()
where TEntity : class, IIntEntity, new()
where TDbContext : DbContext
{
private readonly IDbContextFactory _contextFactory;
private readonly ILogger> _logger;
private readonly IMapper _mapper;
public GenericOldMappedIntRepository(IDbContextFactory contextFactory,
IMapper mapper, ILogger> logger)
{
_contextFactory = contextFactory;
_mapper = mapper;
_logger = logger;
}
public async Task> GetAllAsync()
{
try
{
await using TDbContext context = _contextFactory.CreateDbContext();
List tmp = await context.Set().ToListAsync();
return _mapper.Map(tmp);
}
catch (Exception e)
{
_logger.LogError(e, "GetAllAsync");
throw;
}
}
public async Task GetByIdAsync(int id)
{
try
{
await using DbContext context = _contextFactory.CreateDbContext();
TEntity tmp = await context.Set().FindAsync(id);
return _mapper.Map(tmp);
}
catch (Exception e)
{
_logger.LogError(e, "GetByIdAsync");
throw;
}
}
public async Task> GetByIdsAsync(IEnumerable ids)
{
try
{
await using DbContext context = _contextFactory.CreateDbContext();
List entities = await context.Set().Where(s => ids.Contains(s.Id)).ToListAsync();
return _mapper.Map(entities);
}
catch (Exception e)
{
_logger.LogError(e, "GetByIdsAsync");
throw;
}
}
public async Task SaveAsync(TDto obj)
{
try
{
await using DbContext context = _contextFactory.CreateDbContext();
TEntity entity = _mapper.Map(obj);
await context.SingleMergeAsync(entity, operation =>
{
operation.InsertKeepIdentity = true;
operation.IsCheckConstraintOnInsertDisabled = false;
});
return _mapper.Map(entity);
}
catch (Exception e)
{
_logger.LogError(e, "SaveAsync");
throw;
}
}
public async Task> SaveAsync(IReadOnlyList objs)
{
try
{
List entities = _mapper.Map(objs.ToList());
await using (DbContext context = _contextFactory.CreateDbContext())
{
await context.BulkMergeAsync(entities, operation =>
{
operation.InsertKeepIdentity = true;
operation.IsCheckConstraintOnInsertDisabled = false;
});
}
return _mapper.Map(entities);
}
catch (Exception e)
{
_logger.LogError(e, $"SaveAsync {typeof(TEntity).Name}");
return Enumerable.Empty();
}
}
public async Task DeleteByIdAsync(int id)
{
try
{
await using DbContext context = _contextFactory.CreateDbContext();
var model = new TEntity { Id = id };
context.Set().Attach(model);
context.Set().Remove(model);
await context.SaveChangesAsync();
}
catch (Exception e)
{
_logger.LogError(e, "DeleteByIdAsync");
throw;
}
}
public async Task DeleteByIdsAsync(IEnumerable ids)
{
try
{
await using DbContext context = _contextFactory.CreateDbContext();
var toDelete = ids.Select(s => new TEntity { Id = s }).ToList();
context.Set().RemoveRange(toDelete);
await context.SaveChangesAsync();
}
catch (Exception e)
{
_logger.LogError(e, "DeleteByIdsAsync");
throw;
}
}
}
}