-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUnitOfWork.cs
More file actions
73 lines (61 loc) · 1.81 KB
/
Copy pathUnitOfWork.cs
File metadata and controls
73 lines (61 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GenericRepositoryUnitOfWork.Data;
namespace GenericRepositoryUnitOfWork.Repository
{
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly ApplicationDbContext _dbContext;
private readonly Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
public Dictionary<Type, object> Repositories
{
get { return _repositories; }
set { Repositories = value; }
}
public UnitOfWork(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public IRepository<T> Repository<T>() where T : class
{
if (Repositories.Keys.Contains(typeof(T)))
{
return Repositories[typeof(T)] as IRepository<T>;
}
IRepository<T> repo = new Repository<T>(_dbContext);
Repositories.Add(typeof(T), repo);
return repo;
}
public async Task<int> CommitAsync()
{
return await _dbContext.SaveChangesAsync();
}
void IUnitOfWork.Commit()
{
_dbContext.SaveChanges();
}
public void Rollback()
{
_dbContext.ChangeTracker.Entries().ToList().ForEach(x => x.Reload());
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_dbContext.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}