心澄欲遣

不践迹,亦不入于室

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

单播模式

NuGet引用库

MediatR 
MediatR.Extensions.Microsoft.DependencyInjection
Microsoft.Extensions.DependencyInjection
Newtonsoft.Json 非必须,用来测试使用

创建测试实体类

   public class User
    {
        public string Name { get; set; }
    }

 

1 封装命令和查询消息接口

   /// <summary>
    /// 命令消息接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ICommand<T> : IRequest<T>
    {
    }
    /// <summary>
    /// 查询消息接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IQuery<T>:IRequest<T>
    {
    }

2 创建单播请求命令和查询消息

    /// <summary>
    /// 创建单播命令消息请求
    /// </summary>
    public class CreateObject : ICommand<string>
    {
        //可以自定义任意的字段
        public string Title { get; private set; }
        public string Content { get; private set; }

        public CreateObject(string title,string content)
        {
            Title = title.Trim();
            Content = content.Trim();
        }
    }
    /// <summary>
    /// 创建单播查询消息请求
    /// </summary>
    public class QueryObject : IQuery<User>
    {
        public string Name { get; private set; }

        public QueryObject(string name)
        {
            Name = name.Trim();
        }
    }

3 创建单播命令和查询消息处理类

    /// <summary>
    /// 处理命令消息的类
    /// 继承IRequestHandler
    /// </summary>
    public class CommandHandler : IRequestHandler<CreateObject, string>
    {
        public Task<string> Handle(CreateObject request, CancellationToken cancellationToken)
        {
            Console.WriteLine($"执行单播命令 CreateObject...Title:{request.Title},Content:{request.Content}"  );
            return Task.FromResult("ok");
        }
    }
    /// <summary>
    /// 处理查询消息的类
    /// </summary>
    public class QueryHandler : IRequestHandler<QueryObject, User>
    {
        public Task<User> Handle(QueryObject request, CancellationToken cancellationToken)
        {

            Console.WriteLine($"执行单播命令的query,{request.Name}");

            return Task.FromResult(new User()
            {
                Name = "哈利波特"
            });
        }
    }

4 创建测试类

     async static Task Main(string[] args)
        {

            //发送单播请求
            //使用中介者发送单播消息
            var services = new ServiceCollection();

            services.AddMediatR(typeof(Program).Assembly);

            var serviceProvider = services.BuildServiceProvider();

            var mediator = serviceProvider.GetService<IMediator>();

            var createResult = await mediator.Send(new CreateObject("命令1", "哈利波特骑着扫帚飞"));

            Console.WriteLine($"CreateObject 命令结果为{createResult}");

            var queryResult = await mediator.Send(new QueryObject("哈利波特"));

            Console.WriteLine($"QueryObject 查询结果为{JsonConvert.SerializeObject(queryResult)}");


        }

多播模式

1 创建多播消息

    /// <summary>
    /// 构建多播消息
    /// </summary>
    public class Ping : INotification
    {
        public string Name { get; private set; }

        public Ping(string name)
        {
            Name = name.Trim();
        }
    }

2 创建两个不同的消息处理类

    /// <summary>
    /// 消息处理器1
    /// </summary>
    public class Ping1Handler : INotificationHandler<Ping>
    {
        public Task Handle(Ping notification, CancellationToken cancellationToken)
        {
            Console.WriteLine($"{notification.Name} 多播消息处理 1");
            return Task.CompletedTask;
        }
    }
    /// <summary>
    /// 消息处理器2
    /// </summary>
    public class Ping2Handler : INotificationHandler<Ping>
    {
        public Task Handle(Ping notification, CancellationToken cancellationToken)
        {
            Console.WriteLine($"{notification.Name} 多播消息处理 2");
            return Task.CompletedTask;
        }
    }

3 创建多播测试类

    async static Task Main(string[] args)
        {

            //多播处理
            await mediator.Publish(new Ping("多播命令"));

        }

 

posted on 2021-12-22 16:01  心澄欲遣  阅读(68)  评论(0编辑  收藏  举报
欢迎第myspace graphics个访客