-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
feat: inline methods consisting of a single return statement or local variable declaration expression #1422
feat: inline methods consisting of a single return statement or local variable declaration expression #1422
Conversation
dbd1e5a
to
5fb7526
Compare
5fb7526
to
14977e2
Compare
You can do this with current features: Source: [Mapper]
public static partial class Mapper
{
private static partial System.Linq.IQueryable<B> Map(this System.Linq.IQueryable<A> source);
[MapProperty(nameof(C.Value), nameof(D.Value), Use = nameof(GetComputedValue))]
private static partial D MapToD(C v);
[UserMapping(Default = false)]
private static string GetComputedValue(string value) => value + "-mapped";
}
class A { public string StringValue { get; set; } public C NestedValue { get; set; } }
class B { public string StringValue { get; set; } public D NestedValue { get; set; } }
class C { public string Value { get; set; } }
class D { public string Value { get; set; } } Generated // <auto-generated />
#nullable enable
public static partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private static partial global::System.Linq.IQueryable<B> Map(this global::System.Linq.IQueryable<A> source)
{
#nullable disable
return System.Linq.Queryable.Select(source, x => new B()
{
StringValue = x.StringValue,
NestedValue = new D()
{
Value = x.NestedValue.Value + "-mapped",
},
});
#nullable enable
}
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private static partial D MapToD(C v)
{
var target = new D();
target.Value = GetComputedValue(v.Value);
return target;
}
} |
@trejjam I know that you can do this if you use an expression body. That's the point of this pull request. That you don't have to think about the code style. It just works regardlessly how you write your user mapping. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this contribution!
As @trejjam pointed out this works already by using method expressions. I can understand that there are use-cases where one doesn't want to use expressions and I think it's nice that the Mapperly inlining just works out of the box 😊
I added my feedback.
src/Riok.Mapperly/Descriptors/MappingBuilders/InlineExpressionMappingBuilder.cs
Outdated
Show resolved
Hide resolved
src/Riok.Mapperly/Descriptors/MappingBuilders/InlineExpressionMappingBuilder.cs
Outdated
Show resolved
Hide resolved
src/Riok.Mapperly/Descriptors/MappingBuilders/InlineExpressionMappingBuilder.cs
Outdated
Show resolved
Hide resolved
src/Riok.Mapperly/Descriptors/MappingBuilders/InlineExpressionMappingBuilder.cs
Outdated
Show resolved
Hide resolved
src/Riok.Mapperly/Descriptors/MappingBuilders/InlineExpressionMappingBuilder.cs
Outdated
Show resolved
Hide resolved
Relates #1406 |
14977e2
to
da50bf9
Compare
da50bf9
to
0e680f5
Compare
feat: inline methods consisting of a single return statement or local variable declaration expression
Description
Fixes # (issue)
Checklist