Skip to content

Commit

Permalink
Reduce the need for custom expressions. (#144)
Browse files Browse the repository at this point in the history
Reducing the need for custom expressions in literal member maps.
  • Loading branch information
BlaiseD authored Jun 28, 2022
1 parent d7f1cec commit 8817161
Show file tree
Hide file tree
Showing 10 changed files with 593 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MemberExpression Result
if (string.IsNullOrEmpty(result) || next.Contains(result))
result = next;
else throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
Resource.includeExpressionTooComplex,
Properties.Resources.includeExpressionTooComplex,
string.Concat(_newParentExpression.Type.Name, period, result),
string.Concat(_newParentExpression.Type.Name, period, next)));
Expand All @@ -50,7 +50,7 @@ protected override Expression VisitMember(MemberExpression node)
if (node.Expression.NodeType == ExpressionType.MemberAccess && node.Type.IsLiteralType())
_memberExpressions.Add((MemberExpression)node.Expression);
else if (node.Expression.NodeType == ExpressionType.Parameter && node.Type.IsLiteralType())
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.mappedMemberIsChildOfTheParameterFormat, node.GetPropertyFullName(), node.Type.FullName, sType.FullName));
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.mappedMemberIsChildOfTheParameterFormat, node.GetPropertyFullName(), node.Type.FullName, sType.FullName));
else
_memberExpressions.Add(node);
}
Expand Down
10 changes: 5 additions & 5 deletions src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ TDestDelegate MapBody(Dictionary<Type, Type> typeMappings, XpressionMapperVisito
TDestDelegate GetLambda(Dictionary<Type, Type> typeMappings, XpressionMapperVisitor visitor, Expression mappedBody)
{
if (mappedBody == null)
throw new InvalidOperationException(Resource.cantRemapExpression);
throw new InvalidOperationException(Properties.Resources.cantRemapExpression);

return (TDestDelegate)Lambda
(
Expand Down Expand Up @@ -255,7 +255,7 @@ public static List<ParameterExpression> GetDestinationParameterExpressions(this
/// <returns></returns>
public static Dictionary<Type, Type> AddTypeMapping<TSource, TDest>(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider)
=> typeMappings == null
? throw new ArgumentException(Resource.typeMappingsDictionaryIsNull)
? throw new ArgumentException(Properties.Resources.typeMappingsDictionaryIsNull)
: typeMappings.AddTypeMapping(configurationProvider, typeof(TSource), typeof(TDest));

private static bool HasUnderlyingType(this Type type)
Expand Down Expand Up @@ -284,7 +284,7 @@ private static void AddUnderlyingTypes(this Dictionary<Type, Type> typeMappings,
public static Dictionary<Type, Type> AddTypeMapping(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type sourceType, Type destType)
{
if (typeMappings == null)
throw new ArgumentException(Resource.typeMappingsDictionaryIsNull);
throw new ArgumentException(Properties.Resources.typeMappingsDictionaryIsNull);

if (sourceType.GetTypeInfo().IsGenericType && sourceType.GetGenericTypeDefinition() == typeof(Expression<>))
{
Expand Down Expand Up @@ -357,7 +357,7 @@ public static Type ReplaceType(this Dictionary<Type, Type> typeMappings, Type so
private static Dictionary<Type, Type> AddTypeMappingsFromDelegates(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type sourceType, Type destType)
{
if (typeMappings == null)
throw new ArgumentException(Resource.typeMappingsDictionaryIsNull);
throw new ArgumentException(Properties.Resources.typeMappingsDictionaryIsNull);

typeMappings.DoAddTypeMappingsFromDelegates
(
Expand All @@ -372,7 +372,7 @@ private static Dictionary<Type, Type> AddTypeMappingsFromDelegates(this Dictiona
private static void DoAddTypeMappingsFromDelegates(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, List<Type> sourceArguments, List<Type> destArguments)
{
if (sourceArguments.Count != destArguments.Count)
throw new ArgumentException(Resource.invalidArgumentCount);
throw new ArgumentException(Properties.Resources.invalidArgumentCount);

for (int i = 0; i < sourceArguments.Count; i++)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 146 additions & 0 deletions src/AutoMapper.Extensions.ExpressionMapping/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="cannotCreateBinaryExpressionFormat" xml:space="preserve">
<value>Cannot create a binary expression for the following pair. Node: {0}, Type: {1} and Node: {2}, Type: {3}.</value>
<comment>0=leftNode; 1=leftNodeType; 2=rightNode; 3=rightNodeType</comment>
</data>
<data name="cantRemapExpression" xml:space="preserve">
<value>Can't rempa expression</value>
</data>
<data name="expressionMapValueTypeMustMatchFormat" xml:space="preserve">
<value>The source and destination types must be the same for expression mapping between literal types. Source Type: {0}, Source Description: {1}, Destination Type: {2}, Destination Property: {3}.</value>
<comment>0=Source Type; 1=SourceDescription; 2=Destination Type; 3=Destination Property.</comment>
</data>
<data name="includeExpressionTooComplex" xml:space="preserve">
<value>The Include value-type expression uses multiple sibling navigation objects "{0}", "{1}" and is too complex to translate.</value>
<comment>0=FirstNavigationProperty, 1=SecondNavigationProperty</comment>
</data>
<data name="invalidArgumentCount" xml:space="preserve">
<value>Source and destination must have the same number of arguments.</value>
</data>
<data name="invalidExpErr" xml:space="preserve">
<value>Invalid expression type for this operation.</value>
</data>
<data name="mapperInfoDictionaryIsNull" xml:space="preserve">
<value>Mapper Info dictionary cannot be null.</value>
</data>
<data name="srcMemberCannotBeNullFormat" xml:space="preserve">
<value>SourceMember cannot be null. Source Type: {0}, Destination Type: {1}, Property: {2}.</value>
<comment>0=SorceType; 1=DestinationType; 2=Name of the source property</comment>
</data>
<data name="typeMappingsDictionaryIsNull" xml:space="preserve">
<value>Type Mappings dictionary cannot be null.</value>
</data>
<data name="customResolversNotSupported" xml:space="preserve">
<value>Custom resolvers are not supported for expression mapping.</value>
</data>
<data name="mustBeExpressions" xml:space="preserve">
<value>Arguments must be expressions.</value>
</data>
<data name="mappedMemberIsChildOfTheParameterFormat" xml:space="preserve">
<value>The mapped member {0} is of type {1} and a child of the parameter type {2}. No reference type (parent of) {0} is available to map as an include.</value>
<comment>0=memberName, 1=memberType; 2=parameterType</comment>
</data>
<data name="makeParentTypesMatchForMembersOfLiteralsFormat" xml:space="preserve">
<value>For members of literal types, use IMappingExpression.ForMember() to make the parent property types an exact match. Parent Source Type: {0}, Parent Destination Type: {1}, Full Member Name "{2}".</value>
<comment>0=typeSource, 1=typeDestination; 2=sourceFullName</comment>
</data>
</root>
Loading

0 comments on commit 8817161

Please sign in to comment.