Пример #1
0
        /// <summary>
        /// Adds Ears to a specified service collection.
        /// </summary>
        /// <param name="serviceCollection">The service collection to add the project to.</param>
        /// <param name="eventManagerOptions">Options to configure the event manager.</param>
        /// <param name="autoDiscoverLoggingFactory">
        /// Whether to auto discovery a logging factory from the service container instead of it having to be manually
        /// specified.
        /// </param>
        /// <returns>The service collection with Ears added.</returns>
        public static IServiceCollection AddEars(
            this IServiceCollection serviceCollection,
            EventManagerOptions eventManagerOptions,
            bool autoDiscoverLoggingFactory = false
            )
        {
            EventManager BuildEventManager(IServiceProvider serviceProvider)
            {
                if (autoDiscoverLoggingFactory)
                {
                    eventManagerOptions.LoggerFactory = serviceProvider.GetService <ILoggerFactory>();
                }

                return(new EventManager(eventManagerOptions, serviceProvider));
            }

            if (eventManagerOptions?.AssembliesToSearch != null)
            {
                var listeners = eventManagerOptions.AssembliesToSearch.GetDispatchersAndListeners();
                foreach (var(listener, _) in listeners)
                {
                    serviceCollection.AddTransient(listener);
                }
            }

            return(serviceCollection
                   .AddSingleton <IEventManager, EventManager>(BuildEventManager));
        }
Пример #2
0
        /// <summary>
        /// Initialises a new instance of the <see cref="EventManager"/> class with a set of options.
        /// </summary>
        /// <param name="eventManagerOptions">The options used to configure the event manager.</param>
        /// <param name="serviceProvider">Provider used to resolve dependencies.</param>
        internal EventManager(EventManagerOptions eventManagerOptions, IServiceProvider serviceProvider)
        {
            _eventManagerOptions = eventManagerOptions;
            _serviceProvider     = serviceProvider;

            if (eventManagerOptions == null)
            {
                throw new ArgumentNullException(nameof(eventManagerOptions));
            }
            if (eventManagerOptions.AssembliesToSearch == null)
            {
                throw new NoConfiguredAssembliesException();
            }

            if (eventManagerOptions.AutoDiscoverListeners)
            {
                var listeners = eventManagerOptions.AssembliesToSearch.GetDispatchersAndListeners();
                foreach (var(listener, ofEvent) in listeners)
                {
                    RegisterListeners(ofEvent, listener);
                }
            }

            if (_eventManagerOptions.LoggerFactory != null)
            {
                _logger = _eventManagerOptions.LoggerFactory.CreateLogger <EventManager>();
            }
        }