Like many software developers, I built one too many data access layers. Don’t get me wrong – I’m really happy with ADO.Net and it’s capabilities. I just don’t want to write the same code snippets over and over. I’ve also gone the code-generation route using multiple tools like CodeSmith and even the Guidance Automation Toolkit. I’m a big fan of NHibernate and I’m looking forward to using it again in the future.
On my current project, we’re using IBatis.Net’s DataMapper.
For this project though, there was a slight twist. For security reasons, the connection string is encrypted - but not using the facilities to encrypt sections of the config file. For whatever the reason, it is what it is.
Now, normally you would define your data sources in the SqlMap.config file. It would look something like this:
This, of course, is not an encrypted connection string. It turns out that using an encrypted connection string with IBatis.Net really isn’t that difficult. We’ll start with out SqlMap.Config file.
Our SqlMap.config file
The only purpose of our file is to indicate that type of provider that we’ll be using. Notice that connection string contains information directing people to look elsewhere for it.
We have a custom DataSource that is responsible for retrieving the encrypted connection string, decrypting it and making it available.
We initialize our mapper as you would expect but we reset the DataSource with an instance of our EncryptedDataSource class. The constructor of the EncryptedDataSource accepts the data source that was built as part of the initialization. This way we can return it whenever the framework asks for information about the configured provider.