-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
NotaionWebApp/Notaion.Infrastructure/Repositories/UnitOfWork.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |