Skip to content

Commit

Permalink
chors: unit of work
Browse files Browse the repository at this point in the history
  • Loading branch information
mtai0524 committed Nov 22, 2024
1 parent 9edf7a3 commit ec1828e
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 6 deletions.
31 changes: 29 additions & 2 deletions NotaionWebApp/Notaion.Application/Services/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class ChatService : IChatService
private readonly IGenericRepository<Chat> _chatGenericRepository; // need use unit of work , cuz dont use Chat entity in Application layer
private readonly IChatRepository _chatRepository;
private readonly IMapper _mapper;
public ChatService(IGenericRepository<Chat> chatGenericRepository, IMapper mapper, IChatRepository chatRepository)
private readonly IUnitOfWork _unitOfWork;
public ChatService(IUnitOfWork unitOfWork, IGenericRepository<Chat> chatGenericRepository, IMapper mapper, IChatRepository chatRepository)
{
_chatGenericRepository = chatGenericRepository;
_mapper = mapper;
_chatRepository = chatRepository;
_unitOfWork = unitOfWork;
}

public async Task<ChatResponseDto> CreateChatAsync(CreateChatDto chatDto)
Expand All @@ -41,12 +43,37 @@ public async Task<ChatResponseDto> CreateChatAsync(CreateChatDto chatDto)

return response;
}

/*
* uow generic call repo not pass entity
*/
public async Task<List<ChatResponseDto>> GetChatsAsync()
{
var chats = await _chatRepository.GetAllAsync();
var chats = await _unitOfWork.ChatRepository.GetAllAsync();
return _mapper.Map<List<ChatResponseDto>>(chats);
}


/*
* uow generic param entity
*/
//public async Task<List<ChatResponseDto>> GetChatsAsync()
//{
// var chats = await _unitOfWork.GetGenericRepository<Chat>().GetAllAsync();
// return _mapper.Map<List<ChatResponseDto>>(chats);
//}


/*
* generic repo
*/

//public async Task<List<ChatResponseDto>> GetChatsAsync()
//{
// var chats = await _chatRepository.GetAllAsync();
// return _mapper.Map<List<ChatResponseDto>>(chats);
//}

public async Task<List<ChatResponseDto>> GetChatsHiddenAsync()
{
var chats = await _chatGenericRepository.GetAllAsync();
Expand Down
19 changes: 19 additions & 0 deletions NotaionWebApp/Notaion.Domain/Interfaces/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Domain.Interfaces
{
public interface IUnitOfWork : IDisposable
{
public IChatRepository ChatRepository { get; }

IGenericRepository<T> GetGenericRepository <T>() where T : class;
Task<int> SaveChangeAsync();
Task BeginTransactionAsync();
Task CommitTransactionAsync();
Task RollBackAsync();
}
}
3 changes: 3 additions & 0 deletions NotaionWebApp/Notaion.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
// generic repo
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));

// uow
services.AddScoped<IUnitOfWork, UnitOfWork>();

// account user
services.AddScoped<IAccountRepository, AccountRepository>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ namespace Notaion.Infrastructure.Repositories
public class ChatRepository : GenericRepository<Chat>, IChatRepository
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
public ChatRepository(ApplicationDbContext context, IMapper mapper) : base(context)
public ChatRepository(ApplicationDbContext context) : base(context)
{
_context = context;
_mapper = mapper;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task DeleteAsync(T entity)
await _context.SaveChangesAsync();
}

public async Task<IEnumerable<T>> GetAllAsync()
public async Task<IEnumerable<T>> GetAllAsync() // _context.Chat.ToListAsync()
{
return await _dbSet.ToListAsync();
}
Expand Down
92 changes: 92 additions & 0 deletions NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.EntityFrameworkCore.Storage;
using Notaion.Domain.Interfaces;
using Notaion.Infrastructure.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Notaion.Infrastructure.Repositories
{
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
private IDbContextTransaction _transaction;
private readonly Dictionary<Type, object> _repositories;
public IChatRepository ChatRepository { get; }
public UnitOfWork(ApplicationDbContext context)
{
_context = context;
_repositories = new Dictionary<Type, object>();
ChatRepository = new ChatRepository(_context);
}
public async Task BeginTransactionAsync()
{
_transaction = await _context.Database.BeginTransactionAsync();
}

public async Task CommitTransactionAsync()
{
try
{
await _transaction.CommitAsync();
}
catch
{
await _transaction.RollbackAsync();
return;
}
finally
{
await _transaction.DisposeAsync();
_transaction = null!;
}

}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool dispose = false;



protected virtual void Dispose(bool disposing)
{
if(!this.dispose)
{
if (disposing)
{
_context.Dispose();
}
this.dispose = true;
}
}

public IGenericRepository<T> GetGenericRepository<T>() where T : class
{
if (_repositories.ContainsKey(typeof(T)))
{
return _repositories[typeof(T)] as IGenericRepository<T>;
}
var repository = new GenericRepository<T>(_context);
_repositories.Add(typeof(T), repository);
return repository;
}

public async Task RollBackAsync()
{
await _transaction.RollbackAsync();
await _transaction.DisposeAsync();
_transaction = null!;
}

public Task<int> SaveChangeAsync()
{
return _context.SaveChangesAsync();
}
}
}

0 comments on commit ec1828e

Please sign in to comment.