| West Wind Application Settings for .NET |
| Storing Configuration Settings in a File, String or SQL Serv |
The behavior with different stores is slightly different since these stores all use Serialization of the object. Unlike the default Config file behavior, reading from an alternate store creates a new object rather than setting the properties on the current object. All these stores require factory methods to create an instance of the configuration class. For example, to Create a Configruation object from a file:
App.Configuration = (WebStoreConfig) WebStoreConfig.ReadKeysFromFile("d:\\temp\\WebConfig.xml", typeof(WebStoreConfig)) ;
If you're using encryption, note that because a new instance is created here you should make sure that your Constructor sets up the Encryption:
public WebStoreConfig() { this.SetEnryption("ConnectionString,MailPassword,MerchantPassword","WebStorePassword"); }
This ensures that encryption and decryption occurs when new instances are created from other stores.
The following example demonstrate how to use each of the stores available. Remember data is stored as a single XML value in all modes except the Configuration file option.
// *** Write WebStoreConfig Config = new WebStoreConfig(); WebStoreConfig.WriteKeysToFile("d:\\temp\\WebConfig.xml"); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadKeysFromFile("d:\\temp\\WebConfig.xml", typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;
// *** Write WebStoreConfig Config = new WebStoreConfig(); string XML = ""; Config.WriteKeysToString(out XML); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadKeysFromString(XML,typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;
Note that the WriteKeysToString() method writes the XML tring into an out parameter for consistency with the other Read methods that return a bool result value.
// *** Write WebStoreConfig Config = new WebStoreConfig(); Config.WriteKeysToSqlServer( "server=(local);Database=WebStore;uid=WebStore;pwd=xxxxxxxx", "wwConfigurationSettings",0); ... // *** Read WebStoreConfig Config = (WebStoreConfig) WebStoreConfig.ReadkeysFromSqlServer( "server=(local);Database=WebStore;uid=WebStore;pwd=wwind", "wwConfigurationSettings",0,typeof(WebStoreConfig)); int ConnectMode = Config.ConnectMode;
Both Read and Write operations require a connection string, table name and a key id in the table. However, the Table or record doesn't have to exist - the class will create a new record and even a table (assuming the application has permissions to do so).
public WebStoreConfig() { this.SetEncryption("ConnectionString,MailPassword,MerchantPassword","WebStorePassword"); }
This makes sure that the encryption settings are always applied when calling the various static Read methods that return an object instance. Decryption is applied before the object is returned to you if you have configured any fields to encrypt.