BlogEngine.net 代码学习笔记(一)

计划将blogEngine的代码阅读过程及不懂的地方全部记下来,不知道是否能坚持得住,我用的是1.2版板:
(虽然已不能算是初学者,可是本人记性实在太差,错误之处还请指证:
blogEngine类结构可以参考:
http://www.cnblogs.com/jisiki/archive/2007/04/20/721217.html
以下列出最先准备研究的几个类结构:
BlogEngine.net 代码学习笔记(一)
 

BlogProvider:所有数据提供的基类

MSSQLBlogProvider:数据库访问方式的数据提供和保存

拿其中的一个函数来进行说明:

public class MSSQLBlogProvider : BlogProvider

 {

//提供一个连接:   

private SqlConnection providerConn;

    #region Posts

    ///<summary>

    /// Retrieves a post based on the specified Id.

    ///</summary>

    public override Post SelectPost(Guid id)

    {

     //OpenConnection()用于初始化providerConn;

bool connClose = OpenConnection();

     

      Post post = new Post();

      string sqlQuery = "SELECT PostID, Title, Description, PostContent, DateCreated, " +

                          "DateModified, Author, IsPublished, IsCommentEnabled, Raters, Rating, Slug " +

                          "FROM be_Posts " +

                          "WHERE PostID = @id";

//SqlCommand:表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程。

//要完成此操作,需将一个 SqlConnection、一个查询字符串(它是一个 Transact-SQL SELECT 语句)和 SqlParameter 对象的一个数组传递给该方法。

      SqlCommand cmd = new SqlCommand(sqlQuery, providerConn);

      cmd.Parameters.Add(new SqlParameter("@id", id.ToString()));

//使用DataReader可以提高应用程序的性能,是将数据输出到表示层重要的手段。在创建了一个Command对象之后,通过调用Command.ExecuteReader方法再创建DataReader对象,就能够使用Read方法从数据源检索记录集了。

      SqlDataReader rdr = cmd.ExecuteReader();

      rdr.Read();

      post.Id = rdr.GetGuid(0);

      post.Title = rdr.GetString(1);

      post.Content = rdr.GetString(3);

      if (!rdr.IsDBNull(2))

        post.Description = rdr.GetString(2);

      if (!rdr.IsDBNull(4))

        post.DateCreated = rdr.GetDateTime(4);

      if (!rdr.IsDBNull(5))

        post.DateModified = rdr.GetDateTime(5);

      if (!rdr.IsDBNull(6))

        post.Author = rdr.GetString(6);

      if (!rdr.IsDBNull(7))

        post.IsPublished = rdr.GetBoolean(7);

      if (!rdr.IsDBNull(8))

        post.IsCommentsEnabled = rdr.GetBoolean(8);

      if (!rdr.IsDBNull(9))

        post.Raters = rdr.GetInt32(9);

      if (!rdr.IsDBNull(10))

        post.Rating = rdr.GetFloat(10);

      if (!rdr.IsDBNull(11))

        post.Slug = rdr.GetString(11);

      else

        post.Slug = "";

//需要着重指出的是在使用完DataReader后应该显式的关闭DataReader对象。

      rdr.Close();

      // Tags

      sqlQuery = "SELECT Tag " +

                  "FROM be_PostTag " +

                  "WHERE PostID = @id";

      cmd.CommandText = sqlQuery;

      rdr = cmd.ExecuteReader();

      while (rdr.Read())

      {

        if (!rdr.IsDBNull(0))

          post.Tags.Add(rdr.GetString(0));

      }

      rdr.Close();

      // Categories

      sqlQuery = "SELECT CategoryID " +

                  "FROM be_PostCategory " +

                  "WHERE PostID = @id";

      cmd.CommandText = sqlQuery;

      rdr = cmd.ExecuteReader();

      while (rdr.Read())

      {

        Guid key = rdr.GetGuid(0);

        if (Category.GetCategory(key) != null)

            post.Categories.Add(Category.GetCategory(key));

      }

      rdr.Close();

      // Comments

      sqlQuery = "SELECT PostCommentID, CommentDate, Author, Email, Website, Comment, Country, Ip, IsApproved " +

                  "FROM be_PostComment " +

                  "WHERE PostID = @id";

      cmd.CommandText = sqlQuery;

      rdr = cmd.ExecuteReader();

      while (rdr.Read())

      {

        Comment comment = new Comment();

        comment.Id = rdr.GetGuid(0);

        comment.IsApproved = true;

        comment.Author = rdr.GetString(2);

        if (!rdr.IsDBNull(4))

        {

          Uri website;

          if (Uri.TryCreate(rdr.GetString(4), UriKind.Absolute, out website))

            comment.Website = website;

        }

        comment.Email = rdr.GetString(3);

        comment.Content = rdr.GetString(5);

        comment.DateCreated = rdr.GetDateTime(1);

        comment.Post = post;

        if (!rdr.IsDBNull(6))

          comment.Country = rdr.GetString(6);

        if (!rdr.IsDBNull(7))

          comment.IP = rdr.GetString(7);

        if (!rdr.IsDBNull(8))

          comment.IsApproved = rdr.GetBoolean(8);

        else

          comment.IsApproved = true;

        post.Comments.Add(comment);

      }

      post.Comments.Sort();

      rdr.Close();

      // Email Notification

      sqlQuery = "SELECT NotifyAddress " +

                  "FROM be_PostNotify " +

                  "WHERE PostID = @id";

      cmd.CommandText = sqlQuery;

      rdr = cmd.ExecuteReader();

      while (rdr.Read())

      {

        if (!rdr.IsDBNull(0))

          post.NotificationEmails.Add(rdr.GetString(0));

      }

      rdr.Close();

      if (connClose)

        providerConn.Close();

      return post;

}

 private bool OpenConnection()

    {

      bool result = false;

      // Initial if needed

      if (providerConn == null)

        providerConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString);

      // Open it if needed

      if (providerConn.State == System.Data.ConnectionState.Closed)

      {

        result = true;

        providerConn.Open();

      }

      return result;

    }


你可能感兴趣的:(Engine)