Connection Handling

There are two types of CIPl attributes you can attach to a C# class

  • CIP4ItemAttribute - which defines the characteristics of the class that are invariant across all data providers
  • CIP4ConnectionAttribute subtypes - which are specific to the data provider and provide settings required by the store

You can get a list of the currently available CIP4ConnectionAttribute types by running

CIP4.Installer /action list

The application will return a list somewhat as follows - which of these you can use will depend on what you have installed in the local environment.

Available connection types and their parameters:
================================================
Sql:
        Database:String
        Host:String
        UserId:String
        Password:String
        CacheConnection:CIP4ConnectionAttribute
MySql:
        Database:String
        Host:String
        UserId:String
        Password:String
        CacheConnection:CIP4ConnectionAttribute
Mosso:
        UserName:String
        AccessKey:String
        CacheConnection:CIP4ConnectionAttribute
S3:
        AccessKey:String
        SecretKey:String
        CacheConnection:CIP4ConnectionAttribute
XmlFile:
        XmlFileName:String
        CacheConnection:CIP4ConnectionAttribute
File:
        BaseDirectoryPath:String
        CacheConnection:CIP4ConnectionAttribute
Dictionary:
        BaseDirectoryPath:String
        CacheConnection:CIP4ConnectionAttribute
Cassandra:
        Keyspace:String
        ColumnFamily:String
        CacheConnection:CIP4ConnectionAttribute
MongoDB:
        Server:String
        Port:Int32
        Database:String
        CacheConnection:CIP4ConnectionAttribute
Migrator:
        Target:CIP4ConnectionAttribute
        Source:CIP4ConnectionAttribute
        CacheConnection:CIP4ConnectionAttribute    

Providing Connection Values

There are three ways to provide connection values

  1. As a part of the class definition
  2. When constructing the class wrapper
  3. Via a delegate attached to the wrapper

1. Class Definition

The connection attribute can be defined in the class declaration using the usual C# conventions - for example

    [CIP4Item(AddAllProperties=true, Collection="People"),
     CIP4DictionaryConnection(BaseDirectoryPath = "c:\\temp\\PeopleData")]
    public class PersonType
    {
        public static StrongWrapper<PersonType> Wrapper = new StrongWrapper<PersonType>(Globals.DefaultLogger);

        [CIP4ExternalID]
        public string Name { get; set; }
        public string Address { get; set; }
    }
    

The approach has limited value as it requires the various connection parameters to be defined as constants in the application source (C# attributes do not allow general expressions in attribute parameter assignments)

2. Wrapper

The connection attribute can be provided as a parameter to the wrapper - for example

            StrongWrapper<PersonType> wrapper = new StrongWrapper<PersonType>(
                new CIP4DictionaryConnectionAttribute { 
			BaseDirectoryPath = Path.Combine(Environment.CurrentDirectory, "PeopleData") },
                Globals.DefaultLogger);
  

 

3. Delegate

The connection attribute values can be provided by the GetValues delegate defined on the wrapper - for example

            PersonType.Wrapper.GetValues = delegate(Dictionary<string, string> values)
            {
                values.Add("XmlFileName", "c:\\temp\\PeopleData\\People.xml");
                return ProviderTypesEnum.XmlFile;
            };
    

This is the most awkward to use because you have to know what value to provide in the values dictionary for a given ProviderTypesEnum value - these values (including the appropriate enum value) can be obtained by running

    CIP4.Installer /action list  

The CIP4ConnectionAttribute class defines a function called ProviderTypesEnumFromString that will translate a string into a ProviderTypesEnum value.

The CIP4ConnectionAttribute also provides a function called GetConnectionFromString which will take a sql connection style string (i.e. name-value pairs separated by ";" with "=" between name and value) and return an appropriate CIP4ConnectionAttribute subtype.

Last edited Oct 11, 2010 at 10:58 PM by PatrickThompson, version 2

Comments

No comments yet.