Esempio n. 1
0
        /// <summary>
        /// Tell legion to scan for message handlers that are marked with the <see cref="MessageHandlerAttribute"/>.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="assembliesToScan">The assemblies that should be scanned for message handlers (if null only entry assembly is scanned).</param>
        /// <returns></returns>
        public static IDependencyRegistrationContext ScanForMessageHandlers(
            this IDependencyRegistrationContext context,
            IEnumerable <Assembly> assembliesToScan = null)
        {
            if (assembliesToScan == null)
            {
                assembliesToScan = new[] { Assembly.GetEntryAssembly() }.Distinct();
            }

            context.RegisterTransient(
                c => new MessageDispatcher(c.Resolve <IMessageListener>(), c.Resolve <IMessageHandlerRegistry>()),
                typeof(IMessageDispatcher));

            var messageHandlerRegistry = new MessageHandlerRegistry();

            context.RegisterSingleton(c =>
            {
                messageHandlerRegistry.ScanForMessageHandlerTypes(assembliesToScan, t => context.RegisterTransient(t, typeof(IMessageHandler), t));
                var resolutionContext = c.ResolveForLater();
                messageHandlerRegistry.ConnectContainerResolution(t => (IMessageHandler)resolutionContext.Resolve(t));
                return(messageHandlerRegistry);
            });

            return(context);
        }