Customising MySettings class in .NET to override a connection string.

With .NET 2.0 came the “My” classes, a useful feature of the classes was the integration into the “app.config” file and project properties for configuration of settings, including connection strings.

In order to maintain compatibility with previous app that share database configuration settings with new modules and developments, it would be nice to be able to override settings. It would be nice to use customised connection settings yet seamlessly use built in features of Visual Studio to set connection strings for DataAdapters, LINQtoSQL, etc.

Thankfully this is easily achieved in .NET 2.0 and onwards (with the exception of Entity Models, released with .NET 3.5 SP1, which I will go into in another blog).

To modify simply open the project properties, and switch to the the settings window and click on the “View Code” button.

This will add a partial class for “MySettings” to your project. From which it is easy to override any settings.

To override a particular property or group of properties is easy by overriding the “Item” property: –

Partial Friend NotInheritable Class MySettings
    Default Public Overloads Overrides Property Item(ByVal propertyName As String) As Object
propertyName = “MyDatabase”

End If
        End Get
(ByVal value As Object)
            If propertyName = “MyDatabase” Then

                MyBase.Item(propertyName) = value

            End If

        End Set
    End Property
End Class

Where “Connection” is a custom class build to use shared legacy application connection settings.

Paul B

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s