49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using CleanArchitecture.Application.Contracts.Persistence;
|
|
using CleanArchitecture.Domain.Common;
|
|
using CleanArchitecture.Infrastructure.Repositories;
|
|
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(RepositoryBase<>);
|
|
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T)), context);
|
|
repositories.Add(type, repositoryInstance);
|
|
}
|
|
|
|
return (IAsyncRepository<T>)repositories[type];
|
|
|
|
}
|
|
}
|
|
}
|