Browse Source

-

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
d4afab2e19
  1. 32
      Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs
  2. 8
      Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs

32
Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs

@ -10,10 +10,11 @@ namespace SqlSugar
protected List<T> GetEntityList<T>(SqlSugarClient context, IDataReader dataReader, string fields)
{
Type type = typeof(T);
string key = "DataReaderToList." + fields+ dataReader.FieldCount+ context.CurrentConnectionConfig.DbType + type.FullName;
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(key, () =>
var fieldNames = GetDataReaderNames(dataReader);
string cacheKey = GetCacheKey(type,fieldNames);
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader).CreateBuilder(type);
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type);
return cacheResult;
});
List<T> result = new List<T>();
@ -32,6 +33,29 @@ namespace SqlSugar
return result;
}
private string GetCacheKey(Type type,List<string> keys)
{
StringBuilder sb = new StringBuilder("DataReaderToList.");
sb.Append(type.FullName);
sb.Append(".");
foreach (var item in keys)
{
sb.Append(item);
}
return sb.ToString();
}
private List<string> GetDataReaderNames(IDataReader dataReader)
{
List<string> keys = new List<string>();
var count = dataReader.FieldCount;
for (int i = 0; i < count; i++)
{
keys.Add(dataReader.GetName(i));
}
return keys;
}
protected List<T> GetKeyValueList<T>(Type type, IDataReader dataReader)
{
List<T> reval = new List<T>();
@ -125,7 +149,7 @@ namespace SqlSugar
}
else if (type.IsEnum)
{
reval.Add((T)Enum.Parse(type,value.ObjToString()));
reval.Add((T)Enum.Parse(type, value.ObjToString()));
}
else
{

8
Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs

@ -74,12 +74,12 @@ namespace SqlSugar
}
public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord)
public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord,List<string> fieldNames)
{
this.Context = context;
this.DataRecord = dataRecord;
this.DynamicBuilder = new IDataReaderEntityBuilder<T>();
this.ReaderKeys = new List<string>();
this.ReaderKeys = fieldNames;
}
#endregion
@ -91,10 +91,6 @@ namespace SqlSugar
public IDataReaderEntityBuilder<T> CreateBuilder(Type type)
{
for (int i = 0; i < this.DataRecord.FieldCount; i++)
{
this.ReaderKeys.Add(this.DataRecord.GetName(i));
}
DynamicMethod method = new DynamicMethod("SqlSugarEntity", type,
new Type[] { typeof(IDataRecord) }, type, true);
ILGenerator generator = method.GetILGenerator();

Loading…
Cancel
Save