Phew! That's a long blog post title, but hopefully it clearly conveys what I'm about to show you. In my last post, I talked about where you should set your OpenAccess database connection strings. I showed you how you can define multiple database connections in your web.config (or app.config), simulating the idea of having a unique connection string for DEV, TEST, and PROD environments.

So now that you have your connection strings set in a way that is easy to maintain, you need a way to easily to tell your OpenAccess persistent classes to use a specific connection at runtime. Let's look at one method that makes the process very easy.

WHAT IS OBJECTSCOPEPROVIDER?

When you work with a project that is consuming OpenAccess persistent classes (like a web site or a WinForms application), OpenAccess can automatically add a helper class to your project that simplifies the process of managing the OpenAccess ObjectScope (think LinqToSQL DatabaseContext). This helper class is named "ObjectScopeProvider1.cs." If you examine this class, you'll see that it's default behavior is to open a connection to your database using a hard coded string, like this (where "DatabaseConnection1" is the connection id):

C# (ObjectScopeProvider1.cs)

/// <summary>  /// Returns the instance of Database for the connectionId   /// specified in the Enable Project Wizard.  /// </summary>  /// <returns>Instance of Database.</returns>  static public Database Database()
{
    if( theObjectScopeProvider1 == null )
        theObjectScopeProvider1 = new ObjectScopeProvider1();
 
    if( theObjectScopeProvider1.myDatabase == null )
        theObjectScopeProvider1.myDatabase = Telerik.OpenAccess.Database.Get( "DatabaseConnection1" );
 
    return theObjectScopeProvider1.myDatabase;
}

To change the database connection that your project uses, you simply need to change the string value in the Database.Get() method to match the ID of a connection you've got stored in your App.Config for Web.Config. Simple if you're changing it at design-time. But how do we change connections at runtime?

 

ENHANCING FOR RUNTIME CONFIGURATION

To enable good runtime configuration, we need to make a small change to the ObjectScopeProvider helper class. Instead of hard coding a string to represent our database connection ID, let's instead tell our ObjectScopeProvider to grab the connection ID from our web.config, like this:

C# (Enhanced ObjectScopeProvider1.cs)

static public Database Database()
{
    if( theObjectScopeProvider1 == null )
        theObjectScopeProvider1 = new ObjectScopeProvider1();
 
    //Get the connection ID from the web.config
    var connectionId = ConfigurationManager.AppSettings["OpenAccessConnectionName"];
 
    //Use the configuration value to specify the database  //connection if( theObjectScopeProvider1.myDatabase == null )
    theObjectScopeProvider1.myDatabase = Telerik.OpenAccess.Database.Get( connectionId );
 
    return theObjectScopeProvider1.myDatabase;
}

With this in-place, all we have to do is set an AppSetting in our web.config containing the name of the configured OpenAccess database connection we want our project to use (see my last post for details on setting up the connections), like this:

XML (Web.Config)

<configuration> <appSettings> <add key="OpenAccessConnectionName" value="TESTdb" /> </appSettings> <system.web>
    ...
  </system.web>
...

In this example, we have configured a OpenAccess connection with the ID of "TESTdb" (placed either in our App.Config or Web.Config). At runtime, our enhanced ObjectScopeProvider will try to grab the connection ID from our web.config file, and then from there use the appropriate database connection details. To change the database that our OpenAccess project uses, we simply need to change this single AppSetting.

WRAP-UP

In short, the moral of this blog post is that with a little code, you can make your OpenAccess connection configuration completely "config file based," eliminating the need to make any code changes when you must change or update your database connection strings. Hopefully this tip will save you some time and set you on the road to "real world success" with OpenAccess.


About the Author

Todd Anglin

is an avid HTML5, CSS3, and JavaScript advocate, and geek about all things web development. He is an active speaker and author, helping developers around the world learn and adopt HTML5. Todd works for Telerik as VP of HTML5 Web & Mobile Tools, where his current technical focus is on Kendo UI. Todd is @toddanglin on Twitter

Related Posts

Comments

Comments are disabled in preview mode.