.Net通用基础框架(.Net Common Infrastructure)-数据访问

  IBatis=Internet+aBatis,由于Apache的支持力度不够,项目组在Google Code上另起炉灶,改为MyBatis。和Hibernate相比,IBatis在上手度、系统兼容性、OOA/D要求、Sql监控/优化方面略胜一筹,获得了大多数互联网公司的青睐。唯一不爽的是Java版本很活跃,.Net版本静悄悄。

  • MyBatisBase基类,支持读写库分离,IBatis在解析Dom时很耗时,实现时做了缓存处理。
  
using Common.Core;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.SessionStore;

namespace Common.Data
{
/// <summary>
/// MyBatis基类
/// </summary>
/// <typeparam name="T"> 类型 </typeparam>
public abstract class MyBatisBase < T >
{
/// <summary>
///
/// </summary>
public ISqlMapper Reader
{
get ;
set ;
}

/// <summary>
///
/// </summary>
public ISqlMapper Writer
{
get ;
set ;
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="readerName"> 读名称 </param>
/// <param name="writerName"> 写名称 </param>
public MyBatisBase( string readerName, string writerName)
{
this .Reader = ObjectFactory < ISqlMapper > .GetObject(readerName, this .CreateMapper, null );
this .Writer = ObjectFactory < ISqlMapper > .GetObject(writerName, this .CreateMapper, null );
}

/// <summary>
/// 创建映射器
/// </summary>
/// <param name="name"> 名称 </param>
/// <param name="args"> 参数集合 </param>
/// <returns> Sql映射器 </returns>
public ISqlMapper CreateMapper( string name, object [] args)
{
ISqlMapper result
= null ;
DomSqlMapBuilder builder
= new DomSqlMapBuilder();
result
= builder.Configure(name);
result.SessionStore
= new HybridWebThreadSessionStore(result.Id);

return result;
}

/// <summary>
/// 获取声明名称
/// </summary>
/// <param name="name"> 名称 </param>
/// <returns> 声明名称 </returns>
protected virtual string GetStatementName( string name)
{
return string .Format( " {0}.{1} " , typeof (T).Namespace, name);
}
}
}
  • 其中用到的对象工厂
  
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Common.Core
{
/// <summary>
/// 对象工厂
/// </summary>
/// <typeparam name="T"> 类型 </typeparam>
public sealed class ObjectFactory < T >
{
private static readonly IDictionary < string , T > objects = new Dictionary < string , T > ();
private static readonly object syncLock = new object ();

/// <summary>
/// 构造函数
/// </summary>
private ObjectFactory()
{

}

/// <summary>
/// 创建
/// </summary>
/// <param name="typeName"> 类型名称 </param>
/// <param name="args"> 参数集合 </param>
/// <returns> 结果 </returns>
private static T Create( string typeName, object [] args)
{
return (T)Activator.CreateInstance(Type.GetType(typeName), args);
}

/// <summary>
/// 获取对象
/// </summary>
/// <param name="name"> 名称 </param>
/// <returns> 对象 </returns>
public static T GetObject( string name)
{
T result
= default (T);
string [] parameters = ConfigurationManager.AppSettings[name].Split( new [] { ' | ' }, StringSplitOptions.RemoveEmptyEntries);
if (parameters.Length > 0 )
{
string typeName = parameters[ 0 ];
object [] args = parameters.Skip < object > ( 1 ).ToArray < object > ();
result
= GetObject(name, typeName, null , args);
}

return result;
}

/// <summary>
/// 获取对象
/// </summary>
/// <param name="name"> 名称 </param>
/// <param name="func"> 函数 </param>
/// <param name="args"> 参数集合 </param>
/// <returns> 对象 </returns>
public static T GetObject( string name, Func < string , object [], T > func, params object [] args)
{
return GetObject(name, name, func, args);
}

/// <summary>
/// 获取对象
/// </summary>
/// <param name="name"> 名称 </param>
/// <param name="typeName"> 类型名称 </param>
/// <param name="func"> 函数 </param>
/// <param name="args"> 参数集合 </param>
/// <returns> 对象 </returns>
public static T GetObject( string name, string typeName, Func < string , object [], T > func, params object [] args)
{
T result
= default (T);
if ( ! string .IsNullOrWhiteSpace(name))
{
if (objects.ContainsKey(name))
{
result
= objects[name];
}
else
{
lock (syncLock)
{
if ( ! objects.ContainsKey(name))
{
result
= func == null ? Create(typeName, args) : func(typeName, args);
if (result != null )
{
objects.Add(name, result);
}
}
else
{
result
= objects[name];
}
}
}
}

return result;
}
}
}

源码下载

你可能感兴趣的:(struct)