简体   繁体   English

使用Moles覆盖System.Configuration,而不是单元测试

[英]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. 为了使服务与正常进程兼容,我认为在app.config或web.config中覆盖System.Configuration ConnectionStrings是一件好事。

I've thought about using Moles to do this, but the only examples that I'm finding are for UnitTests. 我曾考虑使用Moles来做这件事,但我找到的唯一例子是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. 我真正想要的是对System.Configuration.ConfigurationManager类的Mole覆盖,以便我可以专门覆盖ConnectionStringsGet方法以查找自定义样式的连接字符串。 If it isn't found, then pass the call on to the original ConfigurationManger code in order to handle normal connection strings. 如果未找到,则将调用传递给原始ConfigurationManger代码,以便处理正常的连接字符串。

I have not yet written any code with Moles because I'm scratching my head about how to start this idea. 我还没有用Moles编写任何代码,因为我对如何开始这个想法感到头疼。 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. 看起来Moles并不真正支持在这种情况下尝试覆盖System.Configuration。

I've found a couple of posts that show promising methods for customizing the system.configuration process. 我发现了一些帖子,显示了用于自定义system.configuration过程的有前途的方法。

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 首先是Andy Pook的博客文章http://andypook.blogspot.com/2007/07/overriding-configurationmanager.html ,其次是CodeProject文章,扩展了Andy http://www.codeproject.com/提供的解决方案。文章/ 69364 /覆盖-配置-管理

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. 然后,您可以执行自定义逻辑,然后回退到标准静态方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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