Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public class EntityService : IEntityService
  2. {
  3. public EntityService(
  4. IUnitOfWorkFactory unitOfWorkFactory,
  5. IRepository<Entity> repository,
  6. IRepository<Entity2> repository2
  7. )
  8. {
  9. this._unitOfWorkFactory = unitOfWorkFactory;
  10. //... same for other dependencies
  11. }
  12.  
  13. public Entity[] GetItDone()
  14. {
  15. using (var uow = _unitOfWorkFactory.Create())
  16. {
  17. var entities = _entityRepository
  18. .GetAllAsQueryable(...)
  19. .OrderByDescending(..)
  20. .ThenByDescending(..)
  21. .ToArray();
  22.  
  23. //... logic
  24. _repository.Update(...);
  25.  
  26. //... logic
  27. // logic with repo2
  28. _repository2.Update(...);
  29. _repository2.Create(...);
  30.  
  31. //... other logic with repoX
  32.  
  33. // until this line the Unit Of Work as a whole :
  34. // 1. Get this entity
  35. // 2. Modify this if that and then create another if whatever
  36. // 3. Send email to a@a.com or whatever
  37. // 4. Consider the unit of work to be done by definition.
  38. // If something explodes until this row nothing will be commited to database and the work van be retried.
  39.  
  40. // Consider it finished
  41. uow.Commit(); // internally calls dbcontext.SaveChanges in case you are using EF.
  42. }
  43. return entities;
  44. }
  45.  
  46. public interface IUnitOfWork : IDisposable
  47. {
  48. void Commit();
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement