Community Server专题六:Delegates & Events

本文详细解析了CommunityServer中异常处理的机制,通过Delegates与Events实现了模块化的异常处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Community Server专题六:Delegates & Events
对于CS的分析你可以能会从页面开始,其实那并不是一个很好的方法,因为CS采用了MasterPage和内建的Theme与Skins,页面一层嵌套一层,如果你对CS页面执行机制不了解,或者你是初学者,这个时候可能就会碰壁,接着就放弃了对CS更深入的了解。我希望我的专题能从CS的运行过程开始一步一步地讲解,同时把ASP.NET的运行机理也表述出来,因此学习了解CS的过程就是对ASP.NET深入了解得过程。当然,我个人的开发经验与水平也是有限的,如果在专题中表述有问题,或者有疑问可以直接在文章的评论中直接指出,我将万分感谢你。
在分析CSHttpModule.cs的时候,你会看到这样两句代码:
CSEvents.UserKnown(csContext.User);
CSEvents.CSException(csException);
其实短短两行代码后面隐藏了Delegates与Events的大量运用,CS也通过这样的运用实现了一种模块化的处理机制,即CSModules。
打开CommunityServerWeb项目下的communityserver.config文件,这是CS的配置文件(与Web.config不同,communityserver.config主要配置的是CS内部的一些运行机制,而Web.config主要配置的是与Asp.net的交互)。找到文件中的这段:
<CSModules>

              
<add name = "CSMembershipRulesModule" type = "CommunityServer.Components.CSMembershipRulesModule, CommunityServer.Components" />

              
<add name = "CSCatastrophicExceptionModule" type = "CommunityServer.Components.CSCatastrophicExceptionModule, CommunityServer.Components" />

        
<add name = "CSExceptionModule" type = "CommunityServer.Components.CSExceptionModule, CommunityServer.Components" />

              
<add name = "IrcCommands" type = "CommunityServer.Discussions.Components.IrcCommandsModule, CommunityServer.Discussions" />

              
<add name = "ForumCensorship" type = "CommunityServer.Discussions.Components.CensorshipModule, CommunityServer.Discussions" />

              
<add name = "ForumEmoticon" type = "CommunityServer.Discussions.Components.EmoticonModule, CommunityServer.Discussions" />

              
<add name = "ForumSourceCode" type = "CommunityServer.Discussions.Components.SourceCodeModule, CommunityServer.Discussions" />

              
<add name = "ForumHtmlScrubbing" type = "CommunityServer.Discussions.Components.HtmlScrubbingModule, CommunityServer.Discussions" />

              
<add name = "BBcodeToHtml" type = "CommunityServer.Discussions.Components.BBcodeToHtmlModule, CommunityServer.Discussions" />

              
<add name = "ForumPlainText" type = "CommunityServer.Discussions.Components.PlainTextModule, CommunityServer.Discussions" />

     

              
<add name = "WeblogCensorModule" type = "CommunityServer.Blogs.Components.CensorModule, CommunityServer.Blogs" />

              
<add name = "WeblogPostandArticleHtmlScrubbing" type = "CommunityServer.Blogs.Components.PostandArticleHtmlScrubbing, CommunityServer.Blogs" />

              
<add name = "WeblogFeedbackHtmlFormatting" type = "CommunityServer.Blogs.Components.FeedbackHtmlFormatting, CommunityServer.Blogs" />

              
<add name = "TrackbackModule" type = "CommunityServer.Blogs.Components.TrackbackModule, CommunityServer.Blogs" />

              
<add name = "XmlRpcPingModule" type = "CommunityServer.Blogs.Components.XmlRpcPingModule, CommunityServer.Blogs" />

              
<add name = "WeblogFormattingModule" type = "CommunityServer.Blogs.Components.WeblogFormattingModule, CommunityServer.Blogs" />

              
<add name = "PictureCensor" type = "CommunityServer.Galleries.Components.CensorPictureModule, CommunityServer.Galleries" />

              
<add name = "PictureHtmlScrubber" type = "CommunityServer.Galleries.Components.HtmlScrubberModule, CommunityServer.Galleries" />

              
<add name = "PictureComments" type = "CommunityServer.Galleries.Components.CommentModule, CommunityServer.Galleries" />

              
<!-- <add name = "MaxPictureSize" type = "CommunityServer.Galleries.Components.MaxPictureSizeModule, CommunityServer.Galleries" maxWidth="1024" maxHeight="768" quality="90" /> -->

         
</CSModules>

我们拿出其中的一个来分析运行过程,例:
     <addname="CSExceptionModule"type="CommunityServer.Components.CSExceptionModule, CommunityServer.Components"/>
这是CS中异常处理的模块,当发生异常的时候该模块将调用一个RedirectToMessage方法,提示一个友好的错误界面,告诉请求的用户有错误发生。那么CS系统是如何在发生错误的时候自动调用RedirectToMessage方法转向另外一个页面提示友好错误的呢?先打开CommunityServerComponents项目下Components文件夹中的CSApplication.cs
 
using System;

using System.Collections;

using System.ComponentModel;

using System.Web.Caching;

using System.Xml;

using CommunityServer.Configuration;

 

namespace CommunityServer.Components

{

 

     
#region Delegates

     
//Do we want one single delegate or a custom one for each type

     
//public delegate void CSEventHandler(object sender, CSEventArgs e);

     
public delegate void CSUserEventHandler(User user, CSEventArgs e);

     
public delegate void CSPostEventHandler(Post post, CSEventArgs e);

     
public delegate void CSSectionEventHandler(Section section, CSEventArgs e);

     
public delegate void CSGroupEventHandler(Group group, CSEventArgs e);

     
public delegate void CSExceptionHandler(CSException csEx, CSEventArgs e);

     
#endregion


 

     
/// <summary>

     
/// Summary description for CSApplication.

     
/// </summary>


     
public class CSApplication

     
{

         
#region private members

         
private EventHandlerList Events = new EventHandlerList();

         
private static readonly object sync = new object();

         
private Hashtable modules = new Hashtable();

         
#endregion


 

         
#region Event Keys (static)

         
private static object EventAuthorizePost = new object();

         
private static object EventPrePostUpdate = new object();

         
private static object EventPreProcessPost = new object();

         
private static object EventPostPostUpdate = new object();

         
private static object EventRatePost = new object();

         
//private static object EventPreRenderPost = new object();

 

         
private static object EventPreUserUpdate = new object();

         
private static object EventPostUserUpdate = new object();

         
private static object EventUserRemove = new object();

         
private static object EventUserKnown = new object();

         
private static object EventUserValidated = new object();

 

         
private static object EventPreSectionUpdate = new object();

         
private static object EventPostSectionUpdate = new object();

 

         
private static object EventPreSectionGroupUpdate = new object();

         
private static object EventPostSectionGroupUpdate = new object();

 

         
private static object EventUnhandledException = new object();

         
#endregion


 

         
#region cnstr

         
private CSApplication()

         
{

         }


 

         
internal static CSApplication Instance()

         
{

              
const string key = "CSApplication";

              CSApplication app 
= CSCache.Get(key) as CSApplication;

              
if(app == null)

              
{

                   
lock(sync)

                   
{

                       app 
= CSCache.Get(key) as CSApplication;

                       
if(app == null)

                       
{

                            CSConfiguration config 
= CSContext.Current.Config;

 

                            XmlNode node 
= config.GetConfigSection("CommunityServer/CSModules");

                            app 
= new CSApplication();

                            
if(node != null)

                            
{

                                 
foreach(XmlNode n in node.ChildNodes)

                                 
{

                                     
if(n.NodeType != XmlNodeType.Comment)

                                     
{

                                          
switch(n.Name)

                                          
{

                                               
case "clear":

                                                   app.modules.Clear();

                                                   
break;

                                               
case "remove":

                                                   app.modules.Remove(n.Attributes[
"name"].Value);

                                                   
break;

                                               
case "add":

                                               

                                                   
string name = n.Attributes["name"].Value;

                                                   
string itype = n.Attributes["type"].Value;

 

                                                   Type type 
= Type.GetType(itype);

 

                                                   
if(type == null)

                                                        
throw new Exception(itype + " does not exist");

 

                                                   ICSModule mod 
= Activator.CreateInstance(type) as ICSModule;

 

                                                   
if(mod == null)

                                                        
throw new Exception(itype + " does not implement ICSModule or is not configured correctly");

 

                                                   mod.Init(app, n);

                                                   app.modules.Add(name,mod);

 

 

                                                   
break;

 

                                          }


                                     }


                                 }


                            }


                            CacheDependency dep 
= new CacheDependency(nullnew string[]{CSConfiguration.CacheKey});

                            CSCache.Max(key, app,dep);

                       }


 

                       

                   }


              }


              
return app;

         }


         
#endregion


 

         
#region Post Events

 

         
#region Execute Events

 

         
internal void ExecuteAuthorizePost()

         
{

              ExecuteUserEvent(EventAuthorizePost,CSContext.Current.User);

         }


 

         
internal void ExecutePrePostEvents(Post post, ObjectState state, ApplicationType appType)

         
{

              ExecutePostEvent(EventPreProcessPost,post,state,appType);

         }


 

         
internal void ExecutePrePostUpdateEvents(Post post, ObjectState state, ApplicationType appType)

         
{

              ExecutePostEvent(EventPrePostUpdate,post,state,appType);

         }


 

         
internal void ExecutePostPostUpdateEvents(Post post, ObjectState state, ApplicationType appType)

         
{

              ExecutePostEvent(EventPostPostUpdate,post,state,appType);

         }


 

         
internal void ExecuteRatePostEvents(Post post, ApplicationType appType)

         
{

              ExecutePostEvent(EventRatePost,post,ObjectState.None,appType);

         }


 

//       internal void ExecutePrePostRender(Post post, ApplicationType appType)

//       {

//            ExecutePostEvent(EventPreRenderPost,post,ObjectState.None,appType);

//       }

 

         
protected void ExecutePostEvent(object EventKey, Post post,ObjectState state, ApplicationType appType)

         
{

              CSPostEventHandler handler 
= Events[EventKey] as CSPostEventHandler;

              
if (handler != null)

              
{

                   handler(post, 
new CSEventArgs(state,appType));

              }


         }


 

         
#endregion


 

         
#region Events

         
/// <summary>

         
/// Event raised before a user accesses a page which can be used to create content

         
/// </summary>


         
public event CSUserEventHandler AuthorizePost

         
{

              add
{Events.AddHandler(EventAuthorizePost, value);}

              remove
{Events.RemoveHandler(EventAuthorizePost, value);}

         }


 

         
/// <summary>

         
/// Event raised before any post processing takes place

         
/// </summary>


         
public event CSPostEventHandler PreProcessPost

         
{

              add
{Events.AddHandler(EventPreProcessPost, value);}

              remove
{Events.RemoveHandler(EventPreProcessPost, value);}

         }


 

         
/// <summary>

         
/// Fires after PreProcessPost but before the post change is commited to the datastore

         
/// </summary>


         
public event CSPostEventHandler PrePostUpdate

         
{

              add
{Events.AddHandler(EventPrePostUpdate, value);}

              remove
{Events.RemoveHandler(EventPrePostUpdate, value);}

         }


 

         
/// <summary>

         
/// Fires after a post change is commited to the datastore

         
/// </summary>


         
public event CSPostEventHandler PostPostUpdate

         
{

              add
{Events.AddHandler(EventPostPostUpdate, value);}

              remove
{Events.RemoveHandler(EventPostPostUpdate, value);}

         }


 

         
/// <summary>

         
/// Fires after a Post or Thread is rated

         
/// </summary>


         
public event CSPostEventHandler RatePost

         
{

              add
{Events.AddHandler(EventRatePost, value);}

              remove
{Events.RemoveHandler(EventRatePost, value);}

         }


 

//       /// <summary>

//       /// Event raised before an individual post is rendered

//       /// </summary>

//       public event CSPostEventHandler PreRenderPost

//       {

//            add{Events.AddHandler(EventPreRenderPost, value);}

//            remove{Events.RemoveHandler(EventPreRenderPost, value);}

//       }

 

         
#endregion


 

         
#endregion


 

         
#region User Events

 

         
#region Execute Events

         

         
internal void ExecuteUserValidated(User user)

         
{

              ExecuteUserEvent(EventUserValidated,user);

         }


 

         
internal void ExecuteUserKnown(User user)

         
{

              ExecuteUserEvent(EventUserKnown,user);

         }


 

         
internal void ExecutePreUserUpdate(User user, ObjectState state)

         
{

              ExecuteUserEvent(EventPreUserUpdate,user,state,ApplicationType.Unknown);

         }


 

         
internal void ExecutePostUserUpdate(User user, ObjectState state)

         
{

              ExecuteUserEvent(EventPostUserUpdate,user,state,ApplicationType.Unknown);

         }


 

         
internal void ExecuteUserRemove(User user)

         
{

              ExecuteUserEvent(EventUserRemove,user,ObjectState.Delete,ApplicationType.Unknown);

         }


 

         
protected void ExecuteUserEvent(object EventKey, User user)

         
{

              ExecuteUserEvent(EventKey,user,ObjectState.None,ApplicationType.Unknown);

         }


         
protected void ExecuteUserEvent(object EventKey, User user,ObjectState state, ApplicationType appType)

         
{

              CSUserEventHandler handler 
= Events[EventKey] as CSUserEventHandler;

              
if (handler != null)

              
{

                   handler(user, 
new CSEventArgs(state,appType));

              }


         }


 

         
#endregion


 

         
#region Events

 

         
/// <summary>

         
/// Fires after a user's credentials have been validated.

         
/// </summary>


         
public event CSUserEventHandler UserValidated

         
{

              add
{Events.AddHandler(EventUserValidated, value);}

              remove
{Events.RemoveHandler(EventUserValidated, value);}

         }


 

         
/// <summary>

         
/// Fires once the current user has been identified. This user may still be anonymous.

         
/// </summary>


         
public event CSUserEventHandler UserKnown

         
{

              add
{Events.AddHandler(EventUserKnown, value);}

              remove
{Events.RemoveHandler(EventUserKnown, value);}

         }


 

         
/// <summary>

         
/// Fires before a User is saved/updated to the datastore

         
/// </summary>


         
public event CSUserEventHandler PreUserUpdate

         
{

              add
{Events.AddHandler(EventPreUserUpdate, value);}

              remove
{Events.RemoveHandler(EventPreUserUpdate, value);}

         }


 

         
/// <summary>

         
/// Fires after a User is saved/updated to the datastore

         
/// </summary>


         
public event CSUserEventHandler PostUserUpdate

         
{

              add
{Events.AddHandler(EventPostUserUpdate, value);}

              remove
{Events.RemoveHandler(EventPostUserUpdate, value);}

         }


 

         
/// <summary>

         
/// Fires before a User is removed from the datastore.

         
/// </summary>


         
public event CSUserEventHandler UserRemove

         
{

              add
{Events.AddHandler(EventUserRemove, value);}

              remove
{Events.RemoveHandler(EventUserRemove, value);}

         }


 

         
#endregion


 

         
#endregion


 

         
#region Section Events

 

         
internal void ExecutePreSectionUpdate(Section section, ObjectState state, ApplicationType appType)

         
{

              CSSectionEventHandler handler 
= Events[EventPreSectionUpdate] as CSSectionEventHandler;

              
if (handler != null)

              
{

                   handler(section, 
new CSEventArgs(state,appType));

              }


         }


 

         
internal void ExecutePostSectionUpdate(Section section, ObjectState state, ApplicationType appType)

         
{

              CSSectionEventHandler handler 
= Events[EventPostSectionUpdate] as CSSectionEventHandler;

              
if (handler != null)

              
{

                   handler(section, 
new CSEventArgs(state,appType));

              }


         }


 

 

         
/// <summary>

         
/// Event raised before a section change is committed to the datastore (create/update)

         
/// </summary>


         
public event CSSectionEventHandler PreSectionUpdate

         
{

              add
{Events.AddHandler(EventPreSectionUpdate, value);}

              remove
{Events.RemoveHandler(EventPreSectionUpdate, value);}

         }


 

 

         
/// <summary>

         
/// Event raised after a section chage is committed to the data store

         
/// </summary>


         
public event CSSectionEventHandler PostSectionUpdate

         
{

              add
{Events.AddHandler(EventPostSectionUpdate, value);}

              remove
{Events.RemoveHandler(EventPostSectionUpdate, value);}

         }


 

         
#endregion


 

         
#region Group Events

 

         
internal void ExecutePreSectionGroupUpdate(Group group, ObjectState state, ApplicationType appType)

         
{

              CSGroupEventHandler handler 
= Events[EventPreSectionGroupUpdate] as CSGroupEventHandler;

              
if (handler != null)

              
{

                   handler(group, 
new CSEventArgs(state,appType));

              }


         }


 

         
internal void ExecutePostSectionGroupUpdate(Group group, ObjectState state, ApplicationType appType)

         
{

              CSGroupEventHandler handler 
= Events[EventPostSectionGroupUpdate] as CSGroupEventHandler;

              
if (handler != null)

              
{

                   handler(group, 
new CSEventArgs(state,appType));

              }


         }


 

         
/// <summary>

         
/// Event raised before a group chage is committed to the datastore (create/update)

         
/// </summary>


         
public event CSGroupEventHandler PreSectionGroupUpdate

         
{

              add
{Events.AddHandler(EventPreSectionGroupUpdate, value);}

              remove
{Events.RemoveHandler(EventPreSectionGroupUpdate, value);}

         }


 

         
/// <summary>

         
/// Event raised after a group chage is committed to the data store

         
/// </summary>


         
public event CSGroupEventHandler PostSectionGroupUpdate

         
{

              add
{Events.AddHandler(EventPostSectionGroupUpdate, value);}

              remove
{Events.RemoveHandler(EventPostSectionGroupUpdate, value);}

         }


 

         
#endregion


 

         
#region Exceptions

         
/// <summary>

         
/// Event raised before a group chage is committed to the datastore (create/update)

         
/// </summary>


         
public event CSExceptionHandler CSException

         
{

              add
{Events.AddHandler(EventUnhandledException, value);}

              remove
{Events.RemoveHandler(EventUnhandledException, value);}

         }


 

         
internal void ExecuteCSExcetion(CSException csEx)

         
{

              CSExceptionHandler handler 
= Events[EventUnhandledException] as CSExceptionHandler;

              
if (handler != null)

              
{

                   handler(csEx,
new CSEventArgs());

              }


         }


 

         
#endregion


 

     }


}


 
文件太长,我们抓出关键的部分来分析:
public delegate void CSExceptionHandler(CSException csEx, CSEventArgs e);

这里先申明一个委托,相当于一个函数指针。在通俗一点理解它就是一个跑腿的,专管传递对象与对象间的调用信息。
接下来:
internal static CSApplication Instance()

         
{

              
const string key = "CSApplication";

              CSApplication app 
= CSCache.Get(key) as CSApplication;

              
if(app == null)

              
{

                   
lock(sync)

                   
{

                       app 
= CSCache.Get(key) as CSApplication;

                       
if(app == null)

                       
{

                            CSConfiguration config 
= CSContext.Current.Config;

 

                            XmlNode node 
= config.GetConfigSection("CommunityServer/CSModules");

                            app 
= new CSApplication();

                            
if(node != null)

                            
{

                                 
foreach(XmlNode n in node.ChildNodes)

                                 
{

                                     
if(n.NodeType != XmlNodeType.Comment)

                                     
{

                                          
switch(n.Name)

                                          
{

                                               
case "clear":

                                                   app.modules.Clear();

                                                   
break;

                                               
case "remove":

                                                   app.modules.Remove(n.Attributes[
"name"].Value);

                                                   
break;

                                               
case "add":

                                               

                                                   
string name = n.Attributes["name"].Value;

                                                   
string itype = n.Attributes["type"].Value;

 

                                                   Type type 
= Type.GetType(itype);

 

                                                   
if(type == null)

                                                        
throw new Exception(itype + " does not exist");

 

                                                   ICSModule mod 
= Activator.CreateInstance(type) as ICSModule;

 

                                                   
if(mod == null)

                                                        
throw new Exception(itype + " does not implement ICSModule or is not configured correctly");

 

                                                   mod.Init(app, n);

                                                   app.modules.Add(name,mod);

 

 

                                                   
break;

 

                                          }


                                     }


                                 }


                            }


                            CacheDependency dep 
= new CacheDependency(nullnew string[]{CSConfiguration.CacheKey});

                            CSCache.Max(key, app,dep);

                       }


 

                       

                   }


              }


              
return app;

         }


这段很重要,通过读取communityserver.config文件的<CSModules>,初始化每个CSModule,注意,初始化后并且调用了这些CSModule中的Init方法。具体看看这些Module中的Init都做了什么,打开CommunityServerComponents项目下的Components文件夹中的CSExceptionModule.cs:
using System;

using System.Web;

 

namespace CommunityServer.Components

{

     
/// <summary>

     
/// Summary description for CSExceptionModule.

     
/// </summary>


     
public class CSExceptionModule : ICSModule

     
{

         
public CSExceptionModule()

         
{

              
//

              
// TODO: Add constructor logic here

              
//

         }


          
#region ICSModule Members

 

         
public void Init(CSApplication csa, System.Xml.XmlNode node)

         
{

              csa.CSException 
+=new CSExceptionHandler(csa_CSException);

         }


 

         
#endregion


 

         
private void csa_CSException(CSException csEx, CSEventArgs e)

         
{

              CSContext csContext 
= CSContext.Current;

 

              
if (csEx.ExceptionType != CSExceptionType.UnknownError && csContext.IsWebRequest) 

              
{

                   RedirectToMessage(csContext.Context, csEx);

              }
 

         }


 

         
private static void RedirectToMessage (HttpContext context, CSException exception) 

         
{

 

              
if ((exception.InnerException != null&& ( exception.InnerException is CSException)) 

              
{

                   CSException inner 
= (CSException) exception.InnerException;

              }


              context.Response.Redirect(Globals.GetSiteUrls().Message( exception.ExceptionType ), 
true);

         }


 

     }


}


哈哈,原来在Init方法里把一个CSExceptionHandler委托添加到CSException事件上,这个委托指向csa_CSException方法,还是通俗点说:如果CSException这个事件发生了,CSExceptionHandler这个跑腿的委托就会马上告诉csa_CSException方法要他执行,如果事件没有被激发就什么也不做。
名词: event 关键字使您得以指定当代码中的某些“事件”发生时调用的委托。此委托可以有一个或多个关联的方法,当代码指示该事件已发生时将调用关联的方法。
 
那么这个CSException又是怎么回事?在哪里定义的?我们回到CSApplication.cs文件中,看样几行:

这是访问器的声明,用于添加或移除客户代码中的事件处理程序,这样做的好处是公开大量的事件但不为每个事件分配字段,而是使用EventHandlerList存储这些事件实例。为了理解事件的调用执行过程,我们还必须看几个文件:CSEvents.cs、CSEventArgs.cs:

