Advertisement
Guest User

Untitled

a guest
Jan 28th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. public class UnitOfWork : IUnitOfWork
  2. {
  3. public DbContext Context { get; private set; }
  4.  
  5. public UnitOfWork(DbContext context)
  6. {
  7. Context = context;
  8. }
  9.  
  10. public void Commit()
  11. {
  12. if(Context != null)
  13. {
  14. try
  15. {
  16. Context.SaveChanges();
  17. }
  18. catch(DbEntityValidationException exc)
  19. {
  20. foreach (var eve in exc.EntityValidationErrors)
  21. {
  22. Debug.WriteLine("Entity of type "{0}" in state "{1}" has the following validation errors:",
  23. eve.Entry.Entity.GetType().Name, eve.Entry.State);
  24. foreach (var ve in eve.ValidationErrors)
  25. {
  26. Debug.WriteLine("- Property: "{0}", Error: "{1}"",
  27. ve.PropertyName, ve.ErrorMessage);
  28. }
  29. }
  30.  
  31. throw;
  32. }
  33. }
  34. }
  35.  
  36. public void Dispose()
  37. {
  38. Dispose(true);
  39. GC.SuppressFinalize(this);
  40. }
  41.  
  42. public void Dispose(bool disposing)
  43. {
  44. if(!disposed)
  45. {
  46. if(disposing)
  47. {
  48. // free managed objects
  49. }
  50.  
  51. // free unmanaged objects
  52.  
  53. Context?.Dispose();
  54.  
  55. disposed = true;
  56. }
  57. }
  58.  
  59. ~UnitOfWork()
  60. {
  61. if(!disposed)
  62. {
  63. Dispose(false);
  64. }
  65. }
  66.  
  67. private bool disposed = false;
  68. }
  69.  
  70. public class UserRepository : IUserRepository
  71. {
  72. public UserRepository(DbContext context)
  73. {
  74. this.context = context;
  75. this.unitOfWork = new UnitOfWork(context);
  76. }
  77.  
  78. public void Create(DalUser entity)
  79. {
  80. if (entity == null)
  81. throw new ArgumentNullException(nameof(entity));
  82.  
  83. User ormUser = entity.ToOrmUser();
  84.  
  85. context?.Set<User>().Add(ormUser);
  86. unitOfWork.Commit();
  87. }
  88.  
  89. public void Update(DalUser entity)
  90. {
  91. if (entity == null)
  92. throw new ArgumentNullException(nameof(entity));
  93.  
  94. User ormUser = context?.Set<User>().FirstOrDefault(u => u.Id == entity.Id);
  95.  
  96. if(ormUser != null)
  97. {
  98. ormUser.Nickname = entity.Nickname;
  99. ormUser.Password = entity.Password;
  100. }
  101.  
  102. unitOfWork.Commit();
  103. }
  104.  
  105. public void Delete(DalUser entity)
  106. {
  107. if (entity == null)
  108. throw new ArgumentNullException(nameof(entity));
  109.  
  110. User ormUser = context?.Set<User>().FirstOrDefault(u => u.Id == entity.Id);
  111.  
  112. if (ormUser != null)
  113. context?.Set<User>().Remove(ormUser);
  114.  
  115. unitOfWork.Commit();
  116. }
  117.  
  118. public IEnumerable<DalUser> GetAll() => context?.Set<User>().ToList().Select(u => u.ToDalUser());
  119.  
  120. public DalUser GetById(int id) => context?.Set<User>().FirstOrDefault(u => u.Id == id)?.ToDalUser();
  121.  
  122. public DalUser GetUserByNickname(string nickname) => context?.Set<User>().FirstOrDefault(u => u.Nickname == nickname)?.ToDalUser();
  123.  
  124. private readonly DbContext context;
  125. private readonly IUnitOfWork unitOfWork;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement