PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Events/EventMigrationHierarchyManager.cs

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