         public event CSExceptionHandler CSException

         
{

              add
{Events.AddHandler(EventUnhandledException, value);}

              remove
{Events.RemoveHandler(EventUnhandledException, value);}

     }

这里定义了一个CSException事件,而事件发生的时候只能用CSExceptionHandler这个委托来做跑腿的。其实CS中是把委托都存放在了一个EventHandlerList中,因此此处你可以看到add与remove,
CSEventArgs.cs存储事件的数据,这个很好理解,它继承自EventArgs。当事件发生时CSEventArgs用来传递事件的信息,这里传递两个值:ObjectState与ApplicationType(可以在Enumerations文件夹下找到这两个枚举的内容)
CSEvents.cs这是对事件调用的一个包装器,看异常处理的包装:

         public static void CSException(CSException csEx)

         
{

              CSApplication.Instance().ExecuteCSExcetion(csEx);

          }

这里先调用CSApplication.Instance()方法,实例化一个CSApplication对象,如果你是第一次调用Instance()方法,就实例化所有在<CSModules>中配置的类,并且调用他们的Init方法(在CSModules中配置的这些类,都实现了ICSModule接口,而这个接口要求继承他的类都具备Init方法),执行Init方法的目的就是把委托添加到事件上,使委托指向的方法可以在事件触发的时候被调用。实例化后再调用ExecuteCSExcetion方法并且传递CSException的实例,ExecuteCSExcetion方法如下:

