diff --git a/Slapper.AutoMapper.Tests/MappingWithPropertyAttributesTests.cs b/Slapper.AutoMapper.Tests/MappingWithPropertyAttributesTests.cs new file mode 100644 index 0000000..42a8df0 --- /dev/null +++ b/Slapper.AutoMapper.Tests/MappingWithPropertyAttributesTests.cs @@ -0,0 +1,39 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Dynamic; +using System.Linq; +using System.Text; + +namespace Slapper.Tests +{ + [TestFixture] + public class MappingWithPropertyAttributesTests : TestBase + { + public class Person + { + [Slapper.AutoMapper.ColumnName("PersonId")] + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + } + + [Test] + public void Can_Add_An_ColumnId() + { + // Arrange + dynamic dynamicPerson = new ExpandoObject(); + dynamicPerson.PersonId = 1; + dynamicPerson.FirstName = "Bob"; + dynamicPerson.LastName = "Smith"; + + // Act + Slapper.AutoMapper.Configuration.AddIdentifier(typeof(Person), "PersonId"); + var person = Slapper.AutoMapper.MapDynamic(dynamicPerson) as Person; + + + // Assert + Assert.NotNull(person); + } + } +} diff --git a/Slapper.AutoMapper.Tests/Slapper.Tests.csproj b/Slapper.AutoMapper.Tests/Slapper.Tests.csproj index 78efd18..05ecd25 100644 --- a/Slapper.AutoMapper.Tests/Slapper.Tests.csproj +++ b/Slapper.AutoMapper.Tests/Slapper.Tests.csproj @@ -52,6 +52,7 @@ + diff --git a/Slapper.AutoMapper.sln b/Slapper.AutoMapper.sln index 2452800..7afc2aa 100644 --- a/Slapper.AutoMapper.sln +++ b/Slapper.AutoMapper.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2016 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slapper", "Slapper.AutoMapper\Slapper.csproj", "{A3A6ABBF-8C42-4945-BB8B-804123E45B2D}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slapper.Tests", "Slapper.AutoMapper.Tests\Slapper.Tests.csproj", "{DB57364B-6EE2-46C0-837E-B43F53CEEFA5}" @@ -23,4 +25,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4714146F-EB06-4313-BBE1-730EC2F49A71} + EndGlobalSection EndGlobal diff --git a/Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs b/Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs index 55a6a5c..bc7f4cf 100644 --- a/Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs +++ b/Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs @@ -243,7 +243,16 @@ public static Dictionary CreateFieldAndPropertyInfoDictionary(Ty foreach (var propertyInfo in properties) { - dictionary.Add(propertyInfo.Name, propertyInfo); + if(propertyInfo.GetCustomAttributes(typeof(ColumnName), false).Length > 0) + { + var propertyName = ((ColumnName)propertyInfo.GetCustomAttributes(typeof(ColumnName), false).First()).Name; + dictionary.Add(propertyName, propertyInfo); + } + else + { + dictionary.Add(propertyInfo.Name, propertyInfo); + } + } var fields = type.GetFields(); diff --git a/Slapper.AutoMapper/Slapper.AutoMapper.cs b/Slapper.AutoMapper/Slapper.AutoMapper.cs index 52feb4d..993cea1 100644 --- a/Slapper.AutoMapper/Slapper.AutoMapper.cs +++ b/Slapper.AutoMapper/Slapper.AutoMapper.cs @@ -46,11 +46,24 @@ public static partial class AutoMapper /// /// Attribute for specifying that a field or property is an identifier. /// - [AttributeUsage( AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false )] + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class Id : Attribute { } + /// + /// Attribute for specifying that a field or property is an identifier. + /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + public class ColumnName : Attribute + { + public string Name { get; private set; } + public ColumnName(string name) + { + Name = name; + } + } + #endregion Attributes #region Mapping @@ -66,7 +79,7 @@ public class Id : Attribute /// If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls. /// The type /// Exception that is thrown when the cannot be converted to an IDictionary of type string and object. - public static T MapDynamic( object dynamicObject, bool keepCache = true) + public static T MapDynamic(object dynamicObject, bool keepCache = true) { return (T)MapDynamic(typeof(T), dynamicObject, keepCache); } @@ -110,7 +123,7 @@ public static object MapDynamic(Type type, object dynamicObject, bool keepCache /// If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls. /// List of type /// Exception that is thrown when the cannot be converted to an IDictionary of type string and object. - public static IEnumerable MapDynamic( IEnumerable dynamicListOfProperties, bool keepCache = true) + public static IEnumerable MapDynamic(IEnumerable dynamicListOfProperties, bool keepCache = true) { return MapDynamic(typeof(T), dynamicListOfProperties, keepCache).Cast(); } @@ -152,7 +165,7 @@ public static IEnumerable MapDynamic(Type type, IEnumerable dyna /// List of property names and values /// If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls. /// The type - public static T Map( IDictionary listOfProperties, bool keepCache = true) + public static T Map(IDictionary listOfProperties, bool keepCache = true) { return (T)Map(typeof(T), listOfProperties, keepCache); } @@ -184,7 +197,7 @@ public static object Map(Type type, IDictionary listOfProperties /// List of property names and values /// If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls. /// List of type - public static IEnumerable Map( IEnumerable> listOfProperties, bool keepCache = true) + public static IEnumerable Map(IEnumerable> listOfProperties, bool keepCache = true) { return Map(typeof(T), listOfProperties, keepCache).Cast(); }