Unity3D自学笔记——Photon服务器的后台架构实现(三)
来源:互联网 发布:淘宝旺旺号 编辑:程序博客网 时间:2024/06/10 08:52
ALI.ARPG.CommandProcessor
ICommand
命令接口,该接口为空接口,没有包含任何函数,只作为约束
public interface ICommand { }
ICommandHandler
命令角色,通过调用Data层实现具体的业务逻辑处理,这里用了TCommand泛型而没直接用ICommand,是为了后面CommandBus反射去找ICommand对应的Handler
public interface ICommandHandler<in TCommand> where TCommand : ICommand { ICommandResult Excute(TCommand command); }
ICommandResult
ICommandHandler执行处理的结果
public interface ICommandResult { bool Success { get; } }
public class CommandResult : ICommandResult { public CommandResult(bool success) { this.Success = success; } public bool Success { get; protected set; } }
IValidateHandler
同样为命令角色,命令执行前验证,如用户注册前需要验证用户名合法性,验证结果为IEnumerable,意味可以有多种验证错误,如长度不符合,字符非法等,返回null,则表示验证通过
public interface IValidateHandler<in TCommand> where TCommand : ICommand { IEnumerable<ValidationResult> Validate(TCommand command); }
ValidationResult
验证结果,这里没用接口是因为其没有具体方法,只是一个单纯的对象
public class ValidationResult { public string MemberName { get; set; } public string Message { get; set; } public ValidationResult() { } public ValidationResult(string memberName, string message) { this.MemberName = memberName; this.Message = message; } public ValidationResult(string message) { this.Message = message; } }
ICommandBus
命令接收者,接收命令,并移交至相应的Handler进行处理。
public interface ICommandBus { ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand; IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand; }
CommandBus
通过反射查找对应的CommandHandler和ValidateHandler,具体实现方法为GetHandlerType
1. 遍历ALI.ARPG.DOMAIN里的所有类型
2. 查找是否有实现了TCommand对应ICommandHandler、IValidateHandler接口的类
*如
TCommad为 RegCommand : ICommand
对应的ICommandHandler 即为 ICommandHandler< RegCommand>,则遍历查找是否有类实现了该接口*
3. 反射创建该类
4. 此处应该有缓存
public class CommandBus : ICommandBus { public ICommandResult Submit<TCommand>(TCommand command) where TCommand : ICommand { var handler = GetCommandHandler(command); if (!((handler != null) && handler is ICommandHandler<TCommand>)) { throw new CommandHandlerNotFoundException(typeof(TCommand)); } return handler.Excute(command); } public IEnumerable<ValidationResult> Validate<TCommand>(TCommand command) where TCommand : ICommand { var handler = GetValidateHandler(command); if (!((handler != null) && handler is IValidateHandler<TCommand>)) { throw new ValidationHandlerNotFoundException(typeof(TCommand)); } return handler.Validate(command); } private ICommandHandler<TCommand> GetCommandHandler<TCommand>(TCommand command) where TCommand : ICommand { Object handler = GetHandlerType(command, typeof(ICommandHandler<>)); if (handler != null) return handler as ICommandHandler<TCommand>; return null; } private IValidateHandler<TCommand> GetValidateHandler<TCommand>(TCommand command) where TCommand : ICommand { Object handler = GetHandlerType(command, typeof(IValidateHandler<>)); if (handler != null) return handler as IValidateHandler<TCommand>; return null; } private Object GetHandlerType<TCommand>(TCommand command, Type handlerType) { Assembly assmbly = Assembly.Load("ALI.ARPG.DOMAIN"); Type[] types = assmbly.GetTypes(); Type commandType = command.GetType(); handlerType = handlerType.MakeGenericType(commandType); foreach (var type in types) { if (type.GetInterfaces().Length > 0 && (type.GetInterfaces()[0].Equals(handlerType))) { var handler = Activator.CreateInstance(type); return handler; } } return null; } }
0 0
- Unity3D自学笔记——Photon服务器的后台架构实现(三)
- Unity3D自学笔记——Photon服务器的后台架构实现(一)
- Unity3D自学笔记——Photon服务器的后台架构实现(二)
- Unity3D自学笔记——Photon服务器的后台架构实现(四)
- Unity3D自学笔记——Photon服务器的后台架构实现(五)
- Unity3D自学笔记——架构应用(三)JsonToEntity帮助类
- Unity+ Photon服务器实时对战游戏——Photon服务器介绍(三)
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- Unity3D自学笔记——UGUI前台架构
- Unity3D自学笔记——架构应用(一)用户登录
- Unity3D自学笔记——架构应用(二)选择英雄界面
- Unity3D自学笔记——架构应用(四)JsonToEntity帮助类更新
- Unity3D自学笔记——架构应用(五)Loading场景及持久化场景
- Unity3D自学笔记——架构应用(六)角色创建
- Unity3D自学笔记——架构应用(七)客户端识别角色是否已经创建
- Unity3D自学笔记——架构应用(九)加载角色数据
- Unity3D自学笔记——架构应用(十)角色属性
- Unity3D自学笔记——UGUI背包系统(三)物品弹出提示框
- 将ECSHOP的广告链接地址去掉affiche.php改为绝对地址
- CUDA event 记录GPU端运行时间
- 仿QQ聊天界面里边的相册(QQ相册)
- Linux应用随笔(八)各类软件工具整理
- 利用Metrics+influxdb+grafana构建监控平台
- Unity3D自学笔记——Photon服务器的后台架构实现(三)
- 【Android】掌握自定义LayoutManager(二) 实现流式布局
- Markdown Notes02_Classical Syntax
- jQuery动画高级用法(上)——详解animation中的.queue()函数
- Druid.io系列(六):问题总结
- ST480的驱动分析(三轴磁力计ST480MC)
- selenium 学习网站
- [svn不提交classes文件夹下的文件][SVN不交付classes解决方案]
- 最新cocoapods安装流程,安装过程中遇到的问题及解决方法