示例#1
0
        private static IMediator BuildMediator()
        {
            var container = new Container(cfg =>
            {
                cfg.Scan(scanner =>
                {
                    scanner.AssemblyContainingType<Ping>();
                    scanner.AssemblyContainingType<IMediator>();
                    scanner.WithDefaultConventions();
                    scanner.AddAllTypesOf(typeof(IRequestHandler<,>));
                    scanner.AddAllTypesOf(typeof(IAsyncRequestHandler<,>));
                    scanner.AddAllTypesOf(typeof(INotificationHandler<>));
                    scanner.AddAllTypesOf(typeof(IAsyncNotificationHandler<>));
                });
                cfg.For<TextWriter>().Use(Console.Out);
            });

            var serviceLocator = new StructureMapServiceLocator(container);
            var serviceLocatorProvider = new ServiceLocatorProvider(() => serviceLocator);
            container.Configure(cfg => cfg.For<ServiceLocatorProvider>().Use(serviceLocatorProvider));

            var mediator = serviceLocator.GetInstance<IMediator>();

            return mediator;
        }
示例#2
0
文件: Program.cs 项目: EdsonF/MediatR
        private static IMediator BuildMediator()
        {
            var container = new Container(cfg =>
            {
                cfg.Scan(scanner =>
                {
                    scanner.AssemblyContainingType <Ping>();
                    scanner.AssemblyContainingType <IMediator>();
                    scanner.WithDefaultConventions();
                    scanner.AddAllTypesOf(typeof(IRequestHandler <,>));
                    scanner.AddAllTypesOf(typeof(IAsyncRequestHandler <,>));
                    scanner.AddAllTypesOf(typeof(INotificationHandler <>));
                    scanner.AddAllTypesOf(typeof(IAsyncNotificationHandler <>));
                });
                cfg.For <TextWriter>().Use(Console.Out);
            });

            var serviceLocator         = new StructureMapServiceLocator(container);
            var serviceLocatorProvider = new ServiceLocatorProvider(() => serviceLocator);

            container.Configure(cfg => cfg.For <ServiceLocatorProvider>().Use(serviceLocatorProvider));

            var mediator = serviceLocator.GetInstance <IMediator>();

            return(mediator);
        }