Include Container Values In A Custom Type

This is a little bit more complicated and uses LINQ’s SelectMany function, which in this case takes an array of something that has arrays and enumerates them all. The JSON above would enumerate all 4 of the ‘Person’s contained in the array of Accounts.

Assume that in this case, you want to include the account name with the person information. The classes created when you paste the JSON message, do not have the entity that you want:

 

public class Account
{
    public string AccountName { getset; }
    public Person MainContact { getset; }
}
public class Person
{
    public string FirstName { getset; }
    public string LastName { getset; }
}

To include the account name with the person information, you have to create a new class:

public class AccountPerson
{
    public string FirstName { getset; }
    public string LastName { getset; }
    public string AccountName { getset; }
}

In the Connector.cs, create a function that creates one of these out of the container/message. In this case, you are extracting and transforming the data to better fit the information that you want.

protected override IDictionary<stringMessageDescription> RegisterEntities()
{
    return
        this.Start.Register<AccountAccountPerson>(
            a =>
            new AccountPerson
                {
                    FirstName = a.MainContact.FirstName,
                    LastName = a.MainContact.LastName,
                    AccountName = a.AccountName
                });
}

Syntactically, you have many options here, but the key point is that you provide a function that takes a message, the first type parameter, and returns what you want, the second type parameter. If the function returns many of the second type that works as well.

Sometimes you may want to return a combination of single items and multiple items. Consider a combination of the different versions you have worked with:

public class Account
{
    public string AccountName { getset; }
    public Person MainContact { getset; }
    public Person[] Contacts { getset; }
}

Here you can take advantage of C# functionality to write a function that takes the message and returns what you want. In this case many persons generated by enumerating the MainContact and then all of the Contacts.

public static IEnumerable<Person> GetAll(Account account)
{
    yield return account.MainContact;
    foreach (var person in account.Contacts)
    {
        yield return person;
    }
}
protected override IDictionary<stringMessageDescription> RegisterEntities()
{
    return this.Start.RegisterArray<AccountPerson>(a => GetAll(a));
}

C# yield return syntax is much easier to use in the named function, GetAll, leveraged in a Lambda.

There are many variations and much complexity that can be added into the function that converts the message type into the entity to return to TIBCO Scribe®. The things to keep in mind are the type of the entire message and including any types it is composed of. The Messaging Framework uses this information to deserialize the message into an instance of that type. You must consider the type you want TIBCO Scribe® to use. It may be the same as the message type, contained within the message type of a custom type constructed from the message type. Then, you must create a function that uses C# to get from the message to the entity type, unless it is either of the first two scenarios where the message type is what is returned.

See

Building A Messaging Framework Connector