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

Explicit mapping #18

Merged
merged 3 commits into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added extension methods/fixed issue in docs with 'OfType' -> 'ForType'
  • Loading branch information
eric-swann-q2 committed Jan 20, 2016
commit 782c83a70d356a02485b0477ba20227088c80f22
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>

#####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<TDestination>(object source, TypeAdapterConfig
/// <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<TDestination>(object source, TypeAdapterConfig
/// <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<TDestination>(object source, TypeAdapterConfig
/// <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
Loading