PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Bifrost/Events/EventMigrationHierarchyManager.cs

#
C# | 115 lines | 67 code | 17 blank | 31 comment | 9 complexity | 10542d29085daef8a3d5f98ffe0c1545 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.Execution;
  26. namespace Bifrost.Events
  27. {
  28. /// <summary>
  29. /// Represents a <see cref="IEventMigrationHierarchyManager">IEventMigrationHierarchyManager</see>
  30. /// </summary>
  31. /// <remarks>
  32. /// The manager will automatically build an <see cref="EventMigrationHierarchy">EventMigrationHierarchy</see> for all events and
  33. /// allow clients to query for the current migration level for a specific logical event or the concrete type of a particular link
  34. /// in the migration chain for a logical event.
  35. /// </remarks>
  36. [Singleton]
  37. public class EventMigrationHierarchyManager : IEventMigrationHierarchyManager
  38. {
  39. private readonly IEventMigrationHierarchyDiscoverer _eventMigrationHierarchyDiscoverer;
  40. private readonly IEnumerable<EventMigrationHierarchy> _hierarchies;
  41. /// <summary>
  42. /// Initializes an instance of <see cref="EventMigrationHierarchyManager">EventMigrationHierarchyManager</see>
  43. /// </summary>
  44. /// <param name="eventMigrationHierarchyDiscoverer">IEventMigrationHierarchyDiscoverer</param>
  45. public EventMigrationHierarchyManager(IEventMigrationHierarchyDiscoverer eventMigrationHierarchyDiscoverer)
  46. {
  47. _eventMigrationHierarchyDiscoverer = eventMigrationHierarchyDiscoverer;
  48. _hierarchies = _eventMigrationHierarchyDiscoverer.GetMigrationHierarchies();
  49. }
  50. #pragma warning disable 1591 // Xml Comments
  51. public int GetCurrentMigrationLevelForLogicalEvent(Type logicalEvent)
  52. {
  53. var hierarchy = GetHierarchyForLogicalType(logicalEvent);
  54. return hierarchy.MigrationLevel;
  55. }
  56. public Type GetConcreteTypeForLogicalEventMigrationLevel(Type logicalEvent, int level)
  57. {
  58. if(level < 0)
  59. throw new MigrationLevelOutOfRangeException(string.Format("The lowest possible migration level is 0. You asked for {0}",level));
  60. var hierarchy = GetHierarchyForLogicalType(logicalEvent);
  61. Type type;
  62. try
  63. {
  64. type = hierarchy.GetConcreteTypeForLevel(level);
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new MigrationLevelOutOfRangeException(string.Format(
  69. "The maximum migration level for the logical event {0} is {1}. Does not have a migration level of {2}",
  70. logicalEvent.FullName, hierarchy.MigrationLevel, level
  71. ),ex);
  72. }
  73. return type;
  74. }
  75. public Type GetLogicalTypeForEvent(Type @event)
  76. {
  77. var hierarchy = _hierarchies.Where(h => h.MigratedTypes.Contains(@event)).FirstOrDefault();
  78. if(hierarchy == null)
  79. throw new UnregisteredEventException(string.Format("Cannot find an event migration hierarchy that contains '{0}' event type", @event.AssemblyQualifiedName));
  80. return hierarchy.LogicalEvent;
  81. }
  82. public Type GetLogicalTypeFromName(string typeName)
  83. {
  84. var hierarchy = _hierarchies.Where(h => h.LogicalEvent.Name == typeName).FirstOrDefault();
  85. if (hierarchy == null)
  86. throw new UnregisteredEventException(string.Format("Cannot find an event migration hierarchy with the logical event named '{0}'.", typeName));
  87. return hierarchy.LogicalEvent;
  88. }
  89. #pragma warning restore 1591 // Xml Comments
  90. private EventMigrationHierarchy GetHierarchyForLogicalType(Type logicalEvent)
  91. {
  92. var hierarchy = _hierarchies.Where(hal => hal.LogicalEvent == logicalEvent).FirstOrDefault();
  93. if(hierarchy == null)
  94. throw new UnregisteredEventException(string.Format("Cannot find the logical event '{0}' in the migration hierarchies.", logicalEvent.AssemblyQualifiedName));
  95. return hierarchy;
  96. }
  97. }
  98. }