         internal void ExecuteCSExcetion(CSException csEx)

         
{

              CSExceptionHandler handler 
= Events[EventUnhandledException] as CSExceptionHandler;

              
if (handler != null)

              
{

                   handler(csEx,
new CSEventArgs());

              }


         }

先通过对EventHandlerList索引访问,即Events[EventUnhandledException],从列表中找到这个CSExceptionHandler事件,如果不为null就执行它。EventUnhandledException又是什么,其实这只是一个Key,用来标示存储的事件。
 
有必要总结一下,不然你会被这种调用来调用去的关系搞得一头雾水,
 
      
 
以异常处理为例:
1:在错误发生后,调用Application_OnError方法;
2:在方法的最后调用CSEvents.CSException(csException);
3:进入CSEvents包装器,调用CSApplication.Instance().ExecuteCSExcetion(csEx);
4:执行CSApplication.Instance()方法,如果是第一次执行就根据communityserver.config文件中的配置,把所有的CSModules实例化,并且调用ICSModule接口类中的Init方法,然后缓存这些实例化的类(如果是第二次访问就从缓存中读取)。
5:在实现ICSModule接口的类中,如CSExceptionModule.cs,Init方法是给事件添加委托的过程,这个过程中实现了委托指向的一个或者多个方法与事件进行关联,异常处理的方法csa_CSException(CSException csEx, CSEventArgs e)就是在这里被关联到异常事件上的。
6:经过上面几步后,CS系统接着调用ExecuteCSExcetion方法,在ExecuteCSExcetion方触发了CSException事件
7:CSException事件被触发后,就执行事件中委托所指向的函数,这里是CSExceptionModule.cs文件中的private void csa_CSException(CSException csEx, CSEventArgs e)
 
CS如此大量的使用Delegates与Events带来了什么,也许你会认为它这样是把问题复杂化,而且觉得这非常没有必要,完全可以在异常处理的最后调用处理方法即可,何必通过事件来回周转!最后说明一下这样做的重要性:
1:通过事件使调用方法者与方法本身隔离,如在CSHttpModule.cs文件中的Application_OnError方法触发CSEvents.CSException事件,而事件要做些什么处理,需要调用什么方法Application_OnError根本不知道。如果你要改变CSEvents.CSException事件处理方法的结构,甚至十处理方法的名称,Application_OnError也不需要改动,因为他根本不关心具体的实现,它的任务只是触发这个事件。
2:如果你想一个方法调用多个方法,普通的做法就是在方法中一次调用或者在方法中嵌套调用。这样做并不是一个好的设计模式,而事件可以通过委托调用多个委托指向的方法(在异常处理中只指向了一个方法,当然你可以指向任意N个方法),而这种调用也是相互隔离的,被调用的方法并不致到谁调用它,而调用者也不关心它调用谁。
3:模块化,你的代码直接没有非常的多紧密联系,而是通过事件来通知处理方法,在CS中又加入了xml的配置文件,使得这样的模块化更突出,你甚至可以把处理异常的类单独编译在一个dll中。
好处还有很多…
 
在CS中,对Post内容的不良信息过滤也是通过这样的机制完成的,运行的过程基本一致,只是调用不同的事件处理方法,你可以自己分析。
posted on 2005-09-12
 

Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1538951


 
基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
MPU6050是一款广泛应用在无人机、机器人和运动设备中的轴姿态传感器,它集成了三轴陀螺仪和三轴加速度计。这款传感器能够实时监测并提供设备的角速度和线性加速度数据,对于理解物体的动态运动状态至关重要。在Arduino平台上,通过特定的库文件可以方便地与MPU6050进行通信,获取并解析传感器数据。 `MPU6050.cpp`和`MPU6050.h`是Arduino库的关键组成部分。`MPU6050.h`是头文件,包含了定义传感器接口和函数声明。它定义了类`MPU6050`,该类包含了初始化传感器、读取数据等方法。例如,`begin()`函数用于设置传感器的工作模式和I2C地址,`getAcceleration()`和`getGyroscope()`则分别用于获取加速度和角速度数据。 在Arduino项目中,首先需要包含`MPU6050.h`头文件,然后创建`MPU6050`对象,并调用`begin()`函数初始化传感器。之后,可以通过循环调用`getAcceleration()`和`getGyroscope()`来不断更新传感器读数。为了处理这些原始数据,通常还需要进行校准和滤波,以消除噪声和漂移。 I2C通信协议是MPU6050与Arduino交互的基础,它是一种低引脚数的串行通信协议,允许多个设备共享一对数据线。Arduino板上的Wire库提供了I2C通信的底层支持,使得用户无需深入了解通信细节,就能方便地与MPU6050交互。 MPU6050传感器的数据包括加速度(X、Y、Z轴)和角速度(同样为X、Y、Z轴)。加速度数据可以用来计算物体的静态位置和动态运动,而角速度数据则能反映物体转动的速度。结合这两个数据,可以进一步计算出物体的姿态(如角度和角速度变化)。 在嵌入式开发领域,特别是使用STM32微控制器时,也可以找到类似的库来驱动MPU6050。STM32通常具有更强大的处理能力和更多的GPIO口,可以实现更复杂的控制算法。然而,基本的传感器操作流程和数据处理原理与Arduino平台相似。 在实际应用中,除了基本的传感器读取,还可能涉及到温度补偿、低功耗模式设置、DMP(数字运动处理器)功能的利用等高级特性。DMP可以帮助处理传感器数据,实现更高级的运动估计,减轻主控制器的计算负担。 MPU6050是一个强大的轴传感器,广泛应用于各种需要实时运动追踪的项目中。通过 Arduino 或 STM32 的库文件,开发者可以轻松地与传感器交互,获取并处理数据,实现各种创新应用。博客和其他开源资源是学习和解决问题的重要途径,通过这些资源,开发者可以获得关于MPU6050的详细信息和实践指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值