[ibatisnet]多数据库配置

ibatisnet, 是ibatis的.net实现. 轻量ORM. 隐藏了其他的底层细节,只暴露了语句生成的部分,既让人放心又适当提高开发效率.

最近用它做个小项目, 需要多数据库查询.简述一下实现细节.

目前ibatisnet的几篇文章都是调用 IBatisNet.DataMapper.Mapper.Instance(),返回ISqlmapper.默认只能使用SqlMapper.config 文件获取上下文.

入门配置请参考这篇文章 http://www.cnblogs.com/yjmyzz/p/3362343.html

而多数据库的切换引用不同的xxMapper配置文件. 查了一下文档.需要使用DomSqlMapBuilder.ConfigAndWatch方法.

ISqlMapper mapper = builder.ConfigureAndWatch("anotherSqlMapConfig.config", handler);

handler可以传null.

另外可将每个数据库的ISqlMapper 置于单例的集合中,单个站点中 每个数据库只需一次初始化ISqlMapper

 

首先贴一下DBConfigurationHandler ,根据配置文件返回指定mapper. 

   public class DBConfigurationHandler : IConfigurationSectionHandler
    {
        private static Dictionary<string, ISqlMapper> _mappers;

        public static ISqlMapper GetMapper(string mapperName)
        {
            if (_mappers == null)
            {
                _mappers = new Dictionary<string, ISqlMapper>();
            }
            //懒加载
            if (!_mappers.ContainsKey(mapperName))
            {
                if (!Container.dbConnectionConfigs.ContainsKey(mapperName))
                {
                    throw new Exception("required mapper is not configured");
                }
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                _mappers.Add(mapperName, builder.ConfigureAndWatch(Container.dbConnectionConfigs[mapperName].dbMapperPath,null));

            }
            return _mappers[mapperName];
        }

        private static DBConfigurationContainer _container;
        public static DBConfigurationContainer Container
        {
            get
            {
                if (_container == null)
                {
                    _container = System.Configuration.ConfigurationManager.GetSection("dbConnections") as DBConfigurationContainer;
                }
                if (_container == null || _container.dbConnectionConfigs.Count == 0)
                {
                    throw new Exception("dbConnectionConfig 配置为空");
                }
                return _container;
            }
        }
        object IConfigurationSectionHandler.Create(object parent, object context, XmlNode section)
        {
            DBConfigurationContainer configs = null;

            if (Object.Equals(section, null))
            {
                throw (new ArgumentNullException("外部服务的配置信息不正确"));
            }

            Dictionary<string, DbConnectionConfig> dbConnectionConfigs = new Dictionary<string, DbConnectionConfig>();
            XmlNodeList xmlNodes = section.SelectNodes("dbConnection");
            if (!object.Equals(xmlNodes, null))
            {
                foreach (XmlNode node in xmlNodes)
                {
                    dbConnectionConfigs.Add(node.Attributes["name"].Value
                        ,new DbConnectionConfig(){
                            dbName = node.Attributes["name"].Value,
                            dbMapperPath = node.Attributes["mapper"].Value});
                }
                
            }
            configs = new DBConfigurationContainer(dbConnectionConfigs);
            return configs;
        }
    }

 

DAO基类中的调用.

    public class BaseDAL
    {
        private ISqlMapper _mapper;

        public BaseDAL(string mapperName)
        {
            _mapper = DBConfigurationHandler.GetMapper(mapperName);
        }
  }

对某一数据库,定义子类并指定相应的配置项名称.

    public class AddressDAL : BaseDAL
    {
        private static AddressDAL _addressDAL;
        private static object _lock = new object();
        public static AddressDAL Instance(string suffix)
        {
            if (_addressDAL == null)
            {
                lock (_lock)
                {
                    if (_addressDAL == null)
                    {
                        _addressDAL = new AddressDAL(suffix);
                    }
                }
            }
            return _addressDAL;
        }
        public AddressDAL(string suffix) : base("address")
        {
        }
    }

 

总结, 通过上述代码,实现了可配置的数据库切换, 上下文生成依赖于配置文件,修改某一数据库连接或水平分库无需改动代码. 但是新增数据库则需要新增DAL类型.

 

posted @ 2015-08-23 08:27  sure5597  阅读(208)  评论(0)    收藏  举报