If I have the following class:
class SPUser
{
public int ID { get; set; }
public string Name { get; set; }
public string LoginName { get; set; }
public string Email { get; set; }
public bool IsSiteAdmin { get; set; }
public bool IsSiteAuditor { get; set; }
public bool IsDomainGroup { get; set; }
public List<SPGroup> Groups { get; set; }
}
And I am using the sharepoint web services, which return an XML with an attribute for each property on my class, such as:
<Users>
<User Name="name" Description="desc" ..... />
</Users>
Is there any way to use AutoMapper to map the XML fragment to an SPUser class instance?
Blog has been deleted - here's the Bing archive of the post by #DannyDouglass
Simplify Using Xml Data With AutoMapper and Linq-to-Xml
I recently ran into a scenario at work that required manually consuming several SOAP web services, which I’m sure you can imagine was rather monotonous. A co-worker (Seth Carney) and I tried a few different approaches, but we finally settled on a solution that simplified consumption of the xml and ultimately made the code more testable. That solution centered around leveraging AutoMapper, an open source object-object mapping tool, to create a link between the XElements(http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx) returned in the SOAP messages and custom contracts we created – in a reusable manner.
I put together a quick demo that shows how you could use the same approach to consume and display the Twitter Public Timeline (http://api.twitter.com/1/statuses/public_timeline.xml) (using the API’s Xml response type).
Note: The source code for the following example can be found on my GitHub page: https://github.com/DannyDouglass/AutoMapperXmlMappingDemo
Getting the Project Setup
After creating a basic MVC3 (download beta) project and the associated test project, the first step was to get the AutoMapper package installed. I have been using NuGet, Microsoft’s recently announced package management system, to install any open source dependencies. The following command was all that was needed to setup AutoMapper in my MVC3 project (read more about NuGet here(http://weblogs.asp.net/scottgu/archive/2010/10/06/announcing-nupack-asp-net-mvc-3-beta-and-webmatrix-beta-2.aspx) and here(http://weblogs.asp.net/scottgu/archive/2010/10/06/announcing-nupack-asp-net-mvc-3-beta-and-webmatrix-beta-2.aspx)):
PM> add-package AutoMapper
Creating the Mapping
With AutoMapper installed I’m ready to get started creating the components necessary for the xml-to-object mapping. The first step is creating a quick contract used in my application to represent the Tweet object:
public interface ITweetContract
{
ulong Id { get; set; }
string Name { get; set; }
string UserName { get; set; }
string Body { get; set; }
string ProfileImageUrl { get; set; }
string Created { get; set; }
}
Nothing crazy here – just a simple entity. These are all fields that are provided in the response from the Twitter API using a different name for some fields. In simple cases where the source and destination objects have the same name you can setup a map very quickly using this syntax:
Mapper.CreateMap<SourceObj, DestinationObj>();
However, AutoMapper does not support Xml by default I have to specify the fields that I will be mapping. Using the Fluent API in AutoMapper I’m able to chain my field mappings. Take a look at one example field mapped in my example – the tweet’s Body:
Mapper.CreateMap<XElement, ITweetContract>()
.ForMember(
dest => dest.Body,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("text")))
It may look complicated at first, but all that is really happening here is that we are providing details to AutoMapper on what value to use in my source object and how to map it to the destination object’s property. There is one particular line I would like to focus on in the above Body field mapping:
options => options.ResolveUsing<XElementResolver<ulong>>()
.FromMember(source => source.Element("id")))
The XElementResolver is a custom value resolver (http://automapper.codeplex.com/wikipage?title=Custom%20Value%20Resolvers) that Seth came up with to handle parsing the XmlElement source object to retrieve a strongly-typed value for use in the mapping. I’ll detail that more in a moment, but before we move on take a look at my full mapping:
Mapper.CreateMap<XElement, ITweetContract>()
.ForMember(
dest => dest.Id,
options => options.ResolveUsing<XElementResolver<ulong>>()
.FromMember(source => source.Element("id")))
.ForMember(
dest => dest.Name,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("user")
.Descendants("name").Single()))
.ForMember(
dest => dest.UserName,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("user")
.Descendants("screen_name").Single()))
.ForMember(
dest => dest.Body,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("text")))
.ForMember(
dest => dest.ProfileImageUrl,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("user")
.Descendants("profile_image_url").Single()))
.ForMember(
dest => dest.Created,
options => options.ResolveUsing<XElementResolver<string>>()
.FromMember(source => source.Element("created_at")));
The Generic XElementResolver
This custom value resolver is the real key that allowed these XElement-to-Contract maps to work in the original solution. I’ve reused this resolver in this example as we saw above. This was all that was necessary to create the custom resolver class:
public class XElementResolver<T> : ValueResolver<XElement, T>
{
protected override T ResolveCore(XElement source)
{
if (source == null || string.IsNullOrEmpty(source.Value))
return default(T);
return (T)Convert.ChangeType(source.Value, typeof(T));
}
}
This generic XElementResolver allows use to easily pass the type of the value retrieved in our mapping above. For example, the following syntax is used to strongly type the value retrieved from the XmlElement in the Id field’s .ForMember() declaration above:
ResolveUsing<XElementResolver<ulong>>()
With my mapping completely configured and instantiated, I’m ready to invoke the Twitter API and leverage AutoMapper to display that latest Public Timeline.
Putting the Pieces Together
I created a simple class responsible for retrieving the Twitter API response:
public class TwitterTimelineRetriever
{
private readonly XDocument _twitterTimelineXml;
public TwitterTimelineRetriever()
{
_twitterTimelineXml = XDocument
.Load("http://api.twitter.com/1/statuses/public_timeline.xml");
}
public IEnumerable<ITweetContract> GetPublicTimeline(int numberOfTweets)
{
var tweets = _twitterTimelineXml.Descendants("status")
.Take(numberOfTweets);
return tweets.Select(Mapper.Map<XElement, ITweetContract>).ToList();
}
}
The GetPublicTimeline method is a simple method returning, you guessed it, the Twitter Public Timeline by leveraging the map we created earlier:
return tweets.Select(Mapper.Map<XElement, ITweetContract>).ToList();
In my MVC3 site’s HomeController I can make a quick call to the retrieval method, requesting the last 10 results:
public class HomeController : Controller
{
private TwitterTimelineRetriever _twitterTimelineRetriever;
public ActionResult Index()
{
_twitterTimelineRetriever = new TwitterTimelineRetriever();
ViewModel.Message = "Twitter Public Timeline";
return View(_twitterTimelineRetriever.GetPublicTimeline(10));
}
}
And finally, after a little formatting in my View using the new Razor view engine from Microsoft, I have my public timeline displaying!
You need to check out XML serialization in .NET for this - that's the way to serialize an object to XML or deserialize it from XML.
Automapper can be used to set properties between two objects - it doesn't deal with XML at all.
More resources:
C# Tutorial - XML Serialization
Bit late in the day, but someone has used AutoMapper to map XML to a POCO rather than going down the XMLSerialization route. I found the following blog entry:-
Simplify Using Xml Data with AutoMapper and Linq-to-Xml
This is enough to get you started on implementing a generic custom resolver of your own if the example one is not enough.
EDIT: fixed link
EDIT: really fixed link
You can user XML deserialization for this purposes. In .NET we have XmlSerializer and DataContractSerializer
Related
TL;DR: Is there a way to create a sort of convention or prefix rule that would use the source or destination class names as prefix for some field mappings using AutoMapper 12.0?
I'm experimenting with AutoMapper 12.0 in a dotnet core 6 project and the entity classes use the entity name as prefix for their keys (eg.: Person class has a PersonId property, which is mapped to the primary key in DB), but the API side has DTOs that don't include those prefixes:
// Entity definition
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
// ...
}
// API response definition
public class PersonDetails
{
public int Id { get; set; }
public string Name { get; set; }
// ...
}
The way I configured my mappings to make it work was to manually define the mapping for the IDs using .ForMember(...), but that requires me to do that for every single entity and DTO combination:
public class EntityToViewModelMappingProfile : Profile
{
public EntityToViewModelMappingProfile() : base(nameof(EntityToViewModelMappingProfile))
{
CreateMap<Person, PersonDetails>()
.ForMember(p => p.Id, o => o.MapFrom(p => p.PersonId))
.ReverseMap();
// ...
// the same code for all of my entities :(
}
}
I'm looking for a way to avoid a bunch of boilerplate code and have a generic rule that would work for all of my entities without having to write custom mappings for all of them.
Even though I have custom mappings for other fields, that case is the one that seems to be a fit for a custom convention, but I couldn't find a way to implement one that would give me access to the destination and source type names to be able to use them as prefixes.
As Lucian Bargaoanu pointed out in the comments, there is a similar question for an earlier version of AutoMapper here and it suggests using:
Mapper.Initialize(exp =>
{
exp.CreateMap<User, UserDto>();
exp.ForAllMaps((typeMap, mappingExpression) =>
mappingExpression.ForMember("Id", o=>o.MapFrom(typeMap.SourceType.Name + "Id"))
);
});
AutoMapper 12.0 seems to not support ForAllMaps, from what I researched in their docs. Is there a way to achieve the same with 12.0?
Lucian Bargaoanu's comments lead me to a similar question for a previous version of AutoMapper, but the current version (12.0) doesn't expose that API through the regular public interface anymore. At least it gave me a hint on possible ways of achieving my problem, so I started looking for how to apply the same on newer versions.
By digging through the upgrade docs from past versions, I found that ForAllMaps was moved to the Internal API on version 11.0 and now requires importing AutoMapper.Internal namespace to have access to the .Internal() extension methods on config objects to access that method.
Here is my solution for the problem in AutoMapper 12.0 (possibly compatible with 11.0 as well):
using AutoMapper;
using AutoMapper.Internal; // <== this is important
namespace MyOwn.Solution.AutoMapper
{
public class DomainToAPIModelMappingProfile : Profile
{
public DomainToAPIModelMappingProfile() : base(nameof(DomainToAPIModelMappingProfile))
{
CreateMap<Domain.Entities.Person, APIModels.PersonDetails>();
CreateMap<Domain.Entities.Foo, APIModels.FooData>();
CreateMap<Domain.Entities.Bar, APIModels.BarData>();
// ...
// this is the solution
this.Internal().ForAllMaps((typeMap, mappingExpression) =>
mappingExpression.ForMember("Id", o => o.MapFrom(typeMap.SourceType.Name + "Id"))
);
}
}
}
I am facing issue for mapping for nested objects using Auto Mapper method
Can any one give suggestion to map Source and Target class using auto mapper
For example this is sample code.
Source:
Main Class:
public class EMP {
public int empid {get;set;}
public int Name {get;set;}
public EMPDetails Employeedata {get;set;}//Here is inner object in main class
public EmpContact EmpContactdetails {get;set;}//Here is other inner object in main class
}
public Class EMPDetails {
public String Name {get;set;}
public EmpAdd EmpAddress {get;set;}//Here is inner Object in one class
}
public class EmpAdd {
public int id {get;set;}
public int Street {get;set;}
}
public class EmpContact {
public int id {get;set;}
public int Mobile {get;set;}
}
Target:
Main Class:
public class EMPInfo {
public int id {get;set;}
public int EmpName {get;set;}
public EMPPerdata Employeedata {get;set;}//Here is inner object in main class
public EmpContact EmpContactdetails {get;set;}//Here is other inner object in main class
}
public class EMPPerdata {
public String FullName {get;set;}
public EmpAdressInfo EmpAddress {get;set;}//Here is inner Object in one class
}
public class EmpAdressInfo {
public int id {get;set;}
public int Street {get;set;}
}
public class EmpContact {
public int id {get;set;}
public int Mobile {get;set;}
}
Original Answer
You'll need to create maps for both the outer and inner classes.
Ex:
Mapper.CreateMap<Emp,EmpInfo>();
Mapper.CreateMap<EmpDetails,EMPPerdata>();
etc...
Then:
EmpInfo Dest = Mapper.Map<Emp, EmpInfo>(srcObj);
That should work.
Edit (because that's not a great answer)
So, just wanted to add on this edit to show a few AutoMapper best practices because if you follow those best practices it's a lot easier to use.
AutoMapper Profiles
When using Automapper, unless you are mapping really really simple objects, the best way to set up your mapping is through using profiles. To use those profiles make certain to add a using AutoMapper; to your program and then you can create a mapping profile like so:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<EMP, EMPInfo>();
}
}
Now, in any of the classes you want to use this profile you can use it like so:
Mapper.Initialize(cfg =>
{
cfg.AddProfile<MappingProfile>();
});
EMPInfo empInfo = Mapper.Map<EMP, EMPInfo>(emp);
Now... why would you ever go to all of the trouble to do this? You could have just as easily said Mapper.CreateMap<EMP, EMPInfo>(); Mapper.Map<EMP, EMPInfo(src); and it would be doing the same thing.
The benefits to doing it the way I stated above are three-fold.
Centralization of your mapping expressions. In my case, I usually have a single MappingProfiles class that holds all the Mapping profiles that I could use. This practice helps more to organize your code than anything else.
Reusability. Wherever in your program you need to use that mapping profile which will likely be more than just a single CreateMap() line you can just initialize that profile and then use it.
Unit Testing. Loading in profiles makes unit testing for AutoMapper really easy. And, unit testing will help you figure out exactly how to map nested objects.
Unit Testing AutoMapper
In your unit testing project (add one if you don't have it, it's a default project template in visual studio) you should have a class that holds the following unit test:
[TestMethod]
public void AutoMapperConfigIsValid()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<MappingProfile>();
// add all profiles you'll use in your project here
});
Mapper.AssertConfigurationIsValid();
}
Now, if I were to run that unit test using the classes you laid out above and the mapping profile that I put above it will give me the error:
Unmapped Properties:
id
EmpName
This isn't the full error, just the most important part. What's happening is... AutoMapper cannot figure out where the id and EmpName fields on the EMPInfo Type should be mapped from.
To resolve this error we need to add mapping for particular members to our profile. Our profile becomes:
CreateMap<Source.EMP, Target.EMPInfo>()
.ForMember(dest => dest.id, opt => opt.MapFrom(src => src.empid))
.ForMember(dest => dest.EmpName, opt => opt.MapFrom(src => src.Name));
We add that in... run the test again... get a new error.
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type TestStuff.Target.EmpContact.
Context:
Mapping from type TestStuff.Source.EmpContact to TestStuff.Target.EmpContact
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
This tells us that we need to add in a map for the nested members of the objects we are mapping.
To resolve this, we add the following map to the mapping profile:
CreateMap<Source.EmpContact, Target.EmpContact>();
Now what you need to do here, and this is sort of an arduous, but necessary, process, is keep running that configuration test and fixing the issues that it gives you until the test does not fail.
When all the issues are fixed, the mapping profile looks like this:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Source.EMP, Target.EMPInfo>()
.ForMember(dest => dest.id, opt => opt.MapFrom(src => src.empid))
.ForMember(dest => dest.EmpName, opt => opt.MapFrom(src => src.Name));
CreateMap<Source.EmpContact, Target.EmpContact>();
CreateMap<Source.EMPDetails, Target.EMPPerdata>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name));
CreateMap<Source.EmpAdd, Target.EmpAdressInfo>();
}
}
This mapping profile works great and will map your two objects fine.
Now, this map seem to be a lot of work for a little thing, but the benefits of doing it this way come with scale. If you are ever using AutoMapper in a large 'Adapter' type project, code structured this way is going to be easier to navigate, read and test.
Hopefully this helps a bit. Sorry if I went way too far at any point here but hopefully this answer can help anyone trying to use AutoMapper.
You can find documentation on some of the stuff I mentioned above in the AutoMapper documentation as well: https://github.com/AutoMapper/AutoMapper/wiki/Configuration
you will need to create maps for all your models
Mapper.CreateMap<EMP,EmpInfo>();
Mapper.CreateMap<EmpDetails,EMPPerdata>();
Mapper.CreateMap<EmpContact,EmpContact>();
Mapper.CreateMap<EmpAdd,EmpAdressInfo>();
then:
Mapper.Map<Emp, EmpInfo>(someEmp);
I'm trying to gather windows events that satisfy a particular query and serialize them to JSON so that I can import that collection into a MongoDB database. This is just me practicing and fooling around, but I got stuck at this point and would love to be able to move forward.
The problem:
When I use JsonConvert.SerializeObject(...), I certainly do get it to serialize something similar to the object in the sense that there are a few properties that survived:
public class EventSerializer
{
public static void Serialize(EventLogEntry eventLogEntry)
{
var traceWriter = new MemoryTraceWriter();
JsonConvert.SerializeObject(eventLogEntry, Formatting.Indented, new JsonSerializerSettings
{
TraceWriter = traceWriter
});
Console.WriteLine(traceWriter);
}
}
Output:
{
"DataBuffer": "<seemingly-random, long string>",
"LogName": "Application",
"MachineName": "<my machine's name>"
}
I can even deserialize the object and get what I expected back. But I really don't want the DataBuffer key-value pair in there, I want the contents of the EventLogEntry to be readable to so that I can parse that information out of MongoDB.
I've been looking through the documentation for Json.NET, and I'm wondering if I need to do something with creating my own Converter or something, but I'm having a little trouble understanding it all from here. There is also talk, in the documentation, of the ability to opt-in/opt-out of serializing particular properties/fields, but it isn't clear to me how one would do that to an object whose declaration/specification wasn't written by one's self.
Any and all help/suggestions are appreciated.
As far as I know, you cannot deserialize JSON into a new EventLogEntry object. One reason is that some the properties in EventLogEntry are read-only properties, and the getters for theses properties contain logic to calculate the values, i.e., they are not a simple getters that obtain the value from internally stored field.
I suggest that you create a special object to hold the data. Here is an example:
public class MyEventLogEntry
{
public string MachineName { get; set; }
public DateTime GeneratedTime { get; set; }
public string Message { get; set; }
public long InstanceId { get; set; }
}
You can put in this class the event properties that you are interested in. They can even have different names than the ones inside EventLogEntry.
You can use AutoMapper to easily convert from EventLogEntry to MyEventLogEntry. Or you can do it manually if you want.
Here is a code sample that uses AutoMapper:
AutoMapper.Mapper.CreateMap<EventLogEntry, MyEventLogEntry>()
.ForMember(dst => dst.InstanceId, opt => opt.MapFrom(src => src.InstanceId))
.ForMember(dst => dst.Message, opt => opt.MapFrom(src => src.Message))
.ForMember(dst => dst.GeneratedTime, opt => opt.MapFrom(src => src.TimeGenerated))
.ForMember(dst => dst.MachineName, opt => opt.MapFrom(src => src.MachineName));
EventLog log = new EventLog("Application");
EventLogEntry entry = log.Entries[0];
MyEventLogEntry my_entry = AutoMapper.Mapper.Map<EventLogEntry, MyEventLogEntry>(entry);
string json = JsonConvert.SerializeObject(my_entry);
MyEventLogEntry deserialized_entry = JsonConvert.DeserializeObject<MyEventLogEntry>(json);
Please note that the call to AutoMapper.Mapper.CreateMap needs to done only once (e.g. at the start of your application).
You can use MyEventLogEntry when you want to read/write from/to the database.
I have a simple mapping and it is working but it's not filling in Output.Details.
I am a bit confused, I think it maybe because I am using the source as "Task" for each one.
Mapper.CreateMap<Task, Output>();
Mapper.CreateMap<Task, Output.Details>().ForMember(
dest => dest.Item, opt => opt.MapFrom(src => src.Name));
As far as i know i have to create 2 maps, 1 for the object and 1 for object contained within.
Problem is the source for the OUTPUT and OUTPUT.DETAILS can be found in TASK
I tried delving into Details within the first map and specifying Mapfrom but it gives the following error which is why i must create 2 maps
must resolve to top-level member. Parameter name: lambdaExpression error
IList<Task> tempItems= GetItems();
IList<Output> items =
Mapper.Map<IList<Task>, IList<Output>>(tempItems);
The map works but my property "Item" availble in Output.Details is NULL
What am I doing wrong? Here is my Destination object.
It fills in Name no problem, but nothing inside DETAILS... they are left NULL.
Task is not my class, but I checked it and all values are there to be copied hence Tag has a value and is a STRING.
public class Output
{
public string Name { get; set; }
public Details Summary { get; private set; }
public class Details
{
public string Item{ get; set; }
}
public Output()
{
Summary = new Details();
}
}
EDIT
Here is an example of the Task class.
EDIT
They is a sample vs 2010 project here and it shows exactly the problem.
http://dl.dropbox.com/u/20103903/AutomapperNotWorking.zip
and here is an image showing the issue, as you can see Summary Item is "NULL" but it should contain the NAME from Task.
First off, always use Mapper.AssertConfigurationIsValid(); to make sure your mapping configuration is valid. I added it to your code and it immediately highlighted the problem: You didn't tell Automapper what to do with the Summary property. Since Task doesn't contain a property called Summary, Automapper needs to know what to do with it.
So the problem isn't really how to map a nested class, you just need to tell Automapper what to do with Summary. Here's the Automapper configuration that works for your example:
Mapper.CreateMap<Task, Output>()
.ForMember(d => d.Summary, o => o.MapFrom(t => new Output.Details {Item = t.Name}));
Mapper.AssertConfigurationIsValid();
That's all you need.
for the new version, it can be performed as follow:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<UdtDevQuestion, QuestionViewModel>();
});
config.AssertConfigurationIsValid();
In my ASP.NET MVC 2 (RC) project - I'm using AutoMapper to map between a Linq to Sql class (Media) and a view model (MediaVM). The view model has a SelectList property for a drop down in the view. I have a custom value resolver to populate the SelectList property items from the db, but am wondering if there's a way to pass a couple values from the source model into the resolver (using ConstructedBy method?) to a) define the selected item and b) filter the items from the db. The source object gets passed into the custom resolver - but the resolver is used on several different view models with different types of source objects, so would rather define where to get the values from in my mapping config. Here is my view model:
public class MediaVM
{
public bool Active { get; set; }
public string Name { get; set; }
[UIHint("DropDownList")]
[DisplayName("Users")]
public SelectList slUsers { get; private set; }
}
The automapper mapping config:
Mapper.CreateMap<Media, MediaVM>()
.ForMember(dest => dest.slUsers, opt => opt.ResolveUsing<UsersSelectListResolver>());
It would be nice to be able to do something like this on the .ForMember mapping clause:
.ConstructedBy(src => new UsersSelectListResolver(src.UserID, src.FilterVal))
Is there a way to accomplish this?
I like that idea as a feature request. You can do something like that right now, with MapFrom:
ForMember(dest => dest.slUsers, opt => opt.MapFrom(src => new UsersSelectListResolver(src).Resolve(src));
I found your posting trying to do the same thing. I decided on a simple approach and skip trying to map to my select list directly via AutoMaper. I simply return an array into my ViewModel and reference that object for my select list. The array gets mapped, select list object does not. Simple, effective. And, IMHO each is doing it's intended task - the mapper maps, the ViewModel does the layout
View Model code:
[DisplayName("Criterion Type")]
public virtual CriterionType[] CriterionTypes { get; set; }
[DisplayName("Criterion Type")]
public SelectList CriterionTypeList
{
get
{
return new SelectList(CriterionTypes, "Id", "Key");
}
}
my mapper:
Mapper.CreateMap<Criterion, CriterionForm>()
.ForMember(dest => dest.CriterionTypeList, opt => opt.Ignore());