Skip to content

Repository files navigation

About GcodeFirst

the GcodeFirst is tool for generator code developed by CSharp for Microsoft Entity Framework to support repository, unit of work patterns, multiple database.

Why the UnitOfWork and Repository pattern?

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.

Example generator code

DbContext:

publicclassMyDbContext:DbContext{publicvirtualDbSet<Author>Authors{get;set;}publicvirtualDbSet<Book>Books{get;set;}publicMyDbContext(stringnameOrConnectionString):base(nameOrConnectionString){}}

The Generic Repository:

publicinterfaceIRepository<T>whereT:class{IQueryable<T>Entities{get;}voidRemove(Tentity);voidAdd(Tentity);}publicclassGenericRepository<T>:IRepository<T>whereT:class{privatereadonlyMyDbContext_dbContext;privateIDbSet<T>_dbSet=>_dbContext.Set<T>();publicIQueryable<T>Entities=>_dbSet;publicGenericRepository(MyDbContextdbContext){_dbContext=dbContext;}publicvoidRemove(Tentity){_dbSet.Remove(entity);}publicvoidAdd(Tentity){_dbSet.Add(entity);}}

The UnitOfWork

publicinterfaceIUnitOfWork{IRepository<Author>AuthorRepository{get;}IRepository<Book>BookRepository{get;}/// <summary>/// Commits all changes/// </summary>voidCommit();/// <summary>/// Discards all changes that has not been commited/// </summary>voidRejectChanges();voidDispose();}publicclassUnitOfWork:IUnitOfWork{privatereadonlyMyDbContext_dbContext;
#region Repositories
publicIRepository<Author>AuthorRepository=>newGenericRepository<Author>(_dbContext);publicIRepository<Book>BookRepository=>newGenericRepository<Book>(_dbContext);
#endregion
publicUnitOfWork(MyDbContextdbContext){_dbContext=dbContext;}publicvoidCommit(){_dbContext.SaveChanges();}publicvoidDispose(){_dbContext.Dispose();}publicvoidRejectChanges(){foreach(varentryin_dbContext.ChangeTracker.Entries().Where(e =>e.State!=EntityState.Unchanged)){switch(entry.State){caseEntityState.Added:entry.State=EntityState.Detached;break;caseEntityState.Modified:caseEntityState.Deleted:entry.Reload();break;}}}}

Usage

vardbContext=newMyDbContext("{connectionString goes here}");varunitOfWork=newUnitOfWork(dbContext);

Selecting some entities

varbooks=unitOfWork.BookRepository.Entities.Where(n =>n.Title=="Hello World");varjustOneBook=unitOfWork.BookRepository.Entities.First(n =>n.ID==1);

Create

// Createvarauthor=newAuthor(){Name="Kris"};unitOfWork.AuthorRepository.Add(author);unitOfWork.Commit();// Save changes to database

Update

// Updatevarauthor=unitOfWork.AuthorRepository.Entities.First(n =>n.Name=="Kris");author.Name="Dan";unitOfWork.Commit();// Save changes to database

Delete

// Deletevarauthor=unitOfWork.AuthorRepository.Entities.First(n =>n.Name=="Dan");unitOfWork.AuthorRepository.Remove(author);unitOfWork.Commit();// Save changes to database

Reject Changes

// Deletevarauthor=unitOfWork.AuthorRepository.Entities.First(n =>n.Name=="Dan");unitOfWork.AuthorRepository.Remove(author);// Ops i don't want to do thisunitOfWork.RejectChanges();// Reject all changes

Screenshots

License

The GcodeFirst is open-sourced software licensed under the MIT license

About

it is tool for generator code for Microsoft Entity Framework to support repository, unit of work patterns, multiple database.

Topics

Resources

Stars

7 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages