关于IBatisNet的配置文件中数据库连接字符串加密处理

我们通常在IBatisNet配置文件 properties.config 加入数据库连接字符串。数据库连接字符串直接放在里面,没有被加密,很不安全。如果我们把 properties.config 文件作为资源嵌入到程序集,似乎可用解决安全问题,但是又出现新的问题,那就是部署。因为用户部署时,是需要重新设置数据库地址,名称,用户名,密码等值 的。

        解决办法:
        数据库连接字符串还是放在原来的程序的配置文件中,比如 WebForms 的web.config中, WinForms的 App.config中,这样我们可以以使用企业库管理工具来加密这个配置文件。

       然后,通过编程的方式加入数据库连接字符串。       

DomSqlMapBuilder builder = new DomSqlMapBuilder();
NameValueCollection prop 
= new NameValueCollection();
//DatabaseHelper.GetConnectionString()是用来从原来的配置文件中获得数据库连接字符串
prop.Add("connectionString", DatabaseHelper.GetConnectionString());
builder.Properties 
= prop;
ISqlMapper sqlMapper 
= builder.Configure();

        由于我们自己通过编程的方式提供了 ISqlMapper ,所以我们需要考虑性能的问题.SqlMap是线程安全的,所以我们可以考虑使用单件模式来提供 ISqlMapper .

        整个提供 ISqlMapper 的类如下:

//-----------------------------------------------------------------------------------------
// 模块编号:
// 文件名: SqlMapperHelper.cs
// 描述: SqlMapperHelper 类
// 作者:ChenJie 
// 编写日期:2007-5-26
// Copyright 2007
//-----------------------------------------------------------------------------------------
using System;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.Collections.Specialized;

namespace Novelty.CustomSystem.IBatisNet
{
    
/// <summary>
    
/// 提供 ISqlMapper 对象,属于单件模式(Singleton Pattern)
    
/// </summary>

    public class SqlMapperHelper
    
{
        
#region 私有变量
        
/// <summary>
        
/// ISqlMapper 实例
        
/// </summary>

        private ISqlMapper _sqlMapper;
         
#endregion


         
#region 构造函数
        
/// <summary>
        
/// 构造函数
        
/// </summary>

        SqlMapperHelper()
        
{
            CreateSqlMapper();
        }

        
#endregion


        
#region 嵌套类
        
class Nested
        
{
            
static Nested()
            
{
            }

            
internal static readonly SqlMapperHelper instance = new SqlMapperHelper();
        }

        
#endregion


        
#region 属性
        
/// <summary>
        
/// 唯一实例
        
/// </summary>

        public static SqlMapperHelper Instance
        
{
            
get
            
{
                
return Nested.instance;
            }

        }

        
#endregion


        
#region 公有方法
        
/// <summary>
        
/// 刷新 ISqlMapper 对象
        
/// 原因:当数据库连接出现变化,需要刷新该对象
        
/// </summary>

        public void RefreshSqlMapper()
        
{
            CreateSqlMapper();
        }

        
#endregion


        
#region 私有方法
        
/// <summary>
        
/// 创建 ISqlMapper 对象
        
/// </summary>

        private void CreateSqlMapper()
        
{
            
//-----(1)
            
//DomSqlMapBuilder builder = new DomSqlMapBuilder();
            
//ISqlMapper _sqlMapper = builder.Configure("SqlMap.config");     
            
//-----(2)
            
//SqlMap是线程安全的
            
//Mapper.Get()方法和Mapper.Instance()方法调用默认的 SqlMap.config 配置文件来创建SqlMapper
            
//ISqlMapper sm = Mapper.Get();
            
//以下注释内容摘自一个网友的blog:www.cnblogs.com/shanyou/articles/388602.html 。
            
//与使用DomSqlMapBuilder类的区别是,Mapper.Get()不需要指定配置文件的名称,并且使用Mapper.Get()返回SqlMapper后如果映射的XML没有错误的话,会将该XML文件缓存到内存,
            
//下次调用的时候就不需要在检查XML文件,直到SqlMap.config被改变,这样将大大的提高了程序的性能,而使用DomSqlMapBuilder建立的SqlMapper每次都要先分析映射的XML文件,性能将大大的降低
            

            DomSqlMapBuilder builder 
= new DomSqlMapBuilder();
            NameValueCollection prop 
= new NameValueCollection();
            prop.Add(
"connectionString", DatabaseHelper.GetConnectionString());
            builder.Properties 
= prop;
            _sqlMapper 
= builder.Configure();
        }

        
#endregion


        
#region 对外属性
        
/// <summary>
        
/// ISqlMapper 实例
        
/// </summary>

        public ISqlMapper SqlMapper
        
{
            
get
            
{
                
return _sqlMapper;
            }

        }

        
#endregion


    }

}

原文地址:https://www.cnblogs.com/yasin/p/3195454.html