Last active
June 27, 2019 12:55
-
-
Save gustavodaquino/29729204b951a6cdcc88bf723ce26c19 to your computer and use it in GitHub Desktop.
Unit of Work - C#
This file contains hidden or 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
public interface IRepository<T> where T : BaseEntity | |
{ | |
IUnitOfWork UnitOfWork { get; } | |
IQueryable<T> All<T>() where T : BaseEntity; | |
T Find<T>(Expression<Func<T, bool>> predicate) where T : BaseEntity; | |
bool Contains<T>(Expression<Func<T, bool>> predicate) where T : BaseEntity; | |
} |
This file contains hidden or 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
public interface IUnitOfWork: IDisposable | |
{ | |
void BeginTransaction(); | |
void RollbackTransaction(); | |
void CommitTransaction(); | |
Task<bool> SaveChangesAsync(); | |
} |
This file contains hidden or 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
public class VendorDBContext : DbContext, IUnitOfWork | |
{ | |
public VendorDBContext(DbContextOptions options) : base(options) | |
{ | |
} | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
base.OnConfiguring(optionsBuilder); | |
// optionsBuilder.UseSqlServer(@"Data Source=.sqlexpress; | |
Initial Catalog=FraymsVendorDB;Integrated Security=False; User Id=sa; | |
Password=P@ssw0rd; Timeout=500000;"); | |
} | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
base.OnModelCreating(builder); | |
} | |
public void BeginTransaction() | |
{ | |
this.Database.BeginTransaction(); | |
} | |
public void RollbackTransaction() | |
{ | |
this.Database.RollbackTransaction(); | |
} | |
public void CommitTransaction() | |
{ | |
this.Database.CommitTransaction(); | |
} | |
public Task<bool> SaveChangesAsync() | |
{ | |
return this.SaveChangesAsync(); | |
} | |
public DbSet<VendorMaster> VendorMaster { get; set; } | |
public DbSet<VendorDocument> VendorDocuments { get; protected set; } |
This file contains hidden or 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
public class VendorRepository : IVendorRepository | |
{ | |
VendorDBContext _dbContext; | |
public VendorRepository(VendorDBContext dbContext) | |
{ | |
this._dbContext = dbContext; | |
} | |
public IUnitOfWork UnitOfWork | |
{ | |
get | |
{ | |
return _dbContext; | |
} | |
} | |
public VendorMaster Add(VendorMaster vendorMaster) | |
{ | |
var res= _dbContext.Add(vendorMaster); | |
return res.Entity; | |
} | |
public void AddDocument(VendorDocument vendorDocument) | |
{ | |
var res = _dbContext.Add(vendorDocument); | |
} | |
public void Update(VendorMaster vendorMaster) | |
{ | |
_dbContext.Entry(vendorMaster).State = Microsoft.EntityFrameworkCore.EntityState.Modified; | |
} | |
public async Task<VendorMaster> GetAsync(int vendorID) | |
{ | |
var vendorMaster = await _dbContext.VendorMaster.FindAsync(vendorID); | |
if (vendorMaster != null) | |
{ | |
await _dbContext.Entry(vendorMaster) | |
.Collection(i => i.VendorDocuments).LoadAsync(); | |
} | |
return vendorMaster; | |
} | |
public IQueryable<T> All<T>() where T : BaseEntity | |
{ | |
return _dbContext.Set<T>().AsQueryable(); | |
} | |
public bool Contains<T>(Expression<Func<T, bool>> predicate) where T : BaseEntity | |
{ | |
return _dbContext.Set<T>().Count<T>(predicate) > 0; | |
} | |
public T Find<T>(Expression<Func<T, bool>> predicate) where T : BaseEntity | |
{ | |
return _dbContext.Set<T>().FirstOrDefault<T>(predicate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment