Aucune description

DbRepository.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // DbRepository.cs
  3. //
  4. // Author:
  5. // Bryan Allred <bryan.allred@gmail.com>
  6. //
  7. // Copyright (c) 2011 Bryan Allred
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. using NHibernate;
  29. using WhiteNoise.Domain.Abstract;
  30. namespace WhiteNoise.Domain.Concrete
  31. {
  32. /// <summary>
  33. /// Database repository.
  34. /// </summary>
  35. public abstract class DbRepository<T> : IRepository<T>
  36. {
  37. /// <summary>
  38. /// The NHibernate session.
  39. /// </summary>
  40. private readonly ISession _session;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="WhiteNoise.Domain.Concrete.DbRepository`1"/> class.
  43. /// </summary>
  44. /// <param name='session'>
  45. /// Session.
  46. /// </param>
  47. public DbRepository(ISession session)
  48. {
  49. this._session = session;
  50. }
  51. #region IRepository[T] implementation
  52. /// <summary>
  53. /// Add the specified member.
  54. /// </summary>
  55. /// <param name='member'>
  56. /// Member.
  57. /// </param>
  58. public T Add(T member)
  59. {
  60. using (ITransaction transaction = this._session.BeginTransaction())
  61. {
  62. this._session.Save(member);
  63. transaction.Commit();
  64. }
  65. return member;
  66. }
  67. /// <summary>
  68. /// Find the specified id.
  69. /// </summary>
  70. /// <param name='id'>
  71. /// Identifier.
  72. /// </param>
  73. public T Find(int id)
  74. {
  75. return this._session.Get<T>(id);
  76. }
  77. /// <summary>
  78. /// Remove the specified member.
  79. /// </summary>
  80. /// <param name='member'>
  81. /// Member.
  82. /// </param>
  83. public T Remove(T member)
  84. {
  85. using (ITransaction transaction = this._session.BeginTransaction())
  86. {
  87. this._session.Delete(member);
  88. transaction.Commit();
  89. }
  90. return member;
  91. }
  92. /// <summary>
  93. /// Update the specified member.
  94. /// </summary>
  95. /// <param name='member'>
  96. /// Member.
  97. /// </param>
  98. public T Update(T member)
  99. {
  100. using (ITransaction transaction = this._session.BeginTransaction())
  101. {
  102. this._session.Update(member);
  103. transaction.Commit();
  104. }
  105. return member;
  106. }
  107. /// <summary>
  108. /// Gets the collection.
  109. /// </summary>
  110. /// <value>
  111. /// The collection.
  112. /// </value>
  113. public IList<T> Collection
  114. {
  115. get
  116. {
  117. throw new NotImplementedException();
  118. //return this._session.QueryOver<T>.QueryOver<T>().List();
  119. }
  120. }
  121. #endregion
  122. }
  123. }