Original article: http://www.cnblogs.com/andrewbao/archive/2005/01/05/87061.html
Download cnforumsbeta1 (the cnforum1.2 test version has been released today. You can download it from the development lab or visit the Baoyu blog) for a while.CodeHowever, its project structure is really complicated (and its own capabilities are limited), and it is really difficult to start.
Recently, due to project reasons, I am very interested in how to collect online user information. I just took this opportunity to learn how to implement cnforum.
BTW:Search for "Asp.net user statistics" (such as Google search results) using Google, and find the first few articlesArticleThe content is almost the same, but some words and origins are slightly modified, just interesting. It looks like a huge copy of the article! Of course, this is not the focus of today's discussion. Let's look at the topic below.
The first step is to select the starting point. After cnforum is installed, statistics on user online information are displayed at the bottom left of the Forum homepage.
When you click "user online information", the detailed user online information is displayed. View the "user online information" link and find that it points to viewonline. aspx. Start with the viewonline. ASPX page.
View the HTML code of the viewonline. ASPX page and find that the main function is to display the whoisonlineview control under aspnetforums. controls.
View the whoisonlineview control code (under the subproject controls ).
The whoisonlineview class inherits the skinnedforumwebcontrol class. From the name, the skinnedforumwebcontrol class should not be the focus. The focus is on the initializeskin (control skin) method that is rewritten in the whoisonlineview class. The code in the method shows that the repeat control is used to display registered member and visitor information.
Since the methods for obtaining registered member information and tourist information should be the same, you only need to analyze the acquisition of online registered member information here.
The Code repeater. datasource = users. getusersonline (15) indicates that the online registration of member data is obtained through the getusersonline (INT pastminutes) method of the users class.
Go to the getusersonline method of the users class (under the sub-project components) and find that the data is obtained by calling the getmembersguestsonline (INT pastminutes) method of the users class.
Check the function body of the getmembersguestsonline method. The key code for getting online registered member data should be:
Forumsdataprovider dp = forumsdataprovider. instance ();
Users = DP. whoisonline (pastminutes );
Analyze forumsdataprovider dp = forumsdataprovider. instance ()
Go to the forumsdataprovider class (under the provider in the sub-project components) Static Method Instance ()-> Static Method Instance (httpcontext context, string providertypename, string databaseowner, string connectionstring ), the purpose is to return an instance of the forumsdataprovider class.
So what is the completion of the forumsdataprovider instance method?
First, use forumconfiguration Config = forumconfiguration. getconfig (); to obtain the object of the forumconfirguration class. Go to the getconfig method of forumconfirguration.
Strange things happen : The getconfig method has only one simple sentence (forumconfiguration) configurationsettings. getconfig ("forums/Forums"), but from the perspective of the forumconfirguration class definition, there are many private members (such as providers and defaultprovider ).
In the instance method of forumsdataprovider, the providers of config are used after the getconfig method is called. How are these private members initialized? It seems that xuanjicang is on the configurationsettings. getconfig method. Check msdn to know that this method is used to "return the configuration settings of the User-Defined configuration section", view web. in config, the forums/forums section is <section name = "Forums" type = "aspnetforums. configuration. forumsconfigurationhandler, aspnetforums. components "/>.
Search for the Section in msdn. The section "declare configuration" defines a new element for the configuration file. New Element inclusion configuration section processing Program (That is, implementation . The attributes and child elements of the defined section depend on the section handler used to read the settings .". It is hard to say that forumsconfigurationhandler refers to the custom configuration section handler in the project.
Search for a project. In the forumconfiguration. CS file (under the sub-project components/confirguration), find the forumsconfigurationhandler class, which implements the iconfigurationsectionhandler interface. Search for msdn and find that the iconfigurationsectionhandler interface has the create method. You can return your own configuration object.
Return to the Code. In forumsconfigurationhandler, the user obtains the forumconfiguration object and initializes the private member of the forumconfiguration object through loadvaluesfromconfigurationxml.
Oh, that's it! The seemingly simple return (forumconfiguration) configurationsettings. getconfig ("forums/Forums") has actually filled in the required configuration content.
[Isn't that cool? Don't hit me with bricks! Drool first ~~~~]
Return to the instance method of the forumsdataprovider class, and the subsequent code is to get the default configuration content (for details, you can view web. config), including the configuration information such as the database connection string, database owner, and Key The default provider type name providertypename is "aspnetforums. Data. sqldataprovider" in my project configuration. Remember this name and it will play a key role later!
>>> According to the Code, when the cache ["dataprovider"] is null, the type corresponding to the previously obtained providertypename is obtained. The following code is as follows: cache. insert ("dataprovider", type. getconstructor (paramtypes ));.
The question is, what does this code do?
The search by msdn knows that the type. getconstructor method is "get the specific constructor of the current type" (here it is specific to paramtypes, that is, there are two constructor of the string parameter ). According to the previous knowledge, the type is aspnetforums. data. sqldataprovider, which means there should be a class named sqldataprovider, and it looks like a constructor of sqldataprovider (string param1, string param2.
Search for projects... a class named sqldataprovider is found under the subproject sqldataprovider, And the constructor is sqldataprovider (string databaseowner, string connectionstring ). Original cache. insert ("dataprovider", type. getconstructor (paramtypes) refers to the constructorinfo corresponding to the defaultprovider (sqldataproiver, inherited from forumsdataprovider) configured in webconfig, through this constructinfo object, we can call the sqldataprovider constructor to obtain the sqldataprovider object.
The advantage of doing so is obvious. We only need to modify web. config to flexibly modify the classes used to access data.
Then, the sqldataprovider object is obtained through the invoke method of constructorinfo.
Object [] paramarray = new object [2];
Paramarray [0] = databaseowner;
Paramarray [1] = connectionstring;
Return (forumsdataprovider) (constructorinfo) cache ["dataprovider"]). Invoke (paramarray ));
Now, forumsdataprovider dp = forumsdataprovider. instance () actually obtains the sqldataprovider object. In the sqldataprovider class, all abstract methods defined in forumsdataprovider are rewritten to implement all required data access methods.
Analyze users = DP. whoisonline (pastminutes)
Now that the data access object is ready, users = DP. whoisonline (pastminutes) becomes just a method to call the sqldataprovider object.
Find the whoisonline method of the sqldataprovider class, that is, public override hashtable whoisonline (INT pastminutes)
This method is actually to call the Stored Procedure forums_users_online to view the stored procedure (this stored procedure has the input parameter @ pastminutes, where the value is 15 ).
It is found that it first deletes the online user information that has no activity in the last hour, namely:
Delete
Forums_usersonline
Where
Lastactivity <dateadd (hour,-1, getdate ())
Then, based on the data in the table forums_usersonline, find out information about registered members of the activity (activity) in the last 15 minutes, and find information about all online visitors from the table forums_anonymoususers.
OK. The Implementation Method of online user statistics in cnforum is analyzed here.
Comments: It is indeed a huge benefit to read the code of experts. I hope to write this post to make a good result and make progress together ~
Please forgive me for the errors that may inevitably occur during the analysis process. Of course, you are also welcome to throw bricks ..