CS前置/后置事件框架

         把CommunityServer的事件框架应用到当前的项目中,可以在一个方法调用中设置其前置和后置事件,从而把一个额外的业务处理外置到别的模块中进行独立处理。
1、需要定义一系列的委托,如public delegate void PSPRoleEventHandler(PRole pRole, PSEventArgs e);委托参数的定义决定以后事件的参数,这里的PSEventArgs用于表示一些通用的参数传入,例如ObjectState针对对象操作的状态。

None.gif      public   enum  ObjectState
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Create,
InBlock.gif        Update,
InBlock.gif        Delete,
InBlock.gif        None,
InBlock.gif        Runtime
ExpandedBlockEnd.gif    }

None.gif      public   class  PSEventArgs : EventArgs
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private ObjectState _state;
InBlock.gif
InBlock.gif        
public ObjectState State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _state; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public PSEventArgs(ObjectState state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _state 
= state;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public PSEventArgs() : this(ObjectState.None) dot.gif{ }
ExpandedBlockEnd.gif    }

2、创建PSApplication类,用于统一定义一些事件,并从配置文件的模块配置加载这些事件的真实调用,以下为一下例子:
None.gif      public   class  PSApplication
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
private members#region private members
InBlock.gif        
private EventHandlerList Events = new EventHandlerList();
InBlock.gif        
private static readonly object sync = new object();
InBlock.gif        
private Hashtable modules = new Hashtable();
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Event Keys (static)#region Event Keys (static)
InBlock.gif
InBlock.gif        
private static object EventPrePRoleUpdate = new object();
InBlock.gif        
private static object EventPRoleUpdate = new object();
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
cnstr#region cnstr
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private PSApplication() dot.gif{ }
InBlock.gif        
internal static PSApplication Instance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
const string key = "PSApplication";
InBlock.gif            HttpContext context 
= HttpContext.Current;
InBlock.gif
InBlock.gif            PSApplication app 
= context.Cache[key] as PSApplication;
InBlock.gif            
if (app == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock (sync)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    app 
= context.Cache[key] as PSApplication;
InBlock.gif                    
if (app == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        PSConfiguration config 
= PSConfiguration.GetConfig();
InBlock.gif
InBlock.gif                        XmlNode node 
= config.GetConfigSection("Configurations/PSModules");
InBlock.gif                        app 
= new PSApplication();
InBlock.gif                        
if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
foreach (XmlNode n in node.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
if (n.NodeType != XmlNodeType.Comment)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    
switch (n.Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
dot.gif{
InBlock.gif                                        
case "clear":
InBlock.gif                                            app.modules.Clear();
InBlock.gif                                            
break;
InBlock.gif                                        
case "remove":
InBlock.gif                                            XmlAttribute removeNameAtt 
= n.Attributes["name"];
InBlock.gif                                            
string removeName = removeNameAtt == null ? null : removeNameAtt.Value;
InBlock.gif
InBlock.gif                                            
if (!String.IsNullOrEmpty(removeName) && app.modules.ContainsKey(removeName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
dot.gif{
InBlock.gif                                                app.modules.Remove(removeName);
ExpandedSubBlockEnd.gif                                            }

InBlock.gif
InBlock.gif                                            
break;
InBlock.gif                                        
case "add":
InBlock.gif
InBlock.gif                                            XmlAttribute en 
= n.Attributes["enabled"];
InBlock.gif                                            
if (en != null && en.Value == "false")
InBlock.gif                                                
continue;
InBlock.gif
InBlock.gif                                            XmlAttribute nameAtt 
= n.Attributes["name"];
InBlock.gif                                            XmlAttribute typeAtt 
= n.Attributes["type"];
InBlock.gif                                            
string name = nameAtt == null ? null : nameAtt.Value;
InBlock.gif                                            
string itype = typeAtt == null ? null : typeAtt.Value;
InBlock.gif
InBlock.gif                                            
if (String.IsNullOrEmpty(name))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
dot.gif{
InBlock.gif                                                
//EventLogs.Warn("");
InBlock.gif
                                                continue;
ExpandedSubBlockEnd.gif                                            }

InBlock.gif
InBlock.gif                                            
if (String.IsNullOrEmpty(itype))
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
dot.gif{
InBlock.gif                                                
//EventLogs.Warn("");
InBlock.gif
                                                continue;
ExpandedSubBlockEnd.gif                                            }

InBlock.gif
InBlock.gif                                            Type type 
= Type.GetType(itype);
InBlock.gif
InBlock.gif                                            
if (type == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
dot.gif{
InBlock.gif                                                
//EventLogs.Warn("");
InBlock.gif
                                                continue;
ExpandedSubBlockEnd.gif                                            }

InBlock.gif
InBlock.gif                                            IPSModule mod 
= Activator.CreateInstance(type) as IPSModule;
InBlock.gif
InBlock.gif                                            
if (mod == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                            
dot.gif{
InBlock.gif                                                
//EventLogs.Warn("");
InBlock.gif
                                                continue;
ExpandedSubBlockEnd.gif                                            }

InBlock.gif
InBlock.gif                                            mod.Init(app, n);
//注意,通过Configurations/PSModules/[Model]/add节的配置可以传递一些扩展的配置
InBlock.gif
                                            app.modules.Add(name, mod);
InBlock.gif
InBlock.gif                                            
break;
ExpandedSubBlockEnd.gif                                    }

ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
//以PSConfiguration的CacheKey为依赖项,即PSConfiguration的缓存过期,则本缓存也会过期
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        CacheDependency dep = new CacheDependency(nullnew string[] dot.gif{ PSConfiguration.CacheKey });
InBlock.gif                        context.Cache.Insert(key, app, dep);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return app;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
PRole Events#region PRole Events
InBlock.gif
InBlock.gif        
internal void ExecutePrePRoleUpdate(PRole pRole, ObjectState state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PSPRoleEventHandler handler 
= Events[EventPrePRoleUpdate] as PSPRoleEventHandler;
InBlock.gif            
if (handler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                handler(pRole, 
new PSEventArgs(state));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
internal void ExecutePRoleUpdate(PRole pRole, ObjectState state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PSPRoleEventHandler handler 
= Events[EventPRoleUpdate] as PSPRoleEventHandler;
InBlock.gif            
if (handler != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                handler(pRole, 
new PSEventArgs(state));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 触发事件,在PRole数据保存到数据库之前
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event PSPRoleEventHandler PrePRoleUpdate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            add 
dot.gif{ Events.AddHandler(EventPrePRoleUpdate, value); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            remove 
dot.gif{ Events.RemoveHandler(EventPrePRoleUpdate, value); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 触发事件,在PRole数据保存到数据库之后
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public event PSPRoleEventHandler PRoleUpdate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            add 
dot.gif{ Events.AddHandler(EventPRoleUpdate, value); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            remove 
dot.gif{ Events.RemoveHandler(EventPRoleUpdate, value); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

None.gif

其中的internal static PSApplication Instance()方法用于实例化PSApplication对象并从配置文件中加载事件真实实现的模块信息,例如模块配置可以写成这样:
None.gif < Configurations >
None.gif    
< PSModules >
None.gif        
< add  name ="PRoleModule"  type ="CSDN.PermissionService.Components.PRoleModule, CSDN.PermissionService.Components"   />     
None.gif    
</ PSModules >
None.gif
</ Configurations >
这样通过组件CSDN.PermissionService.Components中的CSDN.PermissionService.Components.PRoleModule模块就可以加载对角色操作的一些前后事件的功能
public event PSPRoleEventHandler PrePRoleUpdate 添加或移除在PRole数据保存到数据库之前的触发事件
public event PSPRoleEventHandler PRoleUpdate 添加或移除在PRole数据保存到数据库之后的触发事件
private EventHandlerList Events = new EventHandlerList(); 用于保存一下委托的事件列表

对于模块设计需要定实现一个IPSModule接口
None.gif      public   interface  IPSModule
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
void Init(PSApplication psa, XmlNode node);
ExpandedBlockEnd.gif    }
例如:PRoleModule
None.gif      public   class  PRoleModule : IPSModule
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
public void Init(PSApplication psa, System.Xml.XmlNode node)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            psa.PrePRoleUpdate 
+= new PSPRoleEventHandler(psa_PrePRoleUpdate);
InBlock.gif            psa.PRoleUpdate 
+= new PSPRoleEventHandler(psa_PRoleUpdate);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void psa_PrePRoleUpdate(PRole pRole, PSEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//前置事件
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private void psa_PRoleUpdate(PRole pRole, PSEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//后置事件
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedBlockEnd.gif    }
在模块里可以注册一些相关的事件,并且实现这些事件,以把一些前置或后置处理外置到主业务体外。
对于PSEvents是些方法调用的封装:
None.gif      public   class  PSEvents
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif        
PRoles#region PRoles
InBlock.gif
InBlock.gif        
public static void BeforePRole(PRole pRole, ObjectState state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PSApplication.Instance().ExecutePrePRoleUpdate(pRole, state);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void AfterPRole(PRole pRole, ObjectState state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PSApplication.Instance().ExecutePRoleUpdate(pRole, state);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/BillChen/archive/2007/05/21/754348.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值