有感于blogengine sqlprovider

in Develop

昨天晚上就在弄这个东东
在blogengine本身提供的MSSQLBlogProvider.cs上做了些许修改
数据导入成功后,将blog的defaultProvider换成了sql
但随之问题就来了:
1、载入特别慢,远远慢于xmlprovider
2、发表评论后,迟迟不出现回调的结果,始终显示”saving……”
观察了一下CommentView.ascx.cs的代码,

public void RaiseCallbackEvent(string eventArgument)
{

//部分代码省略
Post.AddComment(comment);
SetCookie(author, email, website, country);

string path = "~/themes/" + BlogSettings.Instance.Theme + "/CommentView.ascx";

CommentViewBase control = (CommentViewBase)LoadControl(path);
control.Comment = comment;
control.Post = Post;

using (StringWriter sw = new StringWriter())
{
control.RenderControl(new HtmlTextWriter(sw));
_Callback = sw.ToString(); //这里是评论发表成功后回调返回的值,实际上就是评论列表
}
}

继续跟进 Post.AddComment(comment);方法

public void AddComment(Comment comment)
{
CancelEventArgs e = new CancelEventArgs();
OnAddingComment(comment, e);
if (!e.Cancel)
{
Comments.Add(comment);
DataUpdate(); //跟进这一行代码
OnCommentAdded(comment);
SendNotifications(comment);
}
}

DataUpdate

protected override void DataUpdate()
{
BlogService.UpdatePost(this); //这里
Posts.Sort();
}

在BlogService.UpdatePost中调用了 _provider.UpdatePost(post);方法
_provider.UpdatePost(Post)是个抽象方法,供sqlprovider或xmlprovider重写
在可爱的sqlprovider代码中

public override void UpdatePost(Post post)
{

string sqlQuery = "UPDATE be_Posts " +
"SET Title = @title, Description = @desc, PostContent = @content, " +
" DateModified = @modified, Author = @Author, " +
"IsPublished = @published, IsCommentEnabled = @commentEnabled, " +
"Raters = @raters, Rating = @rating, Slug = @slug,[IsWYSWYG]=@iswyswyg,[IsTop]=@istop " +
"WHERE PostID = @id";

SqlParameter[] para = new SqlParameter[13];
para[0] = new SqlParameter("@id", post.Id);
para[1] = new SqlParameter("@title", post.Title);
para[2] = new SqlParameter("@desc", post.Description);
para[3] = new SqlParameter("@content", post.Content);
para[4] = new SqlParameter("@modified", DateTime.Now);
para[5] = new SqlParameter("@author", post.Author ?? string.Empty);
para[6] = new SqlParameter("@published", post.IsPublished);
para[7] = new SqlParameter("@commentEnabled", post.IsCommentsEnabled);
para[8] = new SqlParameter("@raters", post.Raters);
para[9] = new SqlParameter("@rating", post.Rating);
para[10] = new SqlParameter("@slug", post.Slug);
para[11] = new SqlParameter("@iswyswyg", post.IsWYSWYG);
para[12] = new SqlParameter("@istop", post.IsTop);

//更改了post
SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sqlQuery, para);

// 更新Tags
UpdateTags(post);

// 更新Categories
UpdateCategories(post);

// 更新Comments
UpdateComments(post);

// 更新Email Notification
UpdateNotify(post);
}

这个方法中做了N多"无用的操作",也许作者在设计时最大程度上在UpdatePost方法中要满足所有post相关的操作,
才会在该方法中连续调用其它数据处理
但这样会造成很多很多的冗余操作,比如:我只提交评论,而UpdatePost中却将Post/comment/Tag/category等全更新了一次!
在数据量大的情况下,这个sql的io并不亚于xml的io操作.
欢迎大家说说自己的看法,作者这样设计的目的是什么?

3 Comments

3 Comments

  1. 这个,没仔细看过。没资格发言!

  2. 我也没有深入地去看过
    就看了这一部分,所以感觉有点多余。
    我估计这里的UpdatePost在很多地方用到了
    比如评论通知、更新tag、更新categorys……

  3. 哥哥好厉害!
    pr7的站点哦

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>