sharepoint对象模型性能比较

sharepoint的对象模型的属性和方法有多种,可能对于一些初学者不明白他们之间有什么区别,那种更优越,在何种情况下用那种属性或方法,通过这篇文章你会有所了解

 

1,splist

         有些人喜欢通过列表的名字去获得列表,

            using (SPSite site = new SPSite(strSite))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList oList = web.Lists ["MyList"]

                }

             }

        这个例子中,代码会先加载web下的所有OList,返回所有OList的title,然后去一个个比较title与listname后,得到OList;

using (SPSite site = new SPSite(strSite))

      {

       using (SPWeb web = site.OpenWeb())

        {

          SPList oList = web.GetList("http://Site/list/AllItem.aspx")

        }

      }

   在这个例子中,代码先根据url返回列表的guid,然后再根据guid去找web下的list

2,SPListItemColliction

using (SPSite site = new SPSite(strSite))

            {

                using (SPWeb web = site.OpenWeb())

                {

                  SPList oList = web.GetList("http://Site/list/AllItem.aspx");

                    for(int idx =0; idx< oList.ItemCount; idx ++)

                    {

                        string strLstItemName = oList.Items[idx].Name;

                    }

                }

            }

在这个例子中,   oList.Items[idx]每次都会加载一个SpListItemColliction,比如,如果你的列表有10000个Item,

oList.Items[idx]每次都会加载这10000个Item,这将会消耗不少内存去存这个SpListItemColliction;

 

using(SPSite site = new SPSite(strSite))

            {

                using (SPWeb web = site.OpenWeb())

                {

                   SPList oList = web.GetList("http://Site/list/AllItem.aspx");

                    SPListItemCollection oListItems = oList.Items;

                    for(int idx =0; idx< oList.ItemCount; idx ++)

                    {

                        string strLstItemName = oListItems[idx].Name;

                    }

                }

            }

在这个例子中,先去得到一个SpListItemColliction,这个内存只需去加载一次SpListItemColliction

3,SPListItem in event handlers

public override void ItemAdded(SPItemEventProperties properties)

 {

  using (SPSite oSite = new SPSite(properties.WebUrl))

   {

    using (SPWeb oWeb = oSite.OpenWeb())

    {

     SPList oList = oWeb.Lists[properties.ListId];

     SPListItem oListItem = oList.GetItemByUniqueId(properties.ListItemId);

    }

   }

 }

在这种情况下,我们多余的装载对象消耗了APIs,并且将装载一个SpListItemColliction,请看下面的方法:

public override void ItemAdded(SPItemEventProperties properties)

 {

  SPListItem oItem = properties.ListItem;

 }

这用该不用多说了吧

4,SPListItem Count

using (SPSite site = new SPSite(strSite))

      {

       using (SPWeb web = site.OpenWeb())

        {

          SPList oList = web.GetList("http://Site/list/AllItem.aspx");

          int iCount = oList.Items.Count;

        }

      }

oList.Items.Count,首先在内存中加载所有的items,并计算出Count;

 

using (SPSite site = new SPSite(strSite))

      {

       using (SPWeb web = site.OpenWeb())

        {

          SPList oList = web.GetList("http://Site/list/AllItem.aspx");

          int iCount = oList.ItemsCount;

        }

      }

ItemsCount是SPList的属性,没有过多的加载Items

5,Items

 

SPList.Items.Count

 

SPList.ItemsCount

 

SPList.Items[Guid]

 

SPList.GetItemByUniqueId(Guid)

 

SPList.Items[Int32]

 

SPList.GetItemById(Int32)

 

SPList.Items.GetItemById(Int32)

 

SPList.GetItemById(Int32)

列表左面的做了额外功,右面高效

posted @ 2011-05-12 04:58  Jason.Bird  阅读(209)  评论(0编辑  收藏  举报