Mediator 基于内存的发布订阅

Github Mediator

使用方法

    /// <summary>
    /// 返回值 BaseEntity
    /// </summary>
    public class Ping1 : IRequest<BaseEntity>
    {
        public string Name { get; set; }
        public int Val { get; set; }
    }

    /// <summary>
    /// 不带返回值
    /// </summary>
    public class Ping2 : IRequest
    {
        public string Name { get; set; }
        public int Val { get; set; }
    }

    public class BaseEntity
    {
        public bool Flag { get; set; }
        public int Val { get; set; }
    }
    public class PingHandler1 : IRequestHandler<Ping1, BaseEntity>
    {
        public Task<BaseEntity> Handle(Ping1 request, CancellationToken cancellationToken)
        {
            Console.WriteLine("传入值 {0} .带有返回值", request.Val);
            return Task.FromResult(new BaseEntity() { Val = request.Val * 2 });
        }
    }
    public class PingHandler2 : IRequestHandler<Ping2>
    {
        public Task Handle(Ping2 message, CancellationToken cancellationToken)
        {
            Console.WriteLine("传入值 {0} .不带返回值", message.Val);
            return Task.FromResult("");
        }
    }
        static void Main(string[] args)
        {

            DoAsync();
            Console.ReadLine();
        }
        public static async void DoAsync()
        {
            var mediator = BuildMediator();

            var response = await mediator.Send(new Ping1 { Val = 666 });

            Console.WriteLine("传出值 {0} .", response.Val);

            await mediator.Send(new Ping2 { Val = 666 });

            Console.ReadLine();
        }

        private static IMediator BuildMediator()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();//注册IMediator自身的组件

            var mediatrOpenTypes = new[]
            {
                typeof(IRequestHandler<,>),
                typeof(IRequestHandler<>),
                typeof(INotificationHandler<>),
            };

            foreach (var mediatrOpenType in mediatrOpenTypes)
            {
                //
                //builder
                //    .RegisterAssemblyTypes(typeof(Ping).GetTypeInfo().Assembly)
                //    .AsClosedTypesOf(mediatrOpenType)
                //    .AsImplementedInterfaces();
                builder
                .RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .AsClosedTypesOf(mediatrOpenType)
                .AsImplementedInterfaces();
            }

            // It appears Autofac returns the last registered types first 显示指定泛型解析
            //builder.RegisterGeneric(typeof(RequestPostProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
            //builder.RegisterGeneric(typeof(RequestPreProcessorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
            //builder.RegisterGeneric(typeof(GenericRequestPreProcessor<>)).As(typeof(IRequestPreProcessor<>));
            //builder.RegisterGeneric(typeof(GenericRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>));
            //builder.RegisterGeneric(typeof(GenericPipelineBehavior<,>)).As(typeof(IPipelineBehavior<,>));
            //builder.RegisterGeneric(typeof(ConstrainedRequestPostProcessor<,>)).As(typeof(IRequestPostProcessor<,>));
            //builder.RegisterGeneric(typeof(ConstrainedPingedHandler<>)).As(typeof(INotificationHandler<>));

            builder.Register<SingleInstanceFactory>(ctx =>
            {
                var c = ctx.Resolve<IComponentContext>();
                return t => c.Resolve(t);
            });

            builder.Register<MultiInstanceFactory>(ctx =>
            {
                var c = ctx.Resolve<IComponentContext>();
                return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
            });

            var container = builder.Build();

            // The below returns:
            //  - RequestPreProcessorBehavior
            //  - RequestPostProcessorBehavior
            //  - GenericPipelineBehavior

            //var behaviors = container
            //    .Resolve<IEnumerable<IPipelineBehavior<Ping, Pong>>>()
            //    .ToList();

            var mediator = container.Resolve<IMediator>();

            return mediator;
        }

输出:

posted @ 2018-03-20 16:08  the boy、图样图森破  阅读(1438)  评论(0编辑  收藏  举报