打印本文 打印本文 关闭窗口 关闭窗口
以增加收藏夹功能为实例,解析asp.net forums2结构流程及组件设计
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4068  更新时间:2009/4/23 10:43:20  文章录入:mintao  责任编辑:mintao
meters.Add("@ReturnRecordCount", SqlDbType.Bit).Value = returnRecordCount;

                // Execute the command
                connection.Open();
                SqlDataReader dr = command.ExecuteReader();

                // Populate the ThreadSet
                //
                while (dr.Read()) {

                    // Add threads
                    //
                    if (forumID == 0)
                        threadSet.Threads.Add( ForumsDataProvider.PopulatePrivateMessageFromIDataReader (dr) );
                    else
                        threadSet.Threads.Add( ForumsDataProvider.PopulateThreadFromIDataReader(dr) );

                }

                // Do we need to return record count?
                //
                if (returnRecordCount) {

                    dr.NextResult();

                    dr.Read();

                    // Read the total records
                    //
                    threadSet.TotalRecords = (int) dr[0];

                }

                // Get the recipients if this is a request for
                // the private message list
                if ((forumID == 0) && (dr.NextResult()) ) {
                    Hashtable recipientsLookupTable = new Hashtable();

                    while(dr.Read()) {
                        int threadID = (int) dr["ThreadID"];

                        if (recipientsLookupTable[threadID] == null) {
                            recipientsLookupTable[threadID] = new ArrayList();
                        }

                        ((ArrayList) recipientsLookupTable[threadID]).Add(ForumsDataProvider.PopulateUserFromIDataReader(dr) );
                    }

                    // Map recipients to the threads
                    //
                    foreach (PrivateMessage thread in threadSet.Threads) {
                        thread.Recipients = (ArrayList) recipientsLookupTable[thread.ThreadID];
                    }

                }


                dr.Close();
                connection.Close();

                return threadSet;
            }
        }

        #endregion

八.增加新方法
在Components\Threads.cs增加新的重载方法,以不必修改原来的方法调用.
// 为了不影响以前的程序,单独加一个重载方法,以获得收藏夹主题
        public static ThreadSet GetThreads(int forumID, int pageIndex, int pageSize, int userID, DateTime threadsNewerThan, SortThreadsBy sortBy, SortOrder sortOrder, ThreadStatus threadStatus, ThreadUsersFilter userFilter, bool activeTopics, bool unreadOnly, bool unansweredOnly, bool returnRecordCount,bool favoriteOnly) // 多了一个参数favoriteOnly
        {
            ForumContext forumContext = ForumContext.Current;
            string anonymousKey = "Thread-" + forumID + pageSize.ToString() + pageIndex.ToString() + threadsNewerThan.DayOfYear.ToString() + sortBy + sortOrder + activeTopics.ToString() + unansweredOnly.ToString() + favoriteOnly.ToString();

            ThreadSet threadSet;

            // If the user is anonymous take some load off the db
            //
            if (userID == 0)
            {
                if (forumContext.Context.Cache[anonymousKey] != null)
                    return (ThreadSet) forumContext.Context.Cache[anonymousKey];
            }

            // Create Instance of the IDataProvider
            //
            ForumsDataProvider dp = ForumsDataProvider.Instance();

            // Get the threads
            //
            threadSet = dp.GetThreads(forumID, pageIndex, pageSize, userID, threadsNewerThan, sortBy, sortOrder, threadStatus, userFilter, activeTopics, unreadOnly, unansweredOnly, returnRecordCount,favoriteOnly);

            if (userID == 0)
                forumContext.Context.Cache.Insert(anonymousKey, threadSet, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.Low, null);

            return threadSet;
        }

九.业务逻辑层
Components目录中增加Favorites.cs,实现主题的增加删除方法
public static void AddFavoritesPost (int userID,int threadID) {

            ForumsDataProvider dp = ForumsDataProvider.Instance();

            dp.CreateFavorites(userID, threadID);

        }
        /// <summary>
        /// 删除收藏
      

上一页  [1] [2] [3] [4] [5] [6]  下一页

打印本文 打印本文 关闭窗口 关闭窗口