Skip to content
Merged
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ or just

var destObject = TypeAdapter.Adapt<TDestination>(sourceObject);

or using extension methods

var destObject = sourceObject.Adapt<TDestination>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool idea.


#####Mapping to an existing object <a name="MappingToTarget"></a>
You make the object, Mapster maps to the object.

Expand Down Expand Up @@ -233,9 +237,9 @@ You may wish to have different settings in different scenarios. If you would not
var config = new TypeAdapterConfig();
config.Default.Ignore("Id");

For type mapping, you can use `OfType` method.
For type mapping, you can use `ForType` method.

config.OfType<TSource, TDestination>()
config.ForType<TSource, TDestination>()
.Map(dest => dest.FullName,
src => string.Format("{0} {1}", src.FirstName, src.LastName));

Expand Down
1 change: 1 addition & 0 deletions src/Mapster.Tests/Mapster.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="Classes\Customer.cs" />
<Compile Include="Classes\Product.cs" />
<Compile Include="Classes\TypeTestClass.cs" />
<Compile Include="WhenMappingWithExtensionMethods.cs" />
<Compile Include="WhenPreserveReferences.cs" />
<Compile Include="WhenAddingCustomMappings.cs" />
<Compile Include="WhenAddingPrimitiveTypes.cs" />
Expand Down
73 changes: 73 additions & 0 deletions src/Mapster.Tests/WhenMappingWithExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using Mapster.Tests.Classes;
using NUnit.Framework;
using Should;

namespace Mapster.Tests
{
/// <summary>
/// Not trying to test core testing here...just a few tests to make sure the extension method approach doesn't hose anything
/// </summary>
[TestFixture]
public class WhenMappingWithExtensionMethods
{

[Test]
public void Adapt_With_Source_And_Destination_Type_Succeeds()
{
TypeAdapterConfig<Product, ProductDTO>.NewConfig()
.Compile();

var product = new Product { Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User { Name = "UserA" } };

var dto = product.Adapt<Product, ProductDTO>();

dto.ShouldNotBeNull();
dto.Id.ShouldEqual(product.Id);
}

[Test]
public void Adapt_With_Source_And_Destination_Types_And_Config_Succeeds()
{
var config = new TypeAdapterConfig();
config.ForType<Product, ProductDTO>();


var product = new Product { Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User { Name = "UserA" } };

var dto = product.Adapt<Product, ProductDTO>(config);

dto.ShouldNotBeNull();
dto.Id.ShouldEqual(product.Id);
}

[Test]
public void Adapt_With_Destination_Type_Succeeds()
{
TypeAdapterConfig<Product, ProductDTO>.NewConfig()
.Compile();

var product = new Product { Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User { Name = "UserA" } };

var dto = product.Adapt<ProductDTO>();

dto.ShouldNotBeNull();
dto.Id.ShouldEqual(product.Id);
}

[Test]
public void Adapt_With_Destination_Type_And_Config_Succeeds()
{
var config = new TypeAdapterConfig();
config.ForType<Product, ProductDTO>();


var product = new Product { Id = Guid.NewGuid(), Title = "ProductA", CreatedUser = new User { Name = "UserA" } };

var dto = product.Adapt<ProductDTO>(config);

dto.ShouldNotBeNull();
dto.Id.ShouldEqual(product.Id);
}
}
}
12 changes: 6 additions & 6 deletions src/Mapster/TypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class TypeAdapter
/// <param name="source">Source object to adapt.</param>
/// <param name="config">Configuration</param>
/// <returns>Adapted destination type.</returns>
public static TDestination Adapt<TDestination>(object source, TypeAdapterConfig config = null)
public static TDestination Adapt<TDestination>(this object source, TypeAdapterConfig config = null)
{
config = config ?? TypeAdapterConfig.GlobalSettings;
dynamic fn = config.GetMapFunction(source.GetType(), typeof(TDestination));
Expand All @@ -32,7 +32,7 @@ public static TDestination Adapt<TDestination>(object source, TypeAdapterConfig
/// <typeparam name="TDestination">Destination type.</typeparam>
/// <param name="source">Source object to adapt.</param>
/// <returns>Adapted destination type.</returns>
public static TDestination Adapt<TSource, TDestination>(TSource source)
public static TDestination Adapt<TSource, TDestination>(this TSource source)
{
try
{
Expand All @@ -52,7 +52,7 @@ public static TDestination Adapt<TSource, TDestination>(TSource source)
/// <param name="source">Source object to adapt.</param>
/// <param name="config">Configuration</param>
/// <returns>Adapted destination type.</returns>
public static TDestination Adapt<TSource, TDestination>(TSource source, TypeAdapterConfig config)
public static TDestination Adapt<TSource, TDestination>(this TSource source, TypeAdapterConfig config)
{
var fn = config.GetMapFunction<TSource, TDestination>();
try
Expand All @@ -74,7 +74,7 @@ public static TDestination Adapt<TSource, TDestination>(TSource source, TypeAdap
/// <param name="destination">The destination object to populate.</param>
/// <param name="config">Configuration</param>
/// <returns>Adapted destination type.</returns>
public static TDestination Adapt<TSource, TDestination>(TSource source, TDestination destination, TypeAdapterConfig config = null)
public static TDestination Adapt<TSource, TDestination>(this TSource source, TDestination destination, TypeAdapterConfig config = null)
{
config = config ?? TypeAdapterConfig.GlobalSettings;
var fn = config.GetMapToTargetFunction<TSource, TDestination>();
Expand All @@ -97,7 +97,7 @@ public static TDestination Adapt<TSource, TDestination>(TSource source, TDestina
/// <param name="destinationType">The type of the destination object.</param>
/// <param name="config">Configuration</param>
/// <returns>Adapted destination type.</returns>
public static object Adapt(object source, Type sourceType, Type destinationType, TypeAdapterConfig config = null)
public static object Adapt(this object source, Type sourceType, Type destinationType, TypeAdapterConfig config = null)
{
config = config ?? TypeAdapterConfig.GlobalSettings;
dynamic fn = config.GetMapFunction(sourceType, destinationType);
Expand All @@ -120,7 +120,7 @@ public static object Adapt(object source, Type sourceType, Type destinationType,
/// <param name="destinationType">The type of the destination object.</param>
/// <param name="config">Configuration</param>
/// <returns>Adapted destination type.</returns>
public static object Adapt(object source, object destination, Type sourceType, Type destinationType, TypeAdapterConfig config = null)
public static object Adapt(this object source, object destination, Type sourceType, Type destinationType, TypeAdapterConfig config = null)
{
config = config ?? TypeAdapterConfig.GlobalSettings;
dynamic fn = config.GetMapToTargetFunction(sourceType, destinationType);
Expand Down
3 changes: 3 additions & 0 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ internal TypeAdapterSettings GetMergedSettings(Type sourceType, Type destination
{
if (this.RequireExplicitMapping && mapType != MapType.InlineMap && !MapContext.HasContext)
{
if(sourceType.)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you did not intend to add this line. This PR is cool except this line, please remove.



if (!this.Dict.ContainsKey(new TypeTuple(sourceType, destinationType)))
throw new InvalidOperationException(
$"Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists for the following mapping: TSource: {sourceType} TDestination: {destinationType}");
Expand Down
Loading