/// <summary> /// Adds an event listener configuration /// </summary> /// <param name="config">the configuration to add</param> /// <returns>the added configuration</returns> /// <exception cref="ArgumentNullException">When the configuration is null</exception> /// <exception cref="ArgumentException">When the configuration is already present.</exception> public EventListenerConfig Add(EventListenerConfig config) { if (config == null) { throw new ArgumentNullException("config"); } if (listeners.ContainsKey(config.ListenerType)) { throw new ArgumentException(string.Format("Configuration for Listener Type {0} is already present.", config.ListenerType.FullName), "config"); } var events = GetEventTypes(config.ListenerType); if (events.Length == 0) { throw new ArgumentException(string.Format("The Listener of type {0} does not implement any known NHibernate event listener interfaces.", config.ListenerType.FullName), "config"); } listeners.Add(config.ListenerType, config); foreach (var eventType in events) { if (!listenersPerEvent.ContainsKey(eventType)) { listenersPerEvent.Add(eventType, new HashedSet <Type>()); } listenersPerEvent[eventType].Add(config.ListenerType); } return(config); }
private static void RegisterEventListeners(IEnumerable <Type> types) { var contributor = new EventListenerContributor(); foreach (var type in types) { var eventListenerAttributes = type.GetCustomAttributes(typeof(EventListenerAttribute), false); if (eventListenerAttributes.Length == 1) { var attribute = (EventListenerAttribute)eventListenerAttributes[0]; var config = new EventListenerConfig(type) { ReplaceExisting = attribute.ReplaceExisting, Ignore = attribute.Ignore, SkipEvent = attribute.SkipEvent, Singleton = attribute.Singleton, Include = attribute.Include, Exclude = attribute.Exclude }; contributor.Add(config); } } if (EventListenerComponentRegistrationHook != null) { EventListenerComponentRegistrationHook(contributor); } var addEventListenerAttributes = new List <EventListenerAssemblyAttribute>(); var ignoreEventListenerAttributes = new List <EventListenerAssemblyAttribute>(); foreach (var assembly in registeredAssemblies) { addEventListenerAttributes.AddRange( (AddEventListenerAttribute[])assembly.GetCustomAttributes(typeof(AddEventListenerAttribute), false)); ignoreEventListenerAttributes.AddRange((IgnoreEventListenerAttribute[])assembly.GetCustomAttributes(typeof(IgnoreEventListenerAttribute), false)); } ProcessEventListenerAssemblyAttributes(contributor, ignoreEventListenerAttributes); ProcessEventListenerAssemblyAttributes(contributor, addEventListenerAttributes); if (EventListenerFacilityConfigurationHook != null) { EventListenerFacilityConfigurationHook(contributor); } AddContributor(contributor); }
/// <summary> /// Adds an event listener configuration /// </summary> /// <param name="config">the configuration to add</param> /// <returns>the added configuration</returns> /// <exception cref="ArgumentNullException">When the configuration is null</exception> /// <exception cref="ArgumentException">When the configuration is already present.</exception> public EventListenerConfig Add(EventListenerConfig config) { if (config == null) throw new ArgumentNullException("config"); if (listeners.ContainsKey(config.ListenerType)) throw new ArgumentException(string.Format("Configuration for Listener Type {0} is already present.", config.ListenerType.FullName), "config"); var events = GetEventTypes(config.ListenerType); if (events.Length == 0) throw new ArgumentException(string.Format("The Listener of type {0} does not implement any known NHibernate event listener interfaces.", config.ListenerType.FullName), "config"); listeners.Add(config.ListenerType, config); foreach (var eventType in events) { if (!listenersPerEvent.ContainsKey(eventType)) listenersPerEvent.Add(eventType, new HashedSet<Type>()); listenersPerEvent[eventType].Add(config.ListenerType); } return config; }
private static void ConfigureEventListener(EventListenerAssemblyAttribute attribute, EventListenerConfig config) { if (attribute is IgnoreEventListenerAttribute) { config.Ignore = true; } else { config.Ignore = false; var addAttribute = (AddEventListenerAttribute)attribute; config.Exclude = addAttribute.Exclude; config.Include = addAttribute.Include; config.SkipEvent = addAttribute.ExcludeEvent; if (addAttribute.IncludeEvent != null) { Type[] eventtypes = EventListenerContributor.GetEventTypes(config.ListenerType); config.SkipEvent = Array.FindAll(eventtypes, type => Array.IndexOf(addAttribute.IncludeEvent, type) < 0); } config.Singleton = addAttribute.Singleton; config.ReplaceExisting = addAttribute.ReplaceExisting; } }
private static object GetInstance(EventListenerConfig config) { return(config.ListenerInstance ?? Activator.CreateInstance(config.ListenerType)); }
private static object GetInstance(EventListenerConfig config) { return config.ListenerInstance ?? Activator.CreateInstance(config.ListenerType); }