如何实例化sqlmap 把Sqlmap.config放到Web应用程序的根目录下与Web.config同级

首先把sqlmap.config以及其相关的文件放到与Web.config同级得web应用程序的根目下。 参考查看Ibatis 的源代码,mapper.cs实例化sqlmap即可,贴下代码:

Code

using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace IBatisNet.DataMapper
{
    
/// <summary>
    
/// A singleton class to access the default SqlMapper defined by the SqlMap.Config
    
/// </summary>
    public sealed class Mapper
    {
        
#region Fields
        
private static volatile ISqlMapper _mapper = null;
        
#endregion

        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="obj"></param>
        public static void Configure (object obj)
        {
            _mapper 
= null;
        }

        
/// <summary>
        
/// Init the 'default' SqlMapper defined by the SqlMap.Config file.
        
/// </summary>
        public static void InitMapper()
        {
            ConfigureHandler handler 
= new ConfigureHandler (Configure);
            DomSqlMapBuilder builder 
= new DomSqlMapBuilder();
            _mapper 
= builder.ConfigureAndWatch (handler);        }

        
/// <summary>
        
/// Get the instance of the SqlMapper defined by the SqlMap.Config file.
        
/// </summary>
        
/// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
        public static ISqlMapper Instance()
        {
            
if (_mapper == null)
            {
                
lock (typeof (SqlMapper))
                {
                    
if (_mapper == null// double-check
                    {    
                        InitMapper();
                    }
                }
            }
            
return _mapper;
        }
        
        
/// <summary>
        
/// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)
        
/// </summary>
        
/// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
        public static ISqlMapper Get()
        {
            
return Instance();
        }
    }
}

 

解释说明:

关键代码:

 

Code
        /// <summary>
        
/// Init the 'default' SqlMapper defined by the SqlMap.Config file.
        
/// </summary>
        public static void InitMapper()
        {
            ConfigureHandler handler 
= new ConfigureHandler (Configure);
            DomSqlMapBuilder builder 
= new DomSqlMapBuilder();
            _mapper 
= builder.ConfigureAndWatch (handler);        
        }

然后我再看下ConfigureAndWatch这个方法:

Code

public ISqlMapper ConfigureAndWatch(ConfigureHandler configureDelegate)
        {
            
return ConfigureAndWatch( DEFAULT_FILE_CONFIG_NAME, configureDelegate ) ;
Code
public ISqlMapper ConfigureAndWatch( string resource, ConfigureHandler configureDelegate )
        {
            XmlDocument document 
= null;
            
if (resource.StartsWith("file://"))
            {
                document 
= Resources.GetUrlAsXmlDocument( resource.Remove(07) );    
            }
            
else
            {
                document 
= Resources.GetResourceAsXmlDocument( resource );    
            }

            ConfigWatcherHandler.ClearFilesMonitored();
            ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( resource ) );

            TimerCallback callBakDelegate 
= new TimerCallback( OnConfigFileChange );

            StateConfig state 
= new StateConfig();
            state.FileName 
= resource;
            state.ConfigureHandler 
= configureDelegate;
            
            ISqlMapper sqlMapper 
= Build( document, true );
            
            
new ConfigWatcherHandler( callBakDelegate, state );

            
return sqlMapper;
        }

 

其中:    public const string DEFAULT_FILE_CONFIG_NAME = "SqlMap.config";

最后通过

Code
if (resource.StartsWith("file://"))
            {
                document 
= Resources.GetUrlAsXmlDocument( resource.Remove(07) );    
            }
            
else
            {
                document 
= Resources.GetResourceAsXmlDocument( resource );    
            }

            ConfigWatcherHandler.ClearFilesMonitored();
            ConfigWatcherHandler.AddFileToWatch( Resources.GetFileInfo( resource ) );

 

   private static string _applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

 

Code

        
/// <summary>
        
/// Get XmlDocument from relative (from root directory of the application) path resource
        
/// </summary>
        
/// <param name="resource"></param>
        
/// <returns></returns>
        public static XmlDocument GetResourceAsXmlDocument(string resource)
        {
            XmlDocument config 
= new XmlDocument();

            
try 
            {
                config.Load( Path.Combine(_applicationBase, resource) );
            }
            
catch(Exception e)
            {
                
throw new ConfigurationException(
                    
string.Format("Unable to load file via resource \"{0}\" as resource. Cause : {1}"
                    resource, 
                    e.Message ) ,e); 
            }

            
return config;
        }

 

就能取到WEB应用程序根目录下的sqlmap.config。

从而实例化SqlMapper.

 

posted @ 2009-09-09 11:25  chunchill  阅读(1053)  评论(0编辑  收藏  举报