Skip to content

Instantly share code, notes, and snippets.

@ognyandim
Created July 21, 2017 08:04
Show Gist options
  • Save ognyandim/7cbd6a0f9f19273d2a19f3985f229f52 to your computer and use it in GitHub Desktop.
Save ognyandim/7cbd6a0f9f19273d2a19f3985f229f52 to your computer and use it in GitHub Desktop.
Unit of work and repo working in tandem.
public class EntityService : IEntityService
{
public EntityService(
IUnitOfWorkFactory unitOfWorkFactory,
IRepository<Entity> repository,
IRepository<Entity2> repository2
)
{
this._unitOfWorkFactory = unitOfWorkFactory;
//... same for other dependencies
}
public Entity[] GetItDone()
{
using (var uow = _unitOfWorkFactory.Create())
{
var entities = _entityRepository
.GetAllAsQueryable(...)
.OrderByDescending(..)
.ThenByDescending(..)
.ToArray();
//... logic
_repository.Update(...);
//... logic
// logic with repo2
_repository2.Update(...);
_repository2.Create(...);
//... other logic with repoX
// until this line the Unit Of Work as a whole :
// 1. Get this entity
// 2. Modify this if that and then create another if whatever
// 3. Send email to a@a.com or whatever
// 4. Consider the unit of work to be done by definition.
// If something explodes until this row nothing will be commited to database and the work van be retried.
// Consider it finished
uow.Commit(); // internally calls dbcontext.SaveChanges in case you are using EF.
}
return entities;
}
public interface IUnitOfWork : IDisposable
{
void Commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment