Last couple of days I was ranting about my upcoming BDC Toolkit project. I hadn’t fully tested the writing theory yet. There was quite a lack of documentation and I was worried for a while that I had to come back to my idea—but luckily I don’t have to.

So you can actually write to your BDC sources using the object model and the GenericInvoker method instance. It isn’t that hard, but it might take a bit of getting used to.

For this sample we will need a table to write to (imagine this rather poor table as your enormous enterprise data model). For simplicity’s sake I am writing directly to a table. If you have read my previous post you will know that you should use a stored procedure as an indirection layer for your data model.

The following script has been used for creating the table.

USE [MYDATABASE];
GO

SET ANSI_NULLS ON;
GO

SET QUOTED_IDENTIFIER ON;
GO

CREATE TABLE [dbo].[Test](
    [MyValue] [int] NULL
) ON [PRIMARY];

First off, you need your regular XML application definition file. I clipped it for readability purposes.

<Method Name="AddRecordMethod">
    <Properties>
        <Property Name="RdbCommandText" Type="System.String">
            INSERT INTO Test (MyValue) VALUES (@MyValue)
        </Property>
        <Property Name="RdbCommandType" Type="System.Data.CommandType">
            Text
        </Property>
    </Properties>
    <Parameters>
        <Parameter Direction="In" Name="@MyValue">
            <TypeDescriptor 
                TypeName="System.Int32"
                IdentifierName="MyValue"
                Name="MyValue" />
        </Parameter>
        <Parameter Direction="Return" Name="MyReturnValue">
            <TypeDescriptor 
                TypeName="System.Int32"
                Name="MyReturnValue" />
        </Parameter>
    </Parameters>
    <MethodInstances>
        <MethodInstance 
            Name="AddRecord"
            Type="GenericInvoker"
            ReturnParameterName="MyReturnValue" />
    </MethodInstances>
</Method>

As you can see in the XML definition above, I’m using a GenericInvoker as my method instance. The return value serves no purpose here but is required by the MethodInstance tag.

The code below is used for executing this method on the Business Data Catalog.

SqlSessionProvider.Instance()
    .SetSharedResourceProviderToUse(yourSSPName);

NamedLobSystemInstanceDictionary sysInstances = 
    ApplicationRegistry.GetLobSystemInstances();

LobSystemInstance writeInstance = 
    sysInstances["yourInstanceName"];

NamedEntityDictionary entities = 
    writeInstance.GetEntities();

Entity testEntity = 
    entities["yourEntityName"];

NamedMethodInstanceDictionary instances = 
    testEntity.GetMethodInstances();

// Gets the method definition for our AddRecord invoker
Method mymethod = 
    instances["AddRecord"].GetMethod();

// Creates a set of default parameters (which we override)
object[] param = 
    mymethod.CreateDefaultParameterInstances(instances["AddRecord"]);

param[0] = 31; // This is the MyValue parameter we defined earlier

// Finally execute the method
testEntity.Execute(instances["AddRecord"], writeInstance, ref param);

So now you can use the BDC as your unified access layer for reading and writing to your external system.