Permalink
Branch: master
Find file Copy path
5948403 Oct 10, 2018
1 contributor

Users who have contributed to this file

56 lines (45 sloc) 1.9 KB
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace Threenine.Data
{
public class UnitOfWork<TContext> : IRepositoryFactory, IUnitOfWork<TContext>, IUnitOfWork
where TContext : DbContext, IDisposable
{
private Dictionary<Type, object> _repositories;
public UnitOfWork(TContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
}
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
if (_repositories == null) _repositories = new Dictionary<Type, object>();
var type = typeof(TEntity);
if (!_repositories.ContainsKey(type)) _repositories[type] = new Repository<TEntity>(Context);
return (IRepository<TEntity>) _repositories[type];
}
public IRepositoryAsync<TEntity> GetRepositoryAsync<TEntity>() where TEntity : class
{
if (_repositories == null) _repositories = new Dictionary<Type, object>();
var type = typeof(TEntity);
if (!_repositories.ContainsKey(type)) _repositories[type] = new RepositoryAsync<TEntity>(Context);
return (IRepositoryAsync<TEntity>) _repositories[type];
}
public IRepositoryReadOnly<TEntity> GetReadOnlyRepository<TEntity>() where TEntity : class
{
if (_repositories == null) _repositories = new Dictionary<Type, object>();
var type = typeof(TEntity);
if (!_repositories.ContainsKey(type)) _repositories[type] = new RepositoryReadOnly<TEntity>(Context);
return (IRepositoryReadOnly<TEntity>) _repositories[type];
}
public TContext Context { get; }
public int SaveChanges()
{
return Context.SaveChanges();
}
public void Dispose()
{
Context?.Dispose();
}
}
}