BDC: Writing programmatically to the BDC using GenericInvoker

Last couple of days I was ranting about my upcoming BDC Toolkit project, I didn’t fully test 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 ain’t that hard, but it might be a bit getting used to.

For this sample we will need a table to write to (imagine that this rather poor table as your enormous enterprise datamodel). For simplicity 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 datamodel.

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 an generic invoker 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 paramers (which we are going to overrule)
object[] param = mymethod.
     CreateDefaultParameterInstances(instances["AddRecord"]);

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

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

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

Advertisements

One Response

  1. good write up!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: