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;
        }

输出:

634379-20180320160554774-1546143503.png

转载于:https://www.cnblogs.com/q975261413/p/8609861.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值