-
Notifications
You must be signed in to change notification settings - Fork 127
Issue #421 pull request. #1 #422
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
Open
cwaldron
wants to merge
7
commits into
mikependon:master
Choose a base branch
from
cwaldron:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8664bda
Added PropertyMap support to RepoDb.
cwaldron 3069241
Refactored inner functions that creates column assignment to private …
cwaldron 99fa630
More refactoring of FactoryFunction2 moving static methodinfos to Typ…
cwaldron 8bea5da
Enable ability to convert property to the database type.
cwaldron 35d3744
Fixed conversion check to also check for converterMethod.
cwaldron 337aa7e
Added property to object handler to StringToDateClass
cwaldron 19c5c4f
Merge pull request #1 from mikependon/master
cwaldron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
16 changes: 12 additions & 4 deletions
16
RepoDb.Core/RepoDb.Tests/RepoDb.IntegrationTests/Models/IdentityTableWithDifferentPrimary.cs
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using RepoDb.Extensions; | ||
using RepoDb.Interfaces; | ||
|
||
namespace RepoDb.Entity | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
public abstract class BaseEntity<TEntity> where TEntity : class | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static readonly IPropertyMap<TEntity> PropertyMap = new EntityPropertyMap<TEntity>(); | ||
|
||
static BaseEntity() | ||
{ | ||
typeof(TEntity).GetProperties().ForEach(x => PropertyMap.Map(x)); | ||
} | ||
|
||
/// <summary> | ||
/// Map property by expression. | ||
/// </summary> | ||
/// <typeparam name="TProperty">type of the property</typeparam> | ||
/// <param name="expression">mapping expression</param> | ||
/// <returns>mapper</returns> | ||
public IConverter Map<TProperty>(Expression<Func<TEntity, TProperty>> expression) | ||
{ | ||
return PropertyMap.Map(expression); | ||
} | ||
} | ||
} |
This file contains hidden or 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,30 @@ | ||
using System; | ||
using RepoDb.Interfaces; | ||
|
||
namespace RepoDb.Entity | ||
{ | ||
internal class EntityPropertyConverter : IConverter | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
internal object ToProperty { get; private set; } | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
internal object ToObject { get; private set; } | ||
|
||
public IConverter Convert<TProperty>(Func<TProperty, object> func) | ||
{ | ||
ToObject = func; | ||
return this; | ||
} | ||
|
||
public IConverter Convert<TFrom, TProperty>(Func<TFrom, TProperty> func) | ||
{ | ||
ToProperty = func; | ||
return this; | ||
} | ||
} | ||
} |
This file contains hidden or 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,94 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using RepoDb.Extensions; | ||
using RepoDb.Interfaces; | ||
|
||
namespace RepoDb.Entity | ||
{ | ||
internal class EntityPropertyMap<TEntity> : IPropertyMap<TEntity> where TEntity : class | ||
{ | ||
private readonly IDictionary<string, IConverter> m_propertyMap = new ConcurrentDictionary<string, IConverter>(); | ||
|
||
/// <summary> | ||
/// Find the sql property from the property info map. | ||
/// </summary> | ||
/// <param name="prop">property info</param> | ||
/// <returns></returns> | ||
public IConverter Find(PropertyInfo prop) | ||
{ | ||
return m_propertyMap.TryGetValue(prop.Name, out var value) ? value : default; | ||
} | ||
|
||
/// <summary> | ||
/// Map property info. | ||
/// </summary> | ||
/// <param name="propertyInfo"></param> | ||
/// <returns></returns> | ||
public IConverter Map(PropertyInfo propertyInfo) | ||
{ | ||
var mapper = m_propertyMap.GetOrAdd(propertyInfo.Name, new EntityPropertyConverter()); | ||
return mapper; | ||
} | ||
|
||
/// <summary> | ||
/// Map property by expression. | ||
/// </summary> | ||
/// <typeparam name="TProperty">type of the property</typeparam> | ||
/// <param name="expression">mapping expression</param> | ||
/// <returns>mapper</returns> | ||
public IConverter Map<TProperty>(Expression<Func<TEntity, TProperty>> expression) | ||
{ | ||
var info = (PropertyInfo) GetMemberInfo(expression); | ||
return Map(info); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the <see cref="T:System.Reflection.MemberInfo"/> for the specified lambda expression. | ||
/// </summary> | ||
/// <param name="lambda">A lambda expression containing a MemberExpression.</param> | ||
/// <returns>A MemberInfo object for the member in the specified lambda expression.</returns> | ||
private static MemberInfo GetMemberInfo(LambdaExpression lambda) | ||
{ | ||
Expression expr = lambda; | ||
while (true) | ||
{ | ||
switch (expr.NodeType) | ||
{ | ||
case ExpressionType.Lambda: | ||
expr = ((LambdaExpression) expr).Body; | ||
break; | ||
|
||
case ExpressionType.Convert: | ||
expr = ((UnaryExpression) expr).Operand; | ||
break; | ||
|
||
case ExpressionType.MemberAccess: | ||
var memberExpression = (MemberExpression) expr; | ||
var baseMember = memberExpression.Member; | ||
|
||
while (memberExpression != null) | ||
{ | ||
var type = memberExpression.Type; | ||
if (type.GetMembers().Any(member => member.Name == baseMember.Name)) | ||
{ | ||
return type.GetMember(baseMember.Name).First(); | ||
} | ||
|
||
memberExpression = memberExpression.Expression as MemberExpression; | ||
} | ||
|
||
// Make sure we get the property from the derived type. | ||
var paramType = lambda.Parameters[0].Type; | ||
return paramType.GetMember(baseMember.Name).First(); | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace RepoDb.Extensions | ||
{ | ||
/// <summary> | ||
/// An extension class for <see cref="IEnumerable{T}"/>. | ||
/// </summary> | ||
public static class DictionaryExtension | ||
{ | ||
/// <summary> | ||
/// Retrieve or add a value to dictionary | ||
/// </summary> | ||
/// <typeparam name="TK">key type</typeparam> | ||
/// <typeparam name="TV">value type</typeparam> | ||
/// <param name="dictionary">dictionary</param> | ||
/// <param name="key">key value</param> | ||
/// <param name="value">value</param> | ||
/// <returns>value</returns> | ||
public static TV GetOrAdd<TK, TV>(this IDictionary<TK, TV> dictionary, TK key, TV value) | ||
{ | ||
if (!dictionary.ContainsKey(key)) | ||
{ | ||
dictionary.Add(key, value); | ||
} | ||
|
||
return dictionary[key]; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve or add a value to dictionary via add function. | ||
/// </summary> | ||
/// <typeparam name="TK">key type</typeparam> | ||
/// <typeparam name="TV">value type</typeparam> | ||
/// <param name="dictionary">dictionary</param> | ||
/// <param name="key">key value</param> | ||
/// <param name="addFunc">add function returning the value</param> | ||
/// <returns>value</returns> | ||
public static TV GetOrAdd<TK, TV>(this IDictionary<TK, TV> dictionary, TK key, Func<TV> addFunc) | ||
{ | ||
if (!dictionary.ContainsKey(key)) | ||
{ | ||
dictionary.Add(key, addFunc()); | ||
} | ||
|
||
return dictionary[key]; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.