How to create a Enterprise Database object similar to factory pattern

Topics: Data Access Application Block, General discussion
Jul 31, 2014 at 9:12 PM
Currently I am creating the Database object like this
Database _dataBase = new SqlDatabase("Data Source=****;Initial Catalog=***;user id=***;password=***");

Can you please tell how to create the specific database object dynamically on demand instead of hardcoding something similar to factory pattern?

Also what is the use of genericDatabase object?
Aug 1, 2014 at 7:08 AM
If you have connection strings defined in a config file then you can set the provider factory and then use the DatabaseFactory to
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Database db = DatabaseFactory.CreateDatabase("DatabaseName");

Or you could create the database programmatically (at startup) and assign it to the factory:
var db = new SqlDatabase("Data Source=****;Initial Catalog=***;user id=***;password=***");
    () => db, 
    name =>
        if (name == "DatabaseName")
            return db;
        throw new Exception("No database found");

Database db1 = DatabaseFactory.CreateDatabase("DatabaseName");

GenericDatabase is usually returned when the specific type of database is not known (e.g. no provider mapping specified). It provides basic common functionality that all databases should support. For example, SqlDatabase provides additional functionality parameter discovery.

Randy Levy
Enterprise Library support engineer
Support How-to
Aug 1, 2014 at 4:48 PM
Thank you so much