-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress towards lazy loading: Unified member accessors.
- Loading branch information
1 parent
7a21ce8
commit bf12431
Showing
24 changed files
with
226 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
Achilles.Entities.Sqlite/Modelling/Mapping/ColumnAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#region Copyright Notice | ||
|
||
// Copyright (c) by Achilles Software, All rights reserved. | ||
// | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
// | ||
// Send questions regarding this copyright notice to: mailto:[email protected] | ||
|
||
#endregion | ||
|
||
#region Namespaces | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
#endregion | ||
|
||
namespace Achilles.Entities.Modelling.Mapping | ||
{ | ||
internal class ColumnAccessor<TEntity> : MemberAccessor | ||
where TEntity : class | ||
{ | ||
private readonly MemberInfo _columnInfo; | ||
|
||
private Func<TEntity, object> _getter; | ||
private Action<TEntity, object> _setter; | ||
|
||
public ColumnAccessor( MemberInfo columnInfo ) | ||
: base( columnInfo ) | ||
{ | ||
_columnInfo = columnInfo; | ||
|
||
CreateGetter(); | ||
CreateSetter(); | ||
} | ||
|
||
public override object GetValue<TMember>( TMember entity ) => _getter( entity as TEntity ); | ||
|
||
public override void SetValue<TMember>( TMember entity, object value ) => _setter( entity as TEntity, value ); | ||
|
||
private void CreateGetter() | ||
{ | ||
if ( _columnInfo is PropertyInfo propertyInfo ) | ||
{ | ||
ParameterExpression instance = Expression.Parameter( typeof( TEntity ), "instance" ); | ||
|
||
var body = Expression.Call( instance, propertyInfo.GetGetMethod() ); | ||
var parameters = new ParameterExpression[] { instance }; | ||
Expression conversion = Expression.Convert( body, typeof( object ) ); | ||
|
||
_getter = Expression.Lambda<Func<TEntity, object>>( conversion, parameters ).Compile(); | ||
} | ||
else if ( _columnInfo is FieldInfo field ) | ||
{ | ||
ParameterExpression instance = Expression.Parameter( typeof( TEntity ), "instance" ); | ||
|
||
MemberExpression fieldExpression = Expression.Field( instance, field ); | ||
var parameters = new ParameterExpression[] { instance }; | ||
Expression conversion = Expression.Convert( fieldExpression, typeof( object ) ); | ||
|
||
_getter = Expression.Lambda<Func<TEntity, object>>( conversion, parameters ).Compile(); | ||
} | ||
} | ||
|
||
private void CreateSetter() | ||
{ | ||
if ( _columnInfo is PropertyInfo propertyInfo ) | ||
{ | ||
var columnPropertySetMethod = propertyInfo.GetSetMethod(); | ||
var setMethodParameterType = columnPropertySetMethod.GetParameters().First().ParameterType; | ||
|
||
var entityInstanceParameter = Expression.Parameter( typeof( TEntity ), "instance" ); | ||
var valueParameter = Expression.Parameter( typeof( object ), "value" ); | ||
Expression conversion = Expression.Convert( valueParameter, setMethodParameterType ); | ||
|
||
var body = Expression.Call( entityInstanceParameter, columnPropertySetMethod, conversion ); | ||
var parameters = new ParameterExpression[] { entityInstanceParameter, valueParameter }; | ||
|
||
_setter = Expression.Lambda<Action<TEntity, object>>( | ||
body, parameters ).Compile(); | ||
} | ||
else if ( _columnInfo is FieldInfo field ) | ||
{ | ||
var instanceParameter = Expression.Parameter( typeof( TEntity ), "instance" ); | ||
var valueParameter = Expression.Parameter( typeof( object ), "value" ); | ||
Expression conversion = Expression.Convert( valueParameter, field.FieldType ); | ||
|
||
MemberExpression fieldExpression = Expression.Field( instanceParameter, field ); | ||
BinaryExpression assignExp = Expression.Assign( fieldExpression, conversion ); | ||
|
||
_setter = Expression.Lambda<Action<TEntity, object>>( | ||
assignExp, instanceParameter, valueParameter ).Compile(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.