Unity3D自学笔记——Photon服务器的后台架构实现(三)
2016-10-28 15:08
513 查看
ALI.ARPG.CommandProcessor
ICommand
命令接口,该接口为空接口,没有包含任何函数,只作为约束
ICommandHandler
命令角色,通过调用Data层实现具体的业务逻辑处理,这里用了TCommand泛型而没直接用ICommand,是为了后面CommandBus反射去找ICommand对应的Handler
ICommandResult
ICommandHandler执行处理的结果
IValidateHandler
同样为命令角色,命令执行前验证,如用户注册前需要验证用户名合法性,验证结果为IEnumerable,意味可以有多种验证错误,如长度不符合,字符非法等,返回null,则表示验证通过
ValidationResult
验证结果,这里没用接口是因为其没有具体方法,只是一个单纯的对象
ICommandBus
命令接收者,接收命令,并移交至相应的Handler进行处理。
CommandBus
通过反射查找对应的CommandHandler和ValidateHandler,具体实现方法为GetHandlerType
1. 遍历ALI.ARPG.DOMAIN里的所有类型
2. 查找是否有实现了TCommand对应ICommandHandler、IValidateHandler接口的类
*如
TCommad为 RegCommand : ICommand
对应的ICommandHandler 即为 ICommandHandler< RegCommand>,则遍历查找是否有类实现了该接口*
3. 反射创建该类
4. 此处应该有缓存
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; } }
相关文章推荐
- Unity3D自学笔记——Photon服务器的后台架构实现(四)
- Unity3D自学笔记——Photon服务器的后台架构实现(五)
- Unity3D自学笔记——Photon服务器的后台架构实现(一)
- Unity3D自学笔记——架构应用(二)选择英雄界面
- Unity3D自学笔记——架构应用(六)角色创建
- Unity3D自学笔记——UGUI背包系统(二)UI设计实现及数据绑定
- Unity3D自学笔记——UGUI前台架构
- Unity3D自学笔记——架构应用(八)人物移动与角色相机的跟随
- Unity3D自学笔记——架构应用(四)JsonToEntity帮助类更新
- 实现智能硬件与移动app后台的服务器架构选择
- Unity3D自学笔记——架构应用(一)用户登录
- Unity3D自学笔记——架构应用(七)客户端识别角色是否已经创建
- Unity3D自学笔记——UGUI背包系统(五)沿用Attribute设计实现物品等级
- Unity3D自学笔记——架构应用(五)Loading场景及持久化场景
- Unity3D自学笔记——架构应用(三)JsonToEntity帮助类
- Unity3D自学笔记——实现2D物体漂浮在制定3D物体上方
- 【Linux笔记】在后台执行scp,实现服务器间无密码文件拷贝。
- Unity3D自学笔记——架构应用(十)角色属性
- 一步一步实现企业网络架构之八:利用IIS建立和维护一个WEB服务器 推荐
- 负载均衡--大型在线系统实现的关键(下篇)(服务器集群架构的设计与选择)