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 Metadata to ColumnInfo which can be accessible from IMapper #690

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
4 changes: 2 additions & 2 deletions src/NPoco/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace NPoco
{
Expand All @@ -26,10 +25,11 @@ public class ColumnInfo
public string ReferenceMemberName { get; set; }
public MemberInfo MemberInfo { get; internal set; }
public bool ExactColumnNameMatch { get; set; }
public IReadOnlyDictionary<string, object> Metadata { get; set; }

public static ColumnInfo FromMemberInfo(MemberInfo mi)
{
var ci = new ColumnInfo{MemberInfo = mi};
var ci = new ColumnInfo { MemberInfo = mi };
var attrs = ReflectionUtils.GetCustomAttributes(mi).ToArray();
var colAttrs = attrs.OfType<ColumnAttribute>().ToArray();
var columnTypeAttrs = attrs.OfType<ColumnTypeAttribute>().ToArray();
Expand Down
6 changes: 3 additions & 3 deletions src/NPoco/DefaultMapper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Data.Common;
using System.Reflection;

namespace NPoco
{
public abstract class DefaultMapper : IMapper
{
public virtual Func<object, object> GetFromDbConverter(MemberInfo destMemberInfo, Type sourceType)
public virtual Func<object, object> GetFromDbConverter(MemberInfo destMemberInfo, Type sourceType, IReadOnlyDictionary<string, object> metadata = null)
{
var type = destMemberInfo.GetMemberInfoType();
return destMemberInfo != null ? GetFromDbConverter(type, sourceType) : null;
Expand All @@ -18,7 +18,7 @@ public virtual Func<object, object> GetFromDbConverter(Type destType, Type sourc
return null;
}

public virtual Func<object, object> GetToDbConverter(Type destType, MemberInfo sourceMemberInfo)
public virtual Func<object, object> GetToDbConverter(Type destType, MemberInfo sourceMemberInfo, IReadOnlyDictionary<string, object> metadata = null)
{
return null;
}
Expand Down
13 changes: 10 additions & 3 deletions src/NPoco/FluentMappings/ColumnConfigurationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;

namespace NPoco.FluentMappings
{
Expand Down Expand Up @@ -118,6 +117,7 @@ public interface IColumnBuilder<TModel>
IColumnBuilder<TModel> ValueObject();
IColumnBuilder<TModel> ValueObject(Expression<Func<TModel, object>> member);
IColumnBuilder<TModel> ForceToUtc(bool enabled);
IColumnBuilder<TModel> WithMetadata(string key, object value);
}

public class ColumnBuilder<TModel> : IColumnBuilder<TModel>
Expand Down Expand Up @@ -156,7 +156,7 @@ public IColumnBuilder<TModel> WithDbType(Type type)

public IColumnBuilder<TModel> WithDbType<T>()
{
return WithDbType(typeof (T));
return WithDbType(typeof(T));
}

public IColumnBuilder<TModel> Version()
Expand Down Expand Up @@ -253,5 +253,12 @@ public IColumnBuilder<TModel> ForceToUtc(bool enabled)
_columnDefinition.ForceUtc = enabled;
return this;
}

public IColumnBuilder<TModel> WithMetadata(string key, object value)
{
_columnDefinition.Metadata ??= new Dictionary<string, object>(1);
_columnDefinition.Metadata.Add(key, value);
return this;
}
}
}
2 changes: 2 additions & 0 deletions src/NPoco/FluentMappings/ColumnDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace NPoco.FluentMappings
Expand Down Expand Up @@ -26,5 +27,6 @@ public class ColumnDefinition
public bool? ValueObjectColumn { get; set; }
public string ValueObjectColumnName { get; set; }
public bool? ExactColumnNameMatch { get; set; }
public Dictionary<string, object> Metadata { get; set; }
}
}
18 changes: 10 additions & 8 deletions src/NPoco/FluentMappings/FluentMappingsPocoDataBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class FluentMappingsPocoDataBuilder : PocoDataBuilder
{
private readonly Mappings _mappings;

public FluentMappingsPocoDataBuilder(Type type, Mappings mappings, MapperCollection mapper) :
public FluentMappingsPocoDataBuilder(Type type, Mappings mappings, MapperCollection mapper) :
base(type, mapper)
{
_mappings = mappings;
Expand Down Expand Up @@ -42,18 +42,18 @@ protected override TableInfoPlan GetTableInfo(Type type, ColumnInfo[] columnInfo
primaryKey = string.Join(",", originalPk);
}
}

a = typeConfig.SequenceName ?? "";
var sequenceName = a.Length == 0 ? null : a;

var autoIncrement = typeConfig.AutoIncrement ?? true;

// Set autoincrement false if primary key has multiple columns
autoIncrement = autoIncrement ? !primaryKey.Contains(',') : autoIncrement;

// Set auto alias
var autoAlias = CreateAlias(type.Name, type);

return () => new TableInfo
{
TableName = tableName,
Expand All @@ -67,10 +67,10 @@ protected override TableInfoPlan GetTableInfo(Type type, ColumnInfo[] columnInfo

protected override bool ShouldIncludePrivateColumn(MemberInfo mi, Type type)
{
if (_mappings.Config.ContainsKey(type)
if (_mappings.Config.ContainsKey(type)
&& _mappings.Config[type].ColumnConfiguration.ContainsKey(mi.Name))
return true;

return base.ShouldIncludePrivateColumn(mi, type);
}

Expand All @@ -80,7 +80,7 @@ protected override ColumnInfo GetColumnInfo(MemberInfo mi, Type type)
return base.GetColumnInfo(mi, type);

var typeConfig = _mappings.Config[type];
var columnInfo = new ColumnInfo() {MemberInfo = mi};
var columnInfo = new ColumnInfo() { MemberInfo = mi };
var key = mi.Name;

bool explicitColumns = typeConfig.ExplicitColumns ?? false;
Expand Down Expand Up @@ -144,8 +144,10 @@ protected override ColumnInfo GetColumnInfo(MemberInfo mi, Type type)
}

columnInfo.ColumnType = colattr.DbColumnType;

columnInfo.Metadata = colattr.Metadata;
}

return columnInfo;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/NPoco/IMapper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Data;
using System.Collections.Generic;
using System.Data.Common;
using System.Reflection;

namespace NPoco
{
public interface IMapper
{
Func<object, object> GetFromDbConverter(MemberInfo memberInfo, Type sourceType);
Func<object, object> GetFromDbConverter(MemberInfo memberInfo, Type sourceType, IReadOnlyDictionary<string, object> metadata = null);
Func<object, object> GetFromDbConverter(Type destType, Type sourceType);
Func<object, object> GetParameterConverter(DbCommand dbCommand, Type sourceType);
Func<object, object> GetToDbConverter(Type destType, MemberInfo sourceMemberInfo);
Func<object, object> GetToDbConverter(Type destType, MemberInfo sourceMemberInfo, IReadOnlyDictionary<string, object> metadata = null);
}
}
12 changes: 6 additions & 6 deletions src/NPoco/MapperCollection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Common;
using System.Linq;
using System.Reflection;

namespace NPoco
Expand All @@ -26,7 +26,7 @@ public void RegisterFactory<T>(Func<DbDataReader, T> factory)

public ObjectFactoryDelegate GetFactory(Type type)
{
return Factories.ContainsKey(type) ? Factories[type] : null;
return Factories.TryGetValue(type, out var factory) ? factory : null;
}

public bool HasFactory(Type type)
Expand Down Expand Up @@ -66,16 +66,16 @@ internal Func<object, object> FindFromDbConverter(Type destType, Type srcType)
return FromDbConverterCache.Get(key, () => Find(x => x.GetFromDbConverter(destType, srcType)));
}

internal Func<object, object> FindFromDbConverter(MemberInfo destInfo, Type srcType)
internal Func<object, object> FindFromDbConverter(MemberInfo destInfo, Type srcType, IReadOnlyDictionary<string, object> metadata = null)
{
var key = new { DestInfo = destInfo, SrcType = srcType };
return FromDbConverterCache.Get(key, () => Find(x => x.GetFromDbConverter(destInfo, srcType)));
return FromDbConverterCache.Get(key, () => Find(x => x.GetFromDbConverter(destInfo, srcType, metadata)));
}

internal Func<object, object> FindToDbConverter(Type destType, MemberInfo srcInfo)
internal Func<object, object> FindToDbConverter(Type destType, MemberInfo srcInfo, IReadOnlyDictionary<string, object> metadata = null)
{
var key = new { DestType = destType, SrcInfo = srcInfo };
return ToDbConverterCache.Get(key, () => Find(x => x.GetToDbConverter(destType, srcInfo)));
return ToDbConverterCache.Get(key, () => Find(x => x.GetToDbConverter(destType, srcInfo, metadata)));
}
}
}
13 changes: 7 additions & 6 deletions src/NPoco/MappingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

Expand All @@ -18,21 +16,24 @@ public static Func<object, object> GetConverter(MapperCollection mapper, PocoCol
// Get converter from the mapper
if (mapper != null)
{
converter = pc != null && pc.MemberInfoData != null ? mapper.FindFromDbConverter(pc.MemberInfoData.MemberInfo, srcType) : mapper.FindFromDbConverter(dstType, srcType);
converter = pc != null && pc.MemberInfoData != null
? mapper.FindFromDbConverter(pc.MemberInfoData.MemberInfo, srcType, pc.Metadata)
: mapper.FindFromDbConverter(dstType, srcType);

if (converter != null)
return converter;
}

if (pc != null && pc.SerializedColumn && mapper?.ColumnSerializer != null)
{
converter = src => mapper.ColumnSerializer.Deserialize((string) src, dstType);
converter = src => mapper.ColumnSerializer.Deserialize((string)src, dstType);
return converter;
}

// Standard DateTime->Utc mapper
if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
{
converter = src => new DateTime(((DateTime) src).Ticks, DateTimeKind.Utc);
converter = src => new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc);
return converter;
}

Expand All @@ -54,7 +55,7 @@ public static Func<object, object> GetConverter(MapperCollection mapper, PocoCol
}
else if (srcType == typeof(string) && (dstType == typeof(Guid) || dstType == typeof(Guid?)))
{
converter = src => Guid.Parse((string) src);
converter = src => Guid.Parse((string)src);
}
else if ((!pc?.ValueObjectColumn ?? true) && !dstType.IsAssignableFrom(srcType))
{
Expand Down
5 changes: 3 additions & 2 deletions src/NPoco/PocoColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Type ColumnType
public bool ValueObjectColumn { get; set; }
public string ValueObjectColumnName { get; set; }
public bool ExactColumnNameMatch { get; set; }
public IReadOnlyDictionary<string, object> Metadata { get; set; }

internal void SetMemberAccessors(List<MemberAccessor> memberAccessors)
{
Expand Down Expand Up @@ -82,8 +83,8 @@ public virtual void SetValue(object target, object val)

public virtual object GetValue(object target)
{
return valueObjectGetter != null
? valueObjectGetter(GetRecursiveValue(target) ?? fastCreate.Create(null))
return valueObjectGetter != null
? valueObjectGetter(GetRecursiveValue(target) ?? fastCreate.Create(null))
: GetRecursiveValue(target);
}

Expand Down
24 changes: 11 additions & 13 deletions src/NPoco/PocoDataBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NPoco.FluentMappings;
using NPoco.RowMappers;

namespace NPoco
{
Expand Down Expand Up @@ -65,7 +62,7 @@ public ColumnInfo[] GetColumnInfos(Type type)

public static bool IsDictionaryType(Type type)
{
return new[] {typeof(object), typeof(IDictionary<string, object>), typeof(Dictionary<string, object>)}.Contains(type)
return new[] { typeof(object), typeof(IDictionary<string, object>), typeof(Dictionary<string, object>) }.Contains(type)
|| (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>) && type.GetGenericArguments().First() == typeof(string));
}

Expand Down Expand Up @@ -111,14 +108,14 @@ private static IEnumerable<PocoColumn> GetPocoColumns(IEnumerable<PocoMember> me
yield return member.PocoColumn;
break;
case ReferenceType.None:
{
yield return member.PocoColumn;
foreach (var pocoMemberChild in GetPocoColumns(member.PocoMemberChildren))
{
yield return pocoMemberChild;
yield return member.PocoColumn;
foreach (var pocoMemberChild in GetPocoColumns(member.PocoMemberChildren))
{
yield return pocoMemberChild;
}
break;
}
break;
}
}
}
}
Expand All @@ -136,8 +133,8 @@ public IEnumerable<PocoMemberPlan> GetPocoMembers(ColumnInfo[] columnInfos, List
if (columnInfo.ReferenceType == ReferenceType.Many)
{
var genericArguments = memberInfoType.GetGenericArguments();
memberInfoType = genericArguments.Any()
? genericArguments.First()
memberInfoType = genericArguments.Any()
? genericArguments.First()
: memberInfoType.GetTypeWithGenericTypeDefinitionOf(typeof(IList<>)).GetGenericArguments().First();
}

Expand Down Expand Up @@ -175,7 +172,7 @@ public IEnumerable<PocoMemberPlan> GetPocoMembers(ColumnInfo[] columnInfos, List
var fastCreate = GetFastCreate(memberType, Mapper, isList, isDynamic);
var columnName = GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name);
var memberInfoData = new MemberInfoData(capturedMemberInfo);

yield return tableInfo =>
{
var pc = new PocoColumn
Expand All @@ -196,6 +193,7 @@ public IEnumerable<PocoMemberPlan> GetPocoMembers(ColumnInfo[] columnInfos, List
VersionColumnType = capturedColumnInfo.VersionColumnType,
SerializedColumn = capturedColumnInfo.SerializedColumn,
ValueObjectColumn = capturedColumnInfo.ValueObjectColumn,
Metadata = capturedColumnInfo.Metadata
};

if (pc.ValueObjectColumn)
Expand Down
Loading