using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.DAL.Factory
|
{
|
/// <summary>
|
/// 数据访问基类
|
/// </summary>
|
public class DataAccessBase
|
{
|
/// <summary>
|
/// 缓存
|
/// </summary>
|
private static Dictionary<string, object> _dict = new Dictionary<string, object>();
|
private static readonly object _locker = new object();
|
|
|
/// <summary>
|
/// 创建DAL
|
/// </summary>
|
public static I CreateDAL<I>(string dllName, string nameSpace, bool isSaveCache = true)
|
{
|
if (true)
|
{
|
|
}
|
object obj = null;
|
var type = typeof(I);
|
var cacheKey = string.Format("{0}.{1}", nameSpace, type.Name.Remove(0, 1));
|
if (_dict.ContainsKey(cacheKey))
|
obj = _dict[cacheKey];
|
|
if (obj == null)
|
{
|
lock (_locker)
|
{
|
if (obj == null)
|
{
|
try
|
{
|
//注意要主入口程序和DAL的程序都是any cpu或都是x86
|
obj = Assembly.Load(dllName).CreateInstance(cacheKey);
|
if (obj == null)
|
throw new Exception("DAL对象创建失败");
|
if (isSaveCache && obj != null)
|
_dict[cacheKey] = obj;
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
}
|
|
}
|
return (I)obj;
|
}
|
|
|
}
|
}
|