schneidenbach / automapper.attributes Goto Github PK
View Code? Open in Web Editor NEWA convenient way to create AutoMapper type mappings using attributes.
License: MIT License
A convenient way to create AutoMapper type mappings using attributes.
License: MIT License
I'm trying to map a List
of Users
to a List
of Contact
.
Is there anything special that needs to happen for this to work?
The attempted mapping...
var contacts = AutoMapper.Mapper.Map<List<Users>>(userList);
The target class...
using AutoMapper.Attributes;
using NS.Models.Context;
namespace NS.Models
{
[MapsFrom(typeof(Users))]
public class Contact
{
[MapsFromProperty(typeof(Users), "DepartmentObj.Department")]
public string department { get; set; }
[MapsFromProperty(typeof(Users), "Email")]
public string emailAddress { get; set; }
[MapsFromProperty(typeof(Users), "First")]
public string firstName { get; set; }
[MapsFromProperty(typeof(Users), "Last")]
public string lastName { get; set; }
[MapsFromProperty(typeof(Users), "Cellnumber")]
public string telephoneMobile { get; set; }
[MapsFromProperty(typeof(Users), "Extension")]
public string telephoneOffice { get; set; }
[IgnoreMapFrom(typeof(Users))]
public Photo photo { get; set; }
}
}
contacts
ends up equaling userList
seemingly without modification.
Branch: https://github.com/schneidenbach/AutoMapper.Attributes/tree/pr/1
Unit test SubclassTests.EmployeeCodeIsMappedFromMapsToPropertyAttribute is failing in the V5 tests, passes in V4.
MapTypes() extension method doesn't work at all when using v.5 of AutoMapper.
I'm using a newer (5.2.0) version of Automapper.dll, but looks 5.0.2 version is required.
Hi,
Thanks for all your works but sadly, i've come to notice that this nuget package does not support .NetStandard v1.6. Are there any plans to support it?
Thank you in advance.
I have two classes inheriting from the same interface. Both contain an property of list with shared interface type. To use AutoMapper I need to explicitly tell it to use a different target type, instead of the shared inherited interface created by the source type.
Let me explain better with some code:
interface IFace {
String Prop { get; set; }
IEnumerable<IListItemFace> List { get; set; }
}
class Source : IFace {
String Prop { get; set; }
IEnumerable<IListItemFace> List { get; set; }
}
[MapsFrom(typeof(IFace))]
class Target : IFace {
String Prop { get; set; }
[MapsFromProperty(typeof(IEnumerable<ListItemTarget>), "List")]
IEnumerable<IListItemFace> List { get; set; }
}
interface IListItemFace {
String Prop2 { get; set; }
}
class ListItemSource : IListItemFace {
String Prop2 { get; set; }
}
[MapsFrom(typeof(IListItemFace))]
class ListItemTarget : IListItemFace {
String Prop2 { get; set; }
}
Note: I don't have access to Source
& ListItemSource
or their interfaces.
Now I call AutoMapper with:
IFace x = new Source { Prop = "Hoi", List = new List<ListItemSource> };
Target y = Mapper.Map<Target>(x);
IEnumerable<ListItemTarget> z = y.List; // <-- this will throw, as it can't convert source type to target type.
I hoped using MapsFromPropertyAttribute
on Target.List
solved this issue, but I need to explicitly set the target type.
So, now my question; would it possible to create an overload to MapsFromProperty
with an target type variable.
P.s. The following solution did solved my problem, but it requires all properties with the same structure to be mapped manually:
public class MapsFromTarget : MapsFromAttribute {
public MapsFromTarget() : base(typeof(IFace)) { }
public void ConfigureMapping(IMappingExpression<IFace, Target> mappingExpression) {
mappingExpression.ForMember(
destinationMember => destinationMember.List,
memberOptions => memberOptions.MapFrom(
sourceMember => Mapper.Map<IEnumerable<Target>>(sourceMember.List)));
}
}
using nuget package with plain framework 4.5 doesn't work, looks dll dargets 452 anyway.
this is true for 5.0.1 (single dll targets 4.5) but also for previous 5.0.0 (both dll targets 4.5.2)
PasteBin: https://pastebin.com/nGYL300T
The repro above shows a weird bug in the system:
When we have class B inherited from class A, and both are decorated with independent MapsFrom
attributes with appropriate ConfigureMapping
methods, the B's class mappings are ignored for some reason.
The method ConfigureMapping
on the B's attribute is actually ran, but is ignored and the Mapper.Map produces a result which we would expect to be when the ConfigureMapping
method would not exist at all - so the custom mappings are gone.
If we remove the custom MapsFrom
attribute from the A class, however, the bug does not appear.
If we manually set the custom mapping to B (via Mapper.CreateMap), the bug does not appear.
AutoMapper v.4.2.1 to v4.x should use the V4 DLL
AutoMapper v.5.x - ? should use the V5 DLL
I've reported the full details here: https://stackoverflow.com/questions/50743285/automapper-mapsto-attribute-does-not-work-on-referenced-classes/
The following code does not work on a Console app target framework .NET Core 2.0
public static void Main()
{
AutoMapper.Mapper.Initialize(config => {
typeof(Program).Assembly.MapTypes(config);
});
var person = new Person { Notes = "these are some notes" };
var customer = AutoMapper.Mapper.Map<Customer>(person);
Console.WriteLine(customer.MyCustomerNotes);
Console.ReadLine();
// Output: these are some notes
}
[MapsTo(typeof(Customer))]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
[MapsToProperty(typeof(Customer), "MyCustomerNotes")]
public string Notes { get; set; }
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MyCustomerNotes { get; set; }
}
We currently fix it but changing the target framework to netcoreapp2.0
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
That'd be great if we have that core2 compatibility in the official release.
Can you please release to NuGet with strong name signed assemblies.
Hi,
Fantastic package :) One issue though, the ReverseMap doesn't work as explained in the documentation.
It states :
"You can set the ReverseMap property in the [MapsTo]/[MapsFrom] attribute to true if you want to map the classes in reverse as well, like this:
[MapsTo(typeof(Customer), true)] //this will map Customer to Person and Person to Customer"
But since ReverseMap is not an argument of the MapsToAttribute constructor, this doesn't work. Can be circumenvented by setting it via a named parameter, ie.:
[MapsTo(typeof(Customer), ReverseMap = true)]
Maybe add an optional parameter to the constructor ?
Kind regards,
Sven.
Hi,
How can I achieve the same functionality as open generic with convert using from the Automapper.Attributes?
Thanks,
Attiqe
It'd be great if AutoMapper.Attributes supported mapping using Lambda expressions like regular AutoMapper does. Mapping using Attributes appears to break down when I get into mapping values from structs attached to properties, whereas the regular AutoMapper works fine with this when using Lambda expressions.
Trying to use this with a .NET Core 2 project, however, the examples do not work. I'm currently working on a solution, but figured i'd put this on here to see if you have any insight while I work on it.
Hi,
I have added AutoMapper.Attributes using NuGet package and when building I am getting following error:
The type or namespace name 'MapsToProperty' could not be found (are you missing a using directive or an assembly reference?)
I can see the AutoMapper.Attributes in bin folder.
I am using .Net 4.5.1
Please help.
Regards,
Dilhan
The following test correctly fails:
public class Foo
{
public string Bar { get; set; }
}
[MapsFrom(typeof(Foo))]
public class Qux
{
public string Bar { get; set; }
public string Baz { get; set; }
}
public class IgnoredPropertyTests
{
[Fact]
public void IgnoredPropertyYieldsValidMapping()
{
var config = new MapperConfiguration(cfg =>
{
typeof(Foo).GetTypeInfo().Assembly.MapTypes(cfg);
});
config.AssertConfigurationIsValid();
}
}
To fix the test, I can add [IgnoreMap]
to Baz
. However, I cannot add [IgnoreMapFrom(typeof(Foo))]
- despite that attribute, the mapping validation still fails. Is that the intended behavior, or a bug?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.