[SharePoint 开发积累] 在Eventhandler中使用State模式

本文通过一个具体的案例,探讨了在Eventhandler开发中如何利用状态模式优化代码结构,以应对需求变化带来的挑战。

在此记录下Eventhandler开发中曾经走过的弯路和一点心得......

曾经的项目中有个小需求,在一个list中创建一个Choice类型的字段,名为Status,分别有"Not Started","In Progress","On Schedule","ahead of Schedule","Delayed","Done"等等用来表示进度的字段,要求当这个item的Status值从"Not Started"变为"In Progress"时,给项目经理发邮件,当从"In Progress"变为"ahead of Schedule"时给整个项目组加上部门经理发邮件。

需求就这么简单,当时也没多想,就用Eventhandler实现了,大致代码如下:

ExpandedBlockStart.gif 代码
string  previousStatus  =  properties.ListItem[ " Status " as  String;
string  currentStatus  =  properties.AfterProperties[ " Status " as  String;
if (previousStatus  ==   " Not Started " && currentStatus  ==   " In Progress " )
{
    SendEmail(mgr);
}
if (previousStatus  ==   " In Progress " && currentStatus  ==   " ahead of Schedule " )
{
    SendEmail(director);
}

 

很好,这样的Eventhandler完成了需求,简单明了。跑了一段时间,业务需求发生变化了,当从"In Progress"变成"Done"时也要发邮件给Mgr,于是又修改了代码中的if condition,但是需求总是在变化,有时候在Eventhandler中不仅涉及到了Stauts字段变化的处理还有别的字段变化处理,总之 if condition到后来变的象面条一样冗长,让人看了很不舒服,维护起来也很麻烦了。

在代码review中,我们认识到了这样的需求变化在Eventhandler开发中太常见了,于是打算用State模式来改写以前的代码,使得我们的扩展更加方便:

State模式的应用场景:在你的类里面有个型别码来表示对象的当前状态,这个对象的行为通常依赖这个状态,而且在运行的时候这个状态会改变,那么对象的行为在运行的时候也要跟着改变。一般我们会使用if/else或者switch来根据这个型别码来执行相关操作,现在我们有更好的方式来处理--State模式。

好像这个场景很符合我们当前的情况,于是我们就开始改写代码了。根据State模式类图,首先创建State的抽象类

public   abstract   class  State
    {
        
public   abstract   void  Select();
    }

 其次为每个可能的状态编写状态类,继承自State类

ExpandedBlockStart.gif 代码
     class  InProgressState:State
    {
        Public StateContext context;
        
public   override   void  Select()
        {
            SendEmail(mgr);
        }
    }


    
class  AheadOfSchedule:State
    {

        
public   override   void  Select()
        {
            SendEmail(director);
        }
    }

 

还有Context类:

 

ExpandedBlockStart.gif 代码
class  StateContext
    {
        State _currentState;
        
string  _currentValue;
        public StateContext(string currentValue)
        {
             this._currentValue = currentValue;
        }
        
public  State CurrentState {  get set ; }
        
public   string  CurrentValue{  get set ; }
        
public   void  Process()
        {
            
switch  (CurrentValue)
            {
                
case   " In Progress " :
                    {
                        CurrentState 
=   new  InProgressState();
                        CurrentState.Select();
                        
return ;
                    }
                
case   "Ahead of Schedule " :
                    {
                        CurrentState 
=   new  AheadOfSchedule();
                        CurrentState.Select();
                        
return ;
                    }
                
default :
                    {
                        
return ;
                    }
            }

        }
    }

 

Eventhandler调用代码:

ExpandedBlockStart.gif 代码
public   override   void  ItemUpdating(SPItemEventProperties properties)
        {

            
string  currentStatus  =  properties.AfterProperties[ " Status " as  String;
            StateContext context = new StateContext(currentStatus);
            context.Process();
        }

当然这里还没有做到完全抽象,但较之前的情况已经好了很多。

当以后还遇到在Eventhandler中涉及到状态改变的复杂逻辑时,先考虑State模式会很有好处。

转载于:https://www.cnblogs.com/tonnie/archive/2010/07/21/eventhandlerstate.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值