Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Updated MappingServices to use underlying types for collections
- Created MappingServices feature to verify functionality
  • Loading branch information
jbatte47 committed Nov 20, 2013
1 parent 9307918 commit 215a60a
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 14 deletions.
43 changes: 31 additions & 12 deletions src/Patterns/Mapping/MappingServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#region FreeBSD

// Copyright (c) 2013, John Batte
// Copyright (c) 2013, The Tribe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
Expand All @@ -24,13 +24,15 @@
#endregion

using System;
using System.Collections;
using System.Linq;

using AutoMapper;

namespace Patterns.Mapping
{
/// <summary>
/// Provides a DI-driven implementation of <see cref="IMappingServices"/>
/// Provides a DI-driven implementation of <see cref="IMappingServices" />
/// </summary>
public class MappingServices : IMappingServices
{
Expand All @@ -53,40 +55,40 @@ public class MappingServices : IMappingServices
}

/// <summary>
/// Gets the engine.
/// Gets the engine.
/// </summary>
/// <value>
/// The engine.
/// The engine.
/// </value>
public IMappingEngine Engine
{
get { return _engine; }
}

/// <summary>
/// Gets the configuration.
/// Gets the configuration.
/// </summary>
/// <value>
/// The configuration.
/// The configuration.
/// </value>
public IConfiguration Configuration
{
get { return _configuration; }
}

/// <summary>
/// Gets the configuration provider.
/// Gets the configuration provider.
/// </summary>
/// <value>
/// The configuration provider.
/// The configuration provider.
/// </value>
public IConfigurationProvider ConfigurationProvider
{
get { return _configurationProvider; }
}

/// <summary>
/// Maps the specified source to the indicated destination.
/// Maps the specified source to the indicated destination.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
Expand Down Expand Up @@ -166,9 +168,10 @@ public TDestination Map<TDestination>(object source, Action<IMappingOperationOpt
/// <param name="destination">The destination.</param>
/// <param name="opts">The opts.</param>
/// <returns></returns>
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination, Action<IMappingOperationOptions> opts)
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination,
Action<IMappingOperationOptions> opts)
{
EnsureTypeMapPresence(typeof(TSource), typeof(TDestination));
EnsureTypeMapPresence(typeof (TSource), typeof (TDestination));

return Engine.Map(source, destination, opts);
}
Expand Down Expand Up @@ -226,14 +229,30 @@ public object Map(object source, object destination, Type sourceType, Type desti
/// <param name="destinationType">Type of the destination.</param>
/// <param name="opts">The opts.</param>
/// <returns></returns>
public object Map(object source, object destination, Type sourceType, Type destinationType, Action<IMappingOperationOptions> opts)
public object Map(object source, object destination, Type sourceType, Type destinationType,
Action<IMappingOperationOptions> opts)
{
EnsureTypeMapPresence(sourceType, destinationType);

return Engine.Map(source, destination, sourceType, destinationType, opts);
}

private void EnsureTypeMapPresence(Type sourceType, Type destinationType)
{
BuildTypeMapIfMissing(ResolveType(sourceType), ResolveType(destinationType));
}

private static Type ResolveType(Type type)
{
if (type.IsArray) return type.GetElementType();

if (typeof (IEnumerable).IsAssignableFrom(type) && type.IsGenericType)
return type.GetGenericArguments().FirstOrDefault();

return type;
}

private void BuildTypeMapIfMissing(Type sourceType, Type destinationType)
{
if (ConfigurationProvider.FindTypeMapFor(sourceType, destinationType) == null)
Configuration.CreateMap(sourceType, destinationType);
Expand Down
2 changes: 1 addition & 1 deletion src/Patterns/SolutionAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
[assembly: ComVisible(false)]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: AssemblyVersion("3.10.0")]
[assembly: AssemblyInformationalVersion("3.10.0-beta4")]
[assembly: AssemblyInformationalVersion("3.10.0-beta5")]
2 changes: 1 addition & 1 deletion src/Patterns/SolutionAssemblyInfo.settings.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Settings.Initialize(
majorNumber: 3,
minorNumber: 10,
patchNumber: 0,
buildNumber: 4,
buildNumber: 5,
buildLevel: BuildLevels.Beta
);
#><#+
Expand Down
15 changes: 15 additions & 0 deletions src/_specs/Features/Mapping/MappingServices.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Mapping Services
In order to perform simple mapping operations
As a developer
I want to map from type to type without creating a map first, and without using dynamic mapping

Scenario: Automatic single type to single type mapping
Given I have a source object for mapping
When I map the object to a destination type
Then the original object should have the same values as the mapped object

Scenario: Automatic collection to collection mapping
Given I have a source object collection with 10 items for mapping
When I map the collection to another type
Then the mapped collection should have 10 items
And all of the original objects should have the same values as the mapped objects
106 changes: 106 additions & 0 deletions src/_specs/Features/Mapping/MappingServices.feature.cs

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

47 changes: 47 additions & 0 deletions src/_specs/Models/Mapping/MappingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#region FreeBSD

// Copyright (c) 2013, The Tribe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
// the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#endregion

using System.Collections.Generic;

using AutoMapper;

using Patterns.Mapping;

namespace Patterns.Specifications.Models.Mapping
{
public class MappingContext
{
public MappingContext()
{
MappingServices = new MappingServices(Mapper.Engine, Mapper.Configuration, Mapper.Configuration as IConfigurationProvider);
}

public IMappingServices MappingServices { get; private set; }
public MappingSource SingleSource { get; set; }
public MappingDestination SingleDestination { get; set; }
public IList<MappingSource> MultiSource { get; set; }
public IList<MappingDestination> MultiDestination { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/_specs/Models/Mapping/MappingDestination.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#region FreeBSD

// Copyright (c) 2013, The Tribe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
// the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#endregion

namespace Patterns.Specifications.Models.Mapping
{
public class MappingDestination
{
public string Id { get; set; }
public string Data { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/_specs/Models/Mapping/MappingSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#region FreeBSD

// Copyright (c) 2013, The Tribe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
// the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the
// following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#endregion

namespace Patterns.Specifications.Models.Mapping
{
public class MappingSource
{
public string Id { get; set; }
public string Data { get; set; }
}
}
Loading

0 comments on commit 215a60a

Please sign in to comment.