Wednesday, November 10, 2010

IdeaBlade DevForce Classic – Load connection string from configuration file

The IDataSourceKeyResolver interface allows your application to take control of the “key resolution” process, allowing you to programmatically determine the DataSourceKey used to connect to the run time database. Using the IDataSourceKeyResolver you can also pick up changes in your connectionstring from the application configuration file and resolve the new database instance. The sample code given below picks up a connectionstring from the application configuration file and updates the one used in the ideablade.idconfig file.
class CIMSDataSourceKeyResolver : IDataSourceKeyResolver
    public IdeaBlade.Util.IDataSourceKey GetKey(string name, string extension)
        RdbKey mainKey = Singleton<DefaultDataSourceKeyResolver>.Current.GetKey(name, extension) as RdbKey;
        string connectionString = ConfigurationManager.AppSettings.Get("ConnectionString");

        RdbKey newKey = MergeKeyAndConnectionString(mainKey, connectionString);
        return newKey;

    private static RdbKey MergeKeyAndConnectionString(RdbKey baseKey, String connectionString)
        if (baseKey == null) return null
        if (connectionString == null) return baseKey; 
        RdbKey result = MergeKey(baseKey, connectionString); 
        return result;

    private static RdbKey MergeKey(RdbKey baseKey, String connectionString)
        return MergeKey(baseKey, null, connectionString);

    private static RdbKey MergeKey(RdbKey baseKey, String dataProviderName, String connectionString)
        return new RdbKey(baseKey.Name, dataProviderName, connectionString, baseKey.ProbeAssemblyNames, baseKey.Tag);
You need to add the AppHelper project to the probeAssembly list in the .idconfig file to get this working properly.

1 comment:

Anonymous said...

Thanks for the how-to and detailed explanation.