Analysis of ASP.NET ForumS2 Structure Process and Components Design by adding favorites functions

xiaoxiao2021-03-06  32

TD ID = "1" class = "controlpaneltabinactive" align = "center" nowrap> <% = ResourceManager.getstring (" myfavorites_title ")%> Modification: crossing the number of columns. Increase the corresponding file performance Layer 1, Favorites Increase Myfavorites.aspx in Web / User / Directory, End User Page In Controls / Views Directory Add MyFavoritesView.cs, page view server control (mainly as page processing logic) interface: in Web / Themes / Default / Skins Add View-MyFavorites.ascx Favorites (mainly as page UI) Components Add Favorites.cs in the Components Directory (equivalent to the business logic layer, plus S table business processing), this example is not in subdirectory In Components / Components, Favorite.cs (equivalent to business entity, unpigneous Skumes entities), due to not, the complete ASP.NET Forums pattern should have this layer.

Expression layer 2, the user clicks the UI rendered after the Favorites (this relatively simple) adds myfavoritesadd.aspx file processing to the home control, add myfavoritesadd.cs in the controls directory (page processing logic) in Web / Themes / Default / Skins Add Skin-MyFavoritesAdd.ascx to add the topic to the view (UI) six. Added to the database table forums_FavoritesUserID int 4 0ThreadID int 4 0FavoriteDate datetime 8 0 Create a stored procedure forums_Favorites_CreateDeleteCREATE procedure forums_Favorites_CreateDelete (@UserID int, @ThreadID int, @Action int) ASBEGINIF @Action = 0BEGIN - Does the user already have the ability to see this thread ? IF EXISTS (SELECT UserID FROM forums_Favorites WHERE UserID = @UserID and ThreadID = @ThreadID) return INSERT INTO forums_Favorites VALUES (@UserID, @ThreadID, getdate ()) RETURNENDIF @Action = 2BEGIN DELETE forums_Favorites WHERE UserID = @UserID aND ThreadID = . @ThreadID RETURNENDENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO seven data processing 1.Components / Provider / ForumsDataProvider.cs increase #region lightbox public abstract void CreateFavorites (ArrayList users, int threadID); public abstract void DeleteFavorites (int userID, ArrayList deleteList) ; #ENDREG Ion2. Data Providers / SqldataProvider / SqldataProvider.cs Add to Implement Method #Region #### Favorites #### by venjiang 0912 ///

/// Additional topic to favorites /// / // User ID /// Topic ID public override void createfavorites (intend) {using (SqlConnection myconnection =

GetSqlConnection ()) {SqlCommand myCommand = new SqlCommand (databaseOwner ".forums_Favorites_CreateDelete", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add ( "@ Action", SqlDbType.Bit) .Value = DataProviderAction.Create MyCommand.Parameters.Add ("@ userid", sqldbtype.int); mycommand.parameters.add ("@ threadid", sqldbtype.int); myconnection.open (); mycommand.parameters ["@ userid"]. Value = UserID; mycommand.Parameters ["@ threadid"]. value = threadid; mycommand.executenonquery ();}} ///

/// from the favorites /// ////// Delete list public override void deletefavorites (int userid, arraylist deletelist) {// create instance of connection and Command Object using (SqlConnection myConnection = GetSqlConnection ()) {SqlCommand myCommand = new SqlCommand (databaseOwner ".forums_Favorites_CreateDelete", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add ( "@ Action", SqlDbType.Int). Value = dataprovideraction.delete; mycommand.parameters.add ("@ userid", sqldbtype.int) .value = userid; mycommand.parameters.add ("@ threadid", sqldbtype.int);

// Open the connection myConnection.Open (); // Add multiple times // foreach (int threadID in deleteList) {myCommand.Parameters [ "@ ThreadID"] Value = threadID;. MyCommand.ExecuteNonQuery ();}}} # endregion3. GetThreads modification methods Data Providers / SqlDataProvider / SqlDataProvider.cs, to support the collection features #region #### Threads #### // postings increase collections by venjiang 0911 public override 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 returnRecordC ount, // add a new parameter, if only to display your favorite topics bool favoriteOnly) {// Create Instance of Connection and Command Object // using (SqlConnection connection = GetSqlConnection ()) {SqlCommand command = new SqlCommand (databaseOwner ".forums_Threads_GetThreadSet ", connection); command.CommandType = CommandType.StoredProcedure; ThreadSet threadSet = new ThreadSet (); StringBuilder sqlCountSelect = new StringBuilder (" SELECT count (T.ThreadID) "); StringBuilder sqlPopulateSelect = new StringBuilder ("

SELECT T.ThreadID, HasRead = "); StringBuilder fromClause = new StringBuilder (" FROM " this.databaseOwner " .forums_Threads T "); StringBuilder whereClause = new StringBuilder (" WHERE "); StringBuilder orderClause = new StringBuilder (" ORDER "); // Add a collection judgment by Venjiang 0911 if (FavoriteOnly == true) {fromClause.Append (" this.Databaseowner ".forums_favorites fav");} // EnSure DateTime is min value for SQL / / threadsNewerThan = SqlDataProvider.GetSafeSqlDateTime (threadsNewerThan);. // Construct the clauses #region Constrain Forums // Contrain the selectivness to a set of specified forums The forumID is our // clustered index so we want this to be first if (forumID> 0) {whereclause.Append ("T.forum ID = "); WHERECLASE.APpend (forumID);} else if (forumid <0) {whereclause.Append (" (t.forumid = "); // Get a list of all the forums the user Has Access to // ArrayList forumlist = forums.getforum (userid, false, true); for (int i = 0; i 0) {IF I 1) <

Forumlist.count) {whereclause.Append ((forum) forumlist [i]). ForumID "OR T.forumId =");} else {whereclause.append ((forum) forumlist [i]). ForumID); WHERECLAUSE.APPEND (")");}}}} else {whereclause.Append ("t.forumid = 0 and p.userid ="); whereclause.append (userid); whereclause.append ("and p.threadID = T.ThreadID "); fromClause.Append (", " this.databaseOwner " .forums_PrivateMessages P ");} #endregion #region Constrain Date whereClause.Append (" AND StickyDate> = ' "); whereClause.Append (threadsNewerThan .Tostring (system.globalization.cultureinfo.currentculture.datetimeformat.SortableDatetimePattern); Where Clause.Append ( " '"); #endregion #region Constain Approval whereClause.Append ( "AND IsApproved = 1"); #endregion #region Constrain Read / Unread if (userID> 0) {sqlPopulateSelect.Append ( "(SELECT" this.DatabaseOwner ".haasreadpost ("); sqlpopulateselect.Append (UserID); SqlpopulateRect.Append (", T.THREADID, T.FORUMID)") "); if (unreadonly) {whereclause.Append (" and "

THISDATABASEOWNER ".haasreadpost ("); WHERECLASE.APpend (userid); WHERECLASE.APPEND (", T.THREADID, T.FORUMID) = 0");}} else {sqlpopulateselectr.Append ("0"); } #endregion #region Unanswered topics if (unansweredOnly) {whereClause.Append ( "AND TotalReplies = 0 AND IsLocked = 0");} #endregion #region Active topics // Top posts if (activeTopics) {whereClause.Append ( " And TotalReplies> 2 and islocked = 0 and TotalViews> 50 ");} #endregion #Region Favorites // Show the topic IF (FavoriteOnly) {whereclause.Append (" and t.threadid = Fav.ThreadID) = "); WHERECLASE.APPEND (UserID); } #Endregion #region Users filter if (userFilter = ThreadUsersFilter.All!) {If ((userFilter == ThreadUsersFilter.HideTopicsParticipatedIn) || (userFilter == ThreadUsersFilter.HideTopicsNotParticipatedIn)) {whereClause.Append ( "AND"); whereClause. Append (userid); if (userfilter == threadusersfilter.hidetopicsNotParticipatedin) Whereclause.Append ("not"); WHERECLASE.APPEND ("in (SELECT Userid from"

this.databaseOwner ".forums_Posts P WHERE P.ThreadID = T.ThreadID)");} else {if (userFilter == ThreadUsersFilter.HideTopicsByNonAnonymousUsers) whereClause.Append ( "AND 0 NOT"); else whereClause.Append ( "AND 0 "); WHERECLASE.APpend (" SELECT Userid from " this.DatabaseOwner " .forums_posts p where threadid = t.threadid and p.userid = 0) ")") ")") "#ENDREGION #REGION THREAD Status if ! threadStatus = ThreadStatus.NotSet) {switch (threadStatus) {case ThreadStatus.Open: whereClause.Append ( "AND ThreadStatus = 0"); break; case ThreadStatus.Closed: whereClause.Append ( "AND ThreadStatus = 0"); brea k; case ThreadStatus.Resolved: whereClause.Append ( "AND ThreadStatus = 0"); break; default: break;}} #endregion #region Order By switch (sortBy) {case SortThreadsBy.LastPost: if (sortOrder == SortOrder. Ascending) {if (activetopics || unansweredonly) ORDERCLASE.APPEND ("threaddate");

else orderClause.Append ( "IsSticky, StickyDate");} else {if (activeTopics || unansweredOnly) orderClause.Append ( "ThreadDate DESC"); else orderClause.Append ( "IsSticky DESC, StickyDate DESC");} break; case SortThreadsBy.TotalRatings: if (sortOrder == SortOrder.Ascending) orderClause.Append ( "TotalRatings"); else orderClause.Append ( "TotalRatings DESC"); break; case SortThreadsBy.TotalReplies: if (sortOrder == SortOrder.Ascending) orderClause .Append ("totalreplies"); else OrderClause.Append (TotalReplies DESC "); break; case SortThreadsBy.ThreadAuthor: if (sortOrder == SortOrder.Ascending) orderClause.Append ( "PostAuthor DESC"); else orderClause.Append ( "PostAuthor"); break; case SortThreadsBy.TotalViews: if (sortOrder == SortOrder. OrderClause.Append ("TotalViews"); Else OrderClause.Append (TotalViews DESC ");

break;} #endregion // Build the SQL statements sqlCountSelect.Append (fromClause.ToString ()); sqlCountSelect.Append (whereClause.ToString ()); sqlPopulateSelect.Append (fromClause.ToString ()); sqlPopulateSelect.Append (whereClause. TOSTRING ()); Sqlpopulaterect.Append (ORDERCLASE.TOSTRING ()); // add parameters to sproc // command.parameters.add ("@ forumid", sqldbtype.int) .value = forumid; command.parameters.add ( "@PageIndex", SqldbType.int, 4) .value = pageindex; command.parameters.add ("@ pagesize", sqldbtype.int, 4) .value = Pagesize; Command.Parameters.Add ("@ Sqlcount", SqldbType .Nvarchar, 4000) .value = SqlcountSelect.toString (); Command.Parameters.Add ("@ Sqlpopulate", SqldbType.nvarchar, 4000) .Value = Sqlpopulatelect.toString (); Command.Parameters.Add ("@ US) erID ", SqlDbType.Int) .Value = userID; command.Parameters.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) RecipientSlookupt able [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 8. Add a new method to add a new overload method in Components / Threads.cs, in order not to modify the original method call. In order not to affect the previous program, add an overload method separately to get a favorites. theme 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) // more than one parameter 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;

转载请注明原文地址:https://www.9cbs.com/read-40110.html

New Post(0)