Share via


BDC-Programmiermodell

Letzte Änderung: Donnerstag, 15. April 2010

Gilt für: SharePoint Server 2010

In Beispiel-BDC-Modell haben wir den externen Inhaltstyp Customer beschrieben. Angenommen, die Struktur des externen Inhaltstyps Customer wird als Customer (Name, ID, Phone no., Address) angezeigt, wobei Address ein weiterer komplexer Typ ist.

Nachdem Sie das Modell für den externen Inhaltstyp Customer für eine bestimmte externe Datenquelle definiert und im BDC-Metadatenspeicher gespeichert haben, wird der für die Interaktion mit dem externen Inhaltstyp Customer verwendete BDC-Code (Create, Read, Update, Delete) identisch angezeigt, unabhängig davon, ob es sich beim Back-End um eine Datenbank, einen Webdienst oder eine .NET-Verbindungsassembly handelt. Dies ist der wichtigste Vorteil dieses Programmiermusters: unabhängig vom Typ der externen Datenquelle, mit der die Anwendung zu interagieren versucht, sieht der Code genau identisch aus.

Beispiel: BDC-Code zum Erstellen der Instanz "Customer" in der externen Datenquelle

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

Beispiel: BDC-Code zum Aufzählen der Instanzen von "Customer" im BCS-Clientcache auf dem 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());
                    }
                }
            }
       }
   }
}