命令过滤器

命令过滤器

中文(中国) Toggle Dropdown
 
v1.6 Toggle Dropdown

关键字: 命令过滤器, 命令, 过滤器, OnCommandExecuting, OnCommandExecuted

SuperSocket 中的命令过滤器看起来有些像 ASP.NET MVC 中的 Action Filter,你可以用它来做命令执行的拦截,命令过滤器会在命令执行前和执行后被调用。

命令过滤器必须继承于 Attribute 类 CommandFilterAttribute:

 /// <summary>
/// Command filter attribute
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public abstract class CommandFilterAttribute : Attribute
{
    /// <summary>
    /// Gets or sets the execution order.
    /// </summary>
    /// <value>
    /// The order.
    /// </value>
    public int Order { get; set; }

    /// <summary>
    /// Called when [command executing].
    /// </summary>
    /// <param name="commandContext">The command context.</param>
    public abstract void OnCommandExecuting(CommandExecutingContext commandContext);

    /// <summary>
    /// Called when [command executed].
    /// </summary>
    /// <param name="commandContext">The command context.</param>
    public abstract void OnCommandExecuted(CommandExecutingContext commandContext);
}

你需要为你的命令过滤器实现下面两个方法:

OnCommandExecuting: 此方法将在命令执行前被调用;

OnCommandExecuted: 此方法将在命令执行后被调用;

Order: 此属性用于设置多个命令过滤器的执行顺序;

下面的代码定义了一个命令过滤器 LogTimeCommandFilterAttribute 用于记录执行时间超过5秒钟的命令:

public class LogTimeCommandFilter : CommandFilterAttribute
{
    public override void OnCommandExecuting(CommandExecutingContext commandContext)
    {
        commandContext.Session.Items["StartTime"] = DateTime.Now;
    }

    public override void OnCommandExecuted(CommandExecutingContext commandContext)
    {
        var session = commandContext.Session;
        var startTime = session.Items.GetValue<DateTime>("StartTime");
        var ts = DateTime.Now.Subtract(startTime);

        if (ts.TotalSeconds > 5 && session.Logger.IsInfoEnabled)
        {
            session.Logger.InfoFormat("A command '{0}' took {1} seconds!", commandContext.CurrentCommand.Name, ts.ToString());
        }
    }
}

然后通过增加属性的方法给命令 "QUERY" 应用此过滤器:

[LogTimeCommandFilter]
public class QUERY : StringCommandBase<TestSession>
{
    public override void ExecuteCommand(TestSession session, StringCommandInfo commandData)
    {
        //Your code
    }
}

如果你想应用次过滤器给所有的命令, 你可以向下面的代码这样将此命令过滤器的属性加到你的 AppServer 类上面去:

[LogTimeCommandFilter]
public class TestServer : AppServer<TestSession>
{

}

你可以通过将 commandContext's 的 Cancel 属性设为 True 来取消当前命令的执行:

public class LoggedInValidationFilter : CommandFilterAttribute
{
    public override void OnCommandExecuting(CommandExecutingContext commandContext)
    {
        var session = commandContext.Session as MyAppSession;

        //If the session is not logged in, cancel the executing of the command
        if (!session.IsLoggedIn)
            commandContext.Cancel = true;
    }

    public override void OnCommandExecuted(CommandExecutingContext commandContext)
    {

    }
}
 

© 2018 - GetDocs.Net - Hosted by BuyVM

转载于:https://www.cnblogs.com/liuslayer/p/8624406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值