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
原创粉丝点击