PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost/Sagas/SagaLibrarian.cs

#
C# | 111 lines | 72 code | 12 blank | 27 comment | 8 complexity | 171e098e2a9bf55be772cf2cc20eaf37 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. #region License
  2. //
  3. // Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
  4. //
  5. // Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
  6. // With one exception :
  7. // Commercial libraries that is based partly or fully on Bifrost and is sold commercially,
  8. // must obtain a commercial license.
  9. //
  10. // You may not use this file except in compliance with the License.
  11. // You may obtain a copy of the license at
  12. //
  13. // http://bifrost.codeplex.com/license
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. #endregion
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using Bifrost.Entities;
  26. namespace Bifrost.Sagas
  27. {
  28. /// <summary>
  29. /// Represents a <see cref="ISagaLibrarian"/>
  30. /// </summary>
  31. public class SagaLibrarian : ISagaLibrarian
  32. {
  33. readonly IEntityContext<SagaHolder> _entityContext;
  34. readonly ISagaConverter _sagaConverter;
  35. /// <summary>
  36. /// Initializes a new instance of <see cref="SagaLibrarian"/>
  37. /// </summary>
  38. /// <param name="entityContext">A <see cref="IEntityContext{SagaHolder}"/> to use for working with persisting and resuming <see cref="ISaga">Sagas</see></param>
  39. /// <param name="sagaConverter">A <see cref="ISagaConverter"/> for converting a <see cref="ISaga"/> to a <see cref="SagaHolder"/> and back</param>
  40. public SagaLibrarian(IEntityContext<SagaHolder> entityContext, ISagaConverter sagaConverter)
  41. {
  42. _entityContext = entityContext;
  43. _sagaConverter = sagaConverter;
  44. }
  45. #pragma warning disable 1591 // Xml Comments
  46. public void Close(ISaga saga)
  47. {
  48. var sagaHolder = _sagaConverter.ToSagaHolder(saga);
  49. _entityContext.Delete(sagaHolder);
  50. _entityContext.Commit();
  51. }
  52. public void Catalogue(ISaga saga)
  53. {
  54. var sagaHolder = GetExistingIfAny(saga.Id);
  55. if (sagaHolder == null)
  56. {
  57. sagaHolder = _sagaConverter.ToSagaHolder(saga);
  58. _entityContext.Insert(sagaHolder);
  59. }
  60. else
  61. {
  62. _sagaConverter.Populate(sagaHolder, saga);
  63. _entityContext.Update(sagaHolder);
  64. }
  65. _entityContext.Commit();
  66. }
  67. public ISaga Get(Guid id)
  68. {
  69. var sagaHolder = (from s in _entityContext.Entities
  70. where s.Id == id
  71. select s).SingleOrDefault();
  72. var saga = _sagaConverter.ToSaga(sagaHolder);
  73. return saga;
  74. }
  75. public ISaga Get(string partition, string key)
  76. {
  77. var query = from s in _entityContext.Entities
  78. where s.Partition == partition && s.Key == key
  79. select _sagaConverter.ToSaga(s);
  80. return query.SingleOrDefault();
  81. }
  82. public IEnumerable<ISaga> GetForPartition(string partition)
  83. {
  84. var sagaHolders = (from s in _entityContext.Entities
  85. where s.Partition == partition
  86. select s).ToArray();
  87. var sagas = sagaHolders.Select(_sagaConverter.ToSaga);
  88. return sagas;
  89. }
  90. #pragma warning restore 1591 // Xml Comments
  91. SagaHolder GetExistingIfAny(Guid id)
  92. {
  93. var sagaHolder = (from s in _entityContext.Entities
  94. where s.Id == id
  95. select s).SingleOrDefault();
  96. return sagaHolder;
  97. }
  98. }
  99. }