Caliburn笔记-过滤器管理(IFilterManager)(wpf框架)

     过滤器用于装饰对象本身和对象中的方法.如下图

image

具体介绍可见此

http://caliburn.codeplex.com/wikipage?title=Filters&referringTitle=Documentation

IFilterManager

IFilterManager接口为过滤器管理器,用于存放过滤器接口,每个Action都有一个过滤器管理器

1.IRescue

捕获错误

public class Calculator
{

    [Rescue(Priority=1)]
    public int Divide(int left, int right)
    {
        throw new Exception("error");
        return left / right;
    }
}

2.IPreProcessor

在Action触发之前触发或阻止Action触发

3.IPostProcessor

在Action触发之后触发

public class PreProcessorAttribute : Attribute, IPreProcessor
   {
       #region IPreProcessor Members

       public bool AffectsTriggers
       {
           get { return true; }
       }

       public bool Execute(IRoutedMessage message, IInteractionNode handlingNode, object[] parameters)
       {
           Console.WriteLine("PreProcessor");
           return true;
       }

       #endregion

       #region IFilter Members

       public int Priority
       {
           get { return 1; }
       }

       #endregion
   }

   public class PostProcessorAttribute : Attribute, IPostProcessor
   {

       #region IPostProcessor Members

       public void Execute(IRoutedMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
       {
           Console.WriteLine("PostProcessor");
       }

       #endregion

       #region IFilter Members

       public int Priority { get; set; }

       #endregion
   }


看下执行结果

[PreProcessorAttribute()]
[PostProcessorAttribute()]
public int Divide(int left, int right)
{
    Console.WriteLine("Action");
    return left / right;
}

 

image

过滤器与Action是密切相关的,我们可以来看下Action内部的实现,看下面代码,上面的流程就非常的清晰了

public override void Execute(ActionMessage actionMessage, IInteractionNode handlingNode, object context)
{
    try
    {
        var parameters = _messageBinder.DetermineParameters(
            actionMessage,
            _requirements,
            handlingNode,
            context
            );

        
 
 
  
  foreach (
  
  var filter 
  
  in _filters.PreProcessors)
 
 
        {
            if (!filter.Execute(actionMessage, handlingNode, parameters)) return;
        }

        var outcome = new MessageProcessingOutcome(
           
 
 
  
   _method.Invoke(handlingNode.MessageHandler.Unwrap(), parameters),
 
 
            _method.Info.ReturnType,
            false
            );

       
 
  
  
  foreach (
  
  var filter 
  
  in _filters.PostProcessors)
 
 
        {
            filter.Execute(actionMessage, handlingNode, outcome);
        }

        HandleOutcome(actionMessage, handlingNode, outcome);
    }
    catch (Exception ex)
    {
      
 
   
  
  if(!TryApplyRescue(actionMessage, handlingNode, ex))
 
 
            throw;
        OnCompleted();
    }
}


可以使用以上特性定义自己需要的过滤器,建议不要重新定义Action,若要这么做的话就需要管理好过滤器的生命周期,或者有意更改生命周期

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值