BDC 编程模型

上次修改时间: 2010年4月15日

适用范围: SharePoint Server 2010

示例 BDC 模型中,介绍了 Customer 外部内容类型。假定 Customer 外部内容类型的结构显示为 Customer (Name, ID, Phone no., Address),其中地址是另一个复杂类型。

在您为给定外部数据源的 Customer 外部内容类型定义模型,并将该模型保存在 BDC 元数据存储中之后,无论后端是数据库、Web 服务还是 .NET 连接器程序集,BDC 代码都可用于与显示方式相同的 Customer 外部内容类型进行交互(创建、读取、更新和删除)。此编程模式的主要好处是:不管应用程序尝试交互的外部数据源的类型如何,代码的外观将保持不变。

示例:在外部数据源中创建 Customer 实例的 BDC 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Web;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://ssdk-server/Pages/Default.aspx"))
                {
                    using (new Microsoft.SharePoint.SPServiceContextScope(SPServiceContext.GetContext(site)))
                    {
                        BdcService service = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
                        IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);

                        IEntity entity = catalog.GetEntity("http://ssdk-server/sdksamples", "Customer");
                        ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;


                        IView createView = entity.GetCreatorView("Create");
                        IFieldValueDictionary valueDictionary = createView.GetDefaultValues();
                        valueDictionary["Name"] = "some name";
                        Identity id = entity.Create(valueDictionary, LobSysteminstance);
                    }
                }
            }
        }
 }

示例:在客户端上的 BCS 客户端缓存中枚举 Customer 实例的 BDC 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.Office.BusinessData.MetadataModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RemoteSharedFileBackedMetadataCatalog RemoteCatalog = new RemoteSharedFileBackedMetadataCatalog();
            IEntity remoteEntity = RemoteCatalog.GetEntity("http://ssdk-server/sdksamples", "Customer");
            ILobSystemInstance LobSysteminstance = remoteEntity.GetLobSystem().GetLobSystemInstances()[0].Value;
 
            IMethodInstance method = remoteEntity.GetMethodInstance("Read List", MethodInstanceType.Finder);
            IEntityInstanceEnumerator ieie = remoteEntity.FindFiltered(method.GetFilters(), LobSysteminstance);
            IView view = remoteEntity.GetFinderView(method.Name);
            while (ieie.MoveNext())
            {
                foreach (IField field in view.Fields)
                {
                    if (ieie.Current[field] != null)
                    {
                        Console.WriteLine(ieie.Current[field].ToString());
                    }
                }
            }
       }
   }
}