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

Creating Column Name attribute to bind property by a specific name. #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions Slapper.AutoMapper.Tests/MappingWithPropertyAttributesTests.cs
Original file line number Diff line number Diff line change
@@ -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<Person>(dynamicPerson) as Person;


// Assert
Assert.NotNull(person);
}
}
}
1 change: 1 addition & 0 deletions Slapper.AutoMapper.Tests/Slapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="MappingToGuidTests.cs" />
<Compile Include="MappingToNullableTypesTests.cs" />
<Compile Include="MappingToEnumTests.cs" />
<Compile Include="MappingWithPropertyAttributesTests.cs" />
<Compile Include="MapUniqueChildsIdTest.cs" />
<Compile Include="MatchingChildNameTests.cs" />
<Compile Include="NoIdentifierTests.cs" />
Expand Down
7 changes: 6 additions & 1 deletion Slapper.AutoMapper.sln
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -23,4 +25,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4714146F-EB06-4313-BBE1-730EC2F49A71}
EndGlobalSection
EndGlobal
11 changes: 10 additions & 1 deletion Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,16 @@ public static Dictionary<string, object> 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();
Expand Down
23 changes: 18 additions & 5 deletions Slapper.AutoMapper/Slapper.AutoMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ public static partial class AutoMapper
/// <summary>
/// Attribute for specifying that a field or property is an identifier.
/// </summary>
[AttributeUsage( AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false )]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class Id : Attribute
{
}

/// <summary>
/// Attribute for specifying that a field or property is an identifier.
/// </summary>
[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
Expand All @@ -66,7 +79,7 @@ public class Id : Attribute
/// <param name="keepCache">If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls.</param>
/// <returns>The type <typeparamref name="T"/></returns>
/// <exception cref="ArgumentException">Exception that is thrown when the <paramref name="dynamicObject"/> cannot be converted to an IDictionary of type string and object.</exception>
public static T MapDynamic<T>( object dynamicObject, bool keepCache = true)
public static T MapDynamic<T>(object dynamicObject, bool keepCache = true)
{
return (T)MapDynamic(typeof(T), dynamicObject, keepCache);
}
Expand Down Expand Up @@ -110,7 +123,7 @@ public static object MapDynamic(Type type, object dynamicObject, bool keepCache
/// <param name="keepCache">If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls.</param>
/// <returns>List of type <typeparamref name="T"/></returns>
/// <exception cref="ArgumentException">Exception that is thrown when the <paramref name="dynamicListOfProperties"/> cannot be converted to an IDictionary of type string and object.</exception>
public static IEnumerable<T> MapDynamic<T>( IEnumerable<object> dynamicListOfProperties, bool keepCache = true)
public static IEnumerable<T> MapDynamic<T>(IEnumerable<object> dynamicListOfProperties, bool keepCache = true)
{
return MapDynamic(typeof(T), dynamicListOfProperties, keepCache).Cast<T>();
}
Expand Down Expand Up @@ -152,7 +165,7 @@ public static IEnumerable<object> MapDynamic(Type type, IEnumerable<object> dyna
/// <param name="listOfProperties">List of property names and values</param>
/// <param name="keepCache">If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls.</param>
/// <returns>The type <typeparamref name="T"/></returns>
public static T Map<T>( IDictionary<string, object> listOfProperties, bool keepCache = true)
public static T Map<T>(IDictionary<string, object> listOfProperties, bool keepCache = true)
{
return (T)Map(typeof(T), listOfProperties, keepCache);
}
Expand Down Expand Up @@ -184,7 +197,7 @@ public static object Map(Type type, IDictionary<string, object> listOfProperties
/// <param name="listOfProperties">List of property names and values</param>
/// <param name="keepCache">If false, clears instance cache after mapping is completed. Defaults to true, meaning instances are kept between calls.</param>
/// <returns>List of type <typeparamref name="T"/></returns>
public static IEnumerable<T> Map<T>( IEnumerable<IDictionary<string, object>> listOfProperties, bool keepCache = true)
public static IEnumerable<T> Map<T>(IEnumerable<IDictionary<string, object>> listOfProperties, bool keepCache = true)
{
return Map(typeof(T), listOfProperties, keepCache).Cast<T>();
}
Expand Down