the GcodeFirst is tool for generator code developed by CSharp for Microsoft Entity Framework to support repository, unit of work patterns, multiple database.
he UnitOfWork and repository patterns are intended to act like a abstraction layer between business logic and data access layer. This can help insulate your application from changes in the data store and can facilitate automated unit testing / test driven development.
public class MyDbContext : DbContext
{
public virtual DbSet<Author> Authors { get; set; }
public virtual DbSet<Book> Books { get; set; }
public MyDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
}
public interface IRepository<T> where T : class
{
IQueryable<T> Entities { get; }
void Remove(T entity);
void Add(T entity);
}
public class GenericRepository<T> : IRepository<T> where T : class
{
private readonly MyDbContext _dbContext;
private IDbSet<T> _dbSet => _dbContext.Set<T>();
public IQueryable<T> Entities => _dbSet;
public GenericRepository(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public void Remove(T entity)
{
_dbSet.Remove(entity);
}
public void Add(T entity)
{
_dbSet.Add(entity);
}
}
public interface IUnitOfWork
{
IRepository<Author> AuthorRepository { get; }
IRepository<Book> BookRepository { get; }
/// <summary>
/// Commits all changes
/// </summary>
void Commit();
/// <summary>
/// Discards all changes that has not been commited
/// </summary>
void RejectChanges();
void Dispose();
}
public class UnitOfWork : IUnitOfWork
{
private readonly MyDbContext _dbContext;
#region Repositories
public IRepository<Author> AuthorRepository =>
new GenericRepository<Author>(_dbContext);
public IRepository<Book> BookRepository =>
new GenericRepository<Book>(_dbContext);
#endregion
public UnitOfWork(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public void Commit()
{
_dbContext.SaveChanges();
}
public void Dispose()
{
_dbContext.Dispose();
}
public void RejectChanges()
{
foreach (var entry in _dbContext.ChangeTracker.Entries()
.Where(e => e.State != EntityState.Unchanged))
{
switch (entry.State)
{
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Modified:
case EntityState.Deleted:
entry.Reload();
break;
}
}
}
}
var dbContext = new MyDbContext("{connectionString goes here}");
var unitOfWork = new UnitOfWork(dbContext);
var books = unitOfWork.BookRepository.Entities
.Where(n => n.Title == "Hello World");
var justOneBook = unitOfWork.BookRepository.Entities
.First(n => n.ID == 1);
// Create
var author = new Author() { Name = "Kris" };
unitOfWork.AuthorRepository.Add(author);
unitOfWork.Commit(); // Save changes to database
// Update
var author = unitOfWork.AuthorRepository.Entities
.First(n => n.Name == "Kris");
author.Name = "Dan";
unitOfWork.Commit(); // Save changes to database
// Delete
var author = unitOfWork.AuthorRepository.Entities
.First(n => n.Name == "Dan");
unitOfWork.AuthorRepository.Remove(author);
unitOfWork.Commit(); // Save changes to database
// Delete
var author = unitOfWork.AuthorRepository.Entities
.First(n => n.Name == "Dan");
unitOfWork.AuthorRepository.Remove(author);
// Ops i don't want to do this
unitOfWork.RejectChanges(); // Reject all changes
The GcodeFirst is open-sourced software licensed under the MIT license