Condividi tramite


BDC Programming Model

Applies to: SharePoint Server 2010

In Sample BDC Model, we described a Customer external content type. Let's assume the structure of the Customer external content type appears as Customer (Name, ID, Phone no., Address), where address is another complex type.

After you define the model for the Customer external content type for a given external data source and save it in the BDC Metadata Store, the BDC code used to interact with the Customer external content type (create, read, update, delete) appears the same, regardless of whether the back-end is a database, a Web service, or a .NET connectivity assembly. This is the primary advantage of this programming pattern: Regardless of the type of the external data source that the application is trying to interact with, the code will look exactly the same.

Example: BDC code that creates a Customer instance in the external data source

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);
                    }
                }
            }
        }
 }

Example: BDC code that enumerates Customer instances in the BCS client cache on the client

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());
                    }
                }
            }
       }
   }
}