简体   繁体   中英

Using Moles to override to System.Configuration, not as a Unit Test

I'm thinking about the building of a centralized service to provide connection strings to properly authenticated applications.

In order keep the service compatible with normal processes, I was thinking that it would be a great thing to override the System.Configuration ConnectionStrings in the app.config or web.config.

I've thought about using Moles to do this, but the only examples that I'm finding are for UnitTests.

What I'd really like to have is a Mole override to the System.Configuration.ConfigurationManager class, so that I could specifically override the ConnectionStringsGet method to look for a custom style of connection string. If it isn't found, then pass the call on to the original ConfigurationManger code in order to handle normal connection strings.

I have not yet written any code with Moles because I'm scratching my head about how to start this idea. If anyone has a pointer on how to get started, I'll take it and go dig some more.

It appears that Moles doesn't really support trying to override the System.Configuration in this scenario.

I've found a couple of posts that show promising methods for customizing the system.configuration process.

First is a blog post by Andy Pook http://andypook.blogspot.com/2007/07/overriding-configurationmanager.html and second is a CodeProject article that extends the solution given by Andy http://www.codeproject.com/Articles/69364/Override-Configuration-Manager

These articles have been useful to my thinking process.

You'd probably be better off creating an interface to get the data from the config file, and a class which will just wrap the static members. You can then do your custom logic, and fall back to the standard static methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM