Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code missing from repo but present on NuGet #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ _UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
*.ide
.vs
47 changes: 21 additions & 26 deletions ObjectHydrator/Caching/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,43 @@ namespace SqlObjectHydrator.Caching
{
internal static class CacheManager
{
internal static readonly Dictionary<int, object> MappingCaches;
internal static readonly Dictionary<int, object> MappingCaches = new Dictionary<int, object>();

static CacheManager()
public static void StoreMappingCache<T>(
Func<MappingCache<T>> mappingCache,
IDataReader dataReader,
Type configurationType )
{
MappingCaches = new Dictionary<int, object>();
}

public static void StoreMappingCache<T>( Func<MappingCache<T>> mappingCache, IDataReader dataReader, Type configurationType )
{
var cacheHashCode = GetCacheHashCode<T>( dataReader, configurationType );
MappingCaches.Add( cacheHashCode, mappingCache() );
int cacheHashCode = CacheManager.GetCacheHashCode<T>( dataReader, configurationType );
CacheManager.MappingCaches.Add( cacheHashCode, (object)mappingCache() );
}

public static int GetCacheHashCode<T>( IDataReader dataReader, Type configurationType )
{
var result = typeof( T ).GetHashCode() + GetReaderHashCode( dataReader );
if ( configurationType != null )
{
result += configurationType.GetHashCode();
}
return result;
int num = typeof( T ).GetHashCode() + CacheManager.GetReaderHashCode( dataReader );
if ( configurationType != (Type)null )
num += configurationType.GetHashCode();
return num;
}

public static bool ContainsMappingCache<T>( IDataReader dataReader, Type configurationType )
{
return MappingCaches.ContainsKey( GetCacheHashCode<T>( dataReader, configurationType ) );
return CacheManager.MappingCaches.ContainsKey( CacheManager.GetCacheHashCode<T>( dataReader, configurationType ) );
}

public static MappingCache<T> GetMappingCache<T>( IDataReader dataReader, Type configurationType )
public static MappingCache<T> GetMappingCache<T>(
IDataReader dataReader,
Type configurationType )
{
return (MappingCache<T>)MappingCaches.FirstOrDefault( x => x.Key == GetCacheHashCode<T>( dataReader, configurationType ) ).Value;
return (MappingCache<T>)CacheManager.MappingCaches.FirstOrDefault<KeyValuePair<int, object>>( (Func<KeyValuePair<int, object>, bool>)( x => x.Key == CacheManager.GetCacheHashCode<T>( dataReader, configurationType ) ) ).Value;
}

public static int GetReaderHashCode( IDataReader dataReader )
{
var hashcode = 0;
for ( var i = 0; i < dataReader.FieldCount; i++ )
{
hashcode += dataReader.GetFieldType( i ).GetHashCode();
hashcode += dataReader.GetName( i ).GetHashCode();
}
return hashcode;
int num = 0;
for ( int i = 0; i < dataReader.FieldCount; ++i )
num = num + dataReader.GetFieldType( i ).GetHashCode() + dataReader.GetName( i ).GetHashCode();
return num;
}
}
}
}
24 changes: 14 additions & 10 deletions ObjectHydrator/Caching/MappingCache.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
using SqlObjectHydrator.ILEmitting;
using System;
using System.Collections.Generic;
using System.Data;
using SqlObjectHydrator.ILEmitting;

namespace SqlObjectHydrator.Caching
{
internal class MappingCache<T>
{
public MappingCache( Func<IDataReader, Dictionary<MappingEnum, object>, Dictionary<Tuple<int,Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>, T> func, Dictionary<MappingEnum, object> mappings )
public MappingCache(
System.Func<IDataReader, Dictionary<MappingEnum, object>, Dictionary<Tuple<int, Type>, System.Func<IDataRecord, Dictionary<MappingEnum, object>, object>>, T> func,
Dictionary<MappingEnum, object> mappings )
{
Func = func;
Mappings = mappings;
TypeMaps = new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>();
this.Func = func;
this.Mappings = mappings;
this.TypeMaps = new Dictionary<Tuple<int, Type>, System.Func<IDataRecord, Dictionary<MappingEnum, object>, object>>();
}

private Func<IDataReader, Dictionary<MappingEnum, object>, Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>, T> Func { get; set; }
private System.Func<IDataReader, Dictionary<MappingEnum, object>, Dictionary<Tuple<int, Type>, System.Func<IDataRecord, Dictionary<MappingEnum, object>, object>>, T> Func { get; set; }

private Dictionary<MappingEnum, object> Mappings { get; set; }
private Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>> TypeMaps { get; set; }

public T Run( IDataReader dataReader )
private Dictionary<Tuple<int, Type>, System.Func<IDataRecord, Dictionary<MappingEnum, object>, object>> TypeMaps { get; set; }

public T Run( IDataReader dataReader )
{
return Func( dataReader, Mappings, TypeMaps );
return this.Func( dataReader, this.Mappings, this.TypeMaps );
}
}
}
}
37 changes: 16 additions & 21 deletions ObjectHydrator/ClassMapping/ClassMapGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
using System;
using SqlObjectHydrator.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using SqlObjectHydrator.Configuration;

namespace SqlObjectHydrator.ClassMapping
{
internal static class ClassMapGenerator
{
public static ClassMapResult Generate<T>( IDataReader dataReader, Type configurationType = null ) where T : new()
public static ClassMapResult Generate<T>(
IDataReader dataReader,
Type configurationType = null )
where T : new()
{
var mappings = new Mapping();
if ( configurationType != null )
Mapping mapping = new Mapping();
if ( configurationType != (Type)null )
( (IObjectHydratorConfiguration)Activator.CreateInstance( configurationType ) ).Mapping( (IMapping)mapping );
ClassMapResult classMapResult = new ClassMapResult()
{
var configuration = (IObjectHydratorConfiguration)Activator.CreateInstance( configurationType );
configuration.Mapping( mappings );
}

var classMapResult = new ClassMapResult
{
Mappings = mappings
Mappings = mapping
};

var baseType = ( typeof( T ).IsGenericType && typeof( T ).GetGenericTypeDefinition() == typeof( List<> ) ) ? typeof( T ).GetGenericArguments()[ 0 ] : typeof( T );
if ( !mappings.TableMaps.ContainsValue( baseType ) )
mappings.TableMaps.Add( 0, baseType );

mappings.TableMaps = mappings.TableMaps.OrderBy( x => x.Key ).ToDictionary( x => x.Key, x => x.Value );

Type type = !typeof( T ).IsGenericType || !( typeof( T ).GetGenericTypeDefinition() == typeof( List<> ) ) ? typeof( T ) : typeof( T ).GetGenericArguments()[ 0 ];
if ( !mapping.TableMaps.ContainsValue( type ) )
mapping.TableMaps.Add( 0, type );
mapping.TableMaps = mapping.TableMaps.OrderBy<KeyValuePair<int, Type>, int>( (Func<KeyValuePair<int, Type>, int>)( x => x.Key ) ).ToDictionary<KeyValuePair<int, Type>, int, Type>( (Func<KeyValuePair<int, Type>, int>)( x => x.Key ), (Func<KeyValuePair<int, Type>, Type>)( x => x.Value ) );
return classMapResult;
}
}
}
}
7 changes: 4 additions & 3 deletions ObjectHydrator/ClassMapping/ClassMapResult.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace SqlObjectHydrator.ClassMapping
{
internal class ClassMapResult
{
public ClassMapResult()
{
TempDataStorage = new List<List<Dictionary<int, object>>>();
this.TempDataStorage = new List<List<Dictionary<int, object>>>();
}

public List<List<Dictionary<int, object>>> TempDataStorage { get; private set; }

public Mapping Mappings { get; set; }
}
}
}
82 changes: 38 additions & 44 deletions ObjectHydrator/ClassMapping/Mapping.cs
Original file line number Diff line number Diff line change
@@ -1,105 +1,99 @@
using SqlObjectHydrator.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using SqlObjectHydrator.Configuration;

namespace SqlObjectHydrator.ClassMapping
{
internal class Mapping : IMapping
{
public Dictionary<PropertyInfo, PropertyMap> PropertyMaps { get; set; }

public Dictionary<int, Type> TableMaps { get; set; }

public List<DictionaryTableJoin> DictionaryTableJoins { get; set; }

public List<TableJoinMap> TableJoins { get; set; }

public Dictionary<KeyValuePair<Type, Type>, object> Joins { get; set; }

public Dictionary<Type, object> VariableTableTypes { get; set; }

public Mapping()
{
PropertyMaps = new Dictionary<PropertyInfo, PropertyMap>();
TableMaps = new Dictionary<int, Type>();
DictionaryTableJoins = new List<DictionaryTableJoin>();
TableJoins = new List<TableJoinMap>();
Joins = new Dictionary<KeyValuePair<Type, Type>, object>();
VariableTableTypes = new Dictionary<Type, object>();
this.PropertyMaps = new Dictionary<PropertyInfo, PropertyMap>();
this.TableMaps = new Dictionary<int, Type>();
this.DictionaryTableJoins = new List<DictionaryTableJoin>();
this.TableJoins = new List<TableJoinMap>();
this.Joins = new Dictionary<KeyValuePair<Type, Type>, object>();
this.VariableTableTypes = new Dictionary<Type, object>();
}

void IMapping.Table<T>( int id )
{
TableMaps.Add( id, typeof( T ) );
this.TableMaps.Add( id, typeof( T ) );
}

void IMapping.PropertyMap<T, TResult>( Expression<Func<T, TResult>> property, Func<IDataRecord, TResult> setAction )
void IMapping.PropertyMap<T, TResult>(
Expression<Func<T, TResult>> property,
Func<IDataRecord, TResult> setAction )
{
PropertyMaps.Add( GetPropertyInfo<T>( property.ToString().Split( '.' ).Last() ), new PropertyMap( setAction ) );
this.PropertyMaps.Add( Mapping.GetPropertyInfo<T>( ( (IEnumerable<string>)property.ToString().Split( '.' ) ).Last<string>() ), new PropertyMap( (Delegate)setAction ) );
}

public void PropertyMap<T, TResult>( Expression<Func<T, TResult>> property, string columnName )
{
PropertyMaps.Add( GetPropertyInfo<T>( property.ToString().Split( '.' ).Last() ), new PropertyMap( columnName ) );
this.PropertyMaps.Add( Mapping.GetPropertyInfo<T>( ( (IEnumerable<string>)property.ToString().Split( '.' ) ).Last<string>() ), new PropertyMap( columnName ) );
}

public void PropertyMap<T, TResult>( Expression<Func<T, TResult>> property, int columnId )
{
PropertyMaps.Add( GetPropertyInfo<T>( property.ToString().Split( '.' ).Last() ), new PropertyMap( columnId ) );
this.PropertyMaps.Add( Mapping.GetPropertyInfo<T>( ( (IEnumerable<string>)property.ToString().Split( '.' ) ).Last<string>() ), new PropertyMap( columnId ) );
}

private static PropertyInfo GetPropertyInfo<T>( string name )
{
var propertyInfo = typeof( T ).GetProperty( name ) ?? typeof( T ).GetProperty( name, BindingFlags.Instance | BindingFlags.NonPublic );
return propertyInfo;
PropertyInfo property = typeof( T ).GetProperty( name );
if ( (object)property == null )
property = typeof( T ).GetProperty( name, BindingFlags.Instance | BindingFlags.NonPublic );
return property;
}

void IMapping.TableJoin<TParent, TChild>( Func<TParent, TChild, bool> canJoin, Action<TParent, List<TChild>> listSet )
void IMapping.TableJoin<TParent, TChild>(
Func<TParent, TChild, bool> canJoin,
Action<TParent, List<TChild>> listSet )
{
TableJoins.Add( new TableJoinMap
this.TableJoins.Add( new TableJoinMap()
{
ChildType = typeof( TChild ),
ParentType = typeof( TParent ),
CanJoin = canJoin,
ListSet = listSet
CanJoin = (object)canJoin,
ListSet = (object)listSet
} );
}

void IMapping.Join<TParent, TChild>( Action<TParent, List<TChild>> listSet )
{
Joins.Add( new KeyValuePair<Type, Type>( typeof( TParent ), typeof( TChild ) ), listSet );
this.Joins.Add( new KeyValuePair<Type, Type>( typeof( TParent ), typeof( TChild ) ), (object)listSet );
}

void IMapping.AddJoin( Func<ITableJoin, ITableJoinMap> func )
{
var tableJoinMap = func( new TableJoin() );
if ( tableJoinMap is DictionaryTableJoin )
{
var dictionaryTableJoin = tableJoinMap as DictionaryTableJoin;
DictionaryTableJoins.Add( dictionaryTableJoin );
TableMaps.Add( dictionaryTableJoin.ChildTable, typeof( ExpandoObject ) );
}
ITableJoinMap tableJoinMap = func( (ITableJoin)new TableJoin() );
if ( !( tableJoinMap is DictionaryTableJoin ) )
return;
DictionaryTableJoin dictionaryTableJoin = tableJoinMap as DictionaryTableJoin;
this.DictionaryTableJoins.Add( dictionaryTableJoin );
this.TableMaps.Add( dictionaryTableJoin.ChildTable, typeof( ExpandoObject ) );
}

void IMapping.VariableTableType<T>( Func<IDataRecord, Type> action )
{
VariableTableTypes.Add( typeof( T ), action );
}
}

internal class TableJoinMap
{
public Type ParentType { get; set; }
public Type ChildType { get; set; }
public object CanJoin { get; set; }
public object ListSet { get; set; }
}

internal class TableJoin : ITableJoin
{
public IDictionaryJoinCondition<T> DictionaryTableJoin<T>() where T : new()
{
return new DictionaryTableJoin<T>();
this.VariableTableTypes.Add( typeof( T ), (object)action );
}
}
}
}
2 changes: 1 addition & 1 deletion ObjectHydrator/ClassMapping/PropertyExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static Type GetActualPropertyType( this PropertyInfo propertyInfo )
return propertyInfo.PropertyType;
}
}
}
}
18 changes: 10 additions & 8 deletions ObjectHydrator/ClassMapping/PropertyMap.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace SqlObjectHydrator.ClassMapping
{
Expand All @@ -7,25 +7,27 @@ internal class PropertyMap
public PropertyMapType PropertyMapType { get; set; }

public object SetAction { get; set; }

public int ColumnId { get; set; }

public string ColumnName { get; set; }

public PropertyMap( Delegate setAction )
{
SetAction = setAction;
PropertyMapType = PropertyMapType.Func;
this.SetAction = (object)setAction;
this.PropertyMapType = PropertyMapType.Func;
}

public PropertyMap( int columnId )
{
ColumnId = columnId;
PropertyMapType = PropertyMapType.ColumnId;
this.ColumnId = columnId;
this.PropertyMapType = PropertyMapType.ColumnId;
}

public PropertyMap( string columnName )
{
ColumnName = columnName;
PropertyMapType = PropertyMapType.ColumnName;
this.ColumnName = columnName;
this.PropertyMapType = PropertyMapType.ColumnName;
}
}
}
}
6 changes: 3 additions & 3 deletions ObjectHydrator/ClassMapping/PropertyMapType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace SqlObjectHydrator.ClassMapping
namespace SqlObjectHydrator.ClassMapping
{
internal enum PropertyMapType
{
Func,
ColumnId,
ColumnName
ColumnName,
}
}
}
Loading