PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Domain/AggregatedRootRepository.cs

#
C# | 81 lines | 48 code | 6 blank | 27 comment | 3 complexity | e6ed237e0a466daba021f2aa9e0157bc 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.Commands;
  26. using Bifrost.Events;
  27. namespace Bifrost.Domain
  28. {
  29. /// <summary>
  30. /// Defines a concrete implementation of <see cref="IAggregatedRootRepository{T}">IAggregatedRootRepository</see>
  31. /// </summary>
  32. /// <typeparam name="T">Type the repository is for</typeparam>
  33. public class AggregatedRootRepository<T> : IAggregatedRootRepository<T>
  34. where T : AggregatedRoot
  35. {
  36. readonly ICommandContextManager _commandContextManager;
  37. /// <summary>
  38. /// Initializes a new instance of <see cref="AggregatedRootRepository{T}">AggregatedRootRepository</see>
  39. /// </summary>
  40. /// <param name="commandContextManager"> <see cref="ICommandContextManager"/> to use for tracking </param>
  41. public AggregatedRootRepository(ICommandContextManager commandContextManager)
  42. {
  43. _commandContextManager = commandContextManager;
  44. }
  45. #pragma warning disable 1591 // Xml Comments
  46. public T Get(Guid id)
  47. {
  48. var commandContext = _commandContextManager.GetCurrent();
  49. var type = typeof (T);
  50. var aggregatedRoot = Activator.CreateInstance(type, id) as T;
  51. if (null != aggregatedRoot)
  52. {
  53. if(!aggregatedRoot.IsStateless())
  54. {
  55. foreach (var stream in commandContext.EventStores.Select(eventStore => eventStore.Load(type, id)).Where(stream => stream.HasEvents))
  56. {
  57. aggregatedRoot.ReApply(stream);
  58. }
  59. }
  60. else
  61. {
  62. var versions = commandContext.EventStores.Select(eventStore => eventStore.GetLastCommittedVersion(type, id)).ToList();
  63. aggregatedRoot.FastForward(versions.Max());
  64. }
  65. }
  66. commandContext.RegisterForTracking(aggregatedRoot);
  67. return aggregatedRoot;
  68. }
  69. object IAggregatedRootRepository.Get(Guid id)
  70. {
  71. return Get(id);
  72. }
  73. #pragma warning restore 1591 // Xml Comments
  74. }
  75. }