using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TaskManager.Entity; using TaskManager.EntityFramework; namespace TaskManager.EntityFramework.Repository { public interface ITaskUnitOfWork : IDisposable { IRepository GetRepository() where TEntity : BaseEntity; Task SaveChangesAsync(); } public class TaskUnitOfWork : ITaskUnitOfWork { private readonly JobDbContext _context; private readonly Dictionary _repositories = new(); public TaskUnitOfWork(JobDbContext context) { _context = context; } public IRepository GetRepository() where TEntity : BaseEntity { if (_repositories.TryGetValue(typeof(TEntity), out var repo)) { return (IRepository)repo; } var newRepo = new Repository(_context); _repositories[typeof(TEntity)] = newRepo; return newRepo; } public async Task SaveChangesAsync() { return await _context.SaveChangesAsync(); } public void Dispose() { _context.Dispose(); } } }