Skip to content

Commit

Permalink
Typeconverter clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed May 29, 2015
1 parent 9c1c4a2 commit 3118453
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 116 deletions.
64 changes: 64 additions & 0 deletions src/AutoMapper/IMappingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace AutoMapper
{
using System.Collections.Generic;
using System.Reflection;
using Internal;

/// <summary>
/// Options for matching source/destination member types
/// </summary>
public interface IMappingOptions
{
/// <summary>
/// Naming convention for source members
/// </summary>
INamingConvention SourceMemberNamingConvention { get; }
/// <summary>
/// Naming convention for destination members
/// </summary>
INamingConvention DestinationMemberNamingConvention { get; }
/// <summary>
/// Source member name prefixes to ignore/drop
/// </summary>
IEnumerable<string> Prefixes { get; }
/// <summary>
/// Source member name postfixes to ignore/drop
/// </summary>
IEnumerable<string> Postfixes { get; }

/// <summary>
/// Destination member name prefixes to ignore/drop
/// </summary>
IEnumerable<string> DestinationPrefixes { get; }

/// <summary>
/// Destination member naem prefixes to ignore/drop
/// </summary>
IEnumerable<string> DestinationPostfixes { get; }

/// <summary>
/// Source/destination member name replacers
/// </summary>
IEnumerable<MemberNameReplacer> MemberNameReplacers { get; }

/// <summary>
/// Source/destination member aliases
/// </summary>
IEnumerable<AliasedMember> Aliases { get; }

/// <summary>
/// Allow mapping to constructors that accept arguments
/// </summary>
bool ConstructorMappingEnabled { get; }

/// <summary>
/// For mapping via data readers, enable lazy returning of values instead of immediate evalaution
/// </summary>
bool DataReaderMapperYieldReturnEnabled { get; }

/// <summary>
/// Source extension methods included for search
/// </summary>
IEnumerable<MethodInfo> SourceExtensionMethods { get; }
}
}
91 changes: 0 additions & 91 deletions src/AutoMapper/INamingConvention.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using AutoMapper.Internal;

namespace AutoMapper
{
Expand All @@ -20,92 +17,4 @@ public interface INamingConvention
/// </summary>
string SeparatorCharacter { get; }
}

/// <summary>
/// Options for matching source/destination member types
/// </summary>
public interface IMappingOptions
{
/// <summary>
/// Naming convention for source members
/// </summary>
INamingConvention SourceMemberNamingConvention { get; }
/// <summary>
/// Naming convention for destination members
/// </summary>
INamingConvention DestinationMemberNamingConvention { get; }
/// <summary>
/// Source member name prefixes to ignore/drop
/// </summary>
IEnumerable<string> Prefixes { get; }
/// <summary>
/// Source member name postfixes to ignore/drop
/// </summary>
IEnumerable<string> Postfixes { get; }

/// <summary>
/// Destination member name prefixes to ignore/drop
/// </summary>
IEnumerable<string> DestinationPrefixes { get; }

/// <summary>
/// Destination member naem prefixes to ignore/drop
/// </summary>
IEnumerable<string> DestinationPostfixes { get; }

/// <summary>
/// Source/destination member name replacers
/// </summary>
IEnumerable<MemberNameReplacer> MemberNameReplacers { get; }

/// <summary>
/// Source/destination member aliases
/// </summary>
IEnumerable<AliasedMember> Aliases { get; }

/// <summary>
/// Allow mapping to constructors that accept arguments
/// </summary>
bool ConstructorMappingEnabled { get; }

/// <summary>
/// For mapping via data readers, enable lazy returning of values instead of immediate evalaution
/// </summary>
bool DataReaderMapperYieldReturnEnabled { get; }

/// <summary>
/// Source extension methods included for search
/// </summary>
IEnumerable<MethodInfo> SourceExtensionMethods { get; }
}

public class PascalCaseNamingConvention : INamingConvention
{
private readonly Regex _splittingExpression = new Regex(@"(\p{Lu}+(?=$|\p{Lu}[\p{Ll}0-9])|\p{Lu}?[\p{Ll}0-9]+)");

public Regex SplittingExpression
{
get { return _splittingExpression; }
}

public string SeparatorCharacter
{
get { return string.Empty; }
}
}

public class LowerUnderscoreNamingConvention : INamingConvention
{
private readonly Regex _splittingExpression = new Regex(@"[\p{Ll}0-9]+(?=_?)");

public Regex SplittingExpression
{
get { return _splittingExpression; }
}

public string SeparatorCharacter
{
get { return "_"; }
}
}
}
25 changes: 0 additions & 25 deletions src/AutoMapper/ITypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,4 @@ public interface ITypeConverter<in TSource, out TDestination>
/// <returns>Destination object</returns>
TDestination Convert(ResolutionContext context);
}

/// <summary>
/// Generic-friendly implementation of <see cref="ITypeConverter{TSource,TDestination}"/>
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TDestination">Destination type</typeparam>
public abstract class TypeConverter<TSource, TDestination> : ITypeConverter<TSource, TDestination>
{
public TDestination Convert(ResolutionContext context)
{
if (context.SourceValue != null && !(context.SourceValue is TSource))
{
throw new AutoMapperMappingException(context, string.Format("Value supplied is of type {0} but expected {1}.\nChange the type converter source type, or redirect the source value supplied to the value resolver using FromMember.", typeof(TSource), context.SourceValue.GetType()));
}

return ConvertCore((TSource)context.SourceValue);
}

/// <summary>
/// When overridden in a base class, this method is provided the casted source object extracted from the <see cref="ResolutionContext"/>
/// </summary>
/// <param name="source">Source object</param>
/// <returns>Destination object</returns>
protected abstract TDestination ConvertCore(TSource source);
}
}
11 changes: 11 additions & 0 deletions src/AutoMapper/LowerUnderscoreNamingConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AutoMapper
{
using System.Text.RegularExpressions;

public class LowerUnderscoreNamingConvention : INamingConvention
{
public Regex SplittingExpression { get; } = new Regex(@"[\p{Ll}0-9]+(?=_?)");

public string SeparatorCharacter => "_";
}
}
11 changes: 11 additions & 0 deletions src/AutoMapper/PascalCaseNamingConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AutoMapper
{
using System.Text.RegularExpressions;

public class PascalCaseNamingConvention : INamingConvention
{
public Regex SplittingExpression { get; } = new Regex(@"(\p{Lu}+(?=$|\p{Lu}[\p{Ll}0-9])|\p{Lu}?[\p{Ll}0-9]+)");

public string SeparatorCharacter => string.Empty;
}
}
28 changes: 28 additions & 0 deletions src/AutoMapper/TypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace AutoMapper
{
/// <summary>
/// Generic-friendly implementation of <see cref="ITypeConverter{TSource,TDestination}"/>
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TDestination">Destination type</typeparam>
public abstract class TypeConverter<TSource, TDestination> : ITypeConverter<TSource, TDestination>
{
public TDestination Convert(ResolutionContext context)
{
if (context.SourceValue != null && !(context.SourceValue is TSource))
{
throw new AutoMapperMappingException(context,
$"Value supplied is of type {typeof (TSource)} but expected {context.SourceValue.GetType()}.\nChange the type converter source type, or redirect the source value supplied to the value resolver using FromMember.");
}

return ConvertCore((TSource)context.SourceValue);
}

/// <summary>
/// When overridden in a base class, this method is provided the casted source object extracted from the <see cref="ResolutionContext"/>
/// </summary>
/// <param name="source">Source object</param>
/// <returns>Destination object</returns>
protected abstract TDestination ConvertCore(TSource source);
}
}

0 comments on commit 3118453

Please sign in to comment.