views:

161

answers:

2

Is it possible to configure the Data Application Block in Enterprise Library entirely in code? Instead of having the big messy config file.

A: 

short answer: yes.

Long answer: Why would you want to do that? The whole idea of the "big messy" file is that you write somewhat generic code that uses interfaces and then configure the specifics. Want to switch from ORacle to SQL Server? I've done it just by updating a config variable. Yes it's a bear porting stored procs, but it works.

No Refunds No Returns
I want to create a small function that returnes a configured database object. I could easily create another method that configures the database object for Oracle as well...The only setting in the config file would be the connectionstring.The idea with the data application block is to have generic code for different db-providers and that is good!How do I configure it in code?
Christian80
+1  A: 

Ok, after a bit of googeling and some trial and error I've come up with this solution that works pretty good. It uses the System.Data.SQLClient provider. Just supply a connectionstring:

            Dim databaseSettings As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings
            Dim connStringSection As System.Configuration.ConnectionStringsSection = New System.Configuration.ConnectionStringsSection()
            Dim dictDataSource As Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource
            Dim dbProvider As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping
            Dim dbFactory As DatabaseProviderFactory
            Dim database As Microsoft.Practices.EnterpriseLibrary.Data.Database

            databaseSettings = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings()
            connStringSection = New System.Configuration.ConnectionStringsSection()
            dictDataSource = New Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource()
            dbProvider = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping.DefaultSqlProviderName, GetType(Sql.SqlDatabase))

            connStringSection.ConnectionStrings.Add(New System.Configuration.ConnectionStringSettings("DBConnectionString", connectionString, "System.Data.SqlClient"))
            databaseSettings.ProviderMappings.Add(dbProvider)
            databaseSettings.DefaultDatabase = "DBConnectionString"

            'Add Database Settings to Dictionary
            dictDataSource.Add(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings.SectionName, databaseSettings)
            'Add Connection String to Dictionary
            dictDataSource.Add("connectionStrings", connStringSection)

            dbFactory = New DatabaseProviderFactory(dictDataSource)
            database = dbFactory.Create("DBConnectionString")
            database.CreateConnection()

            Return database
Christian80