automapper

Mapping flat JSON/Dictionary to model (containing sub classes)

筅森魡賤 提交于 2019-11-30 15:42:45
I want to turn a flat json string into a model, the destination class has subclasses, and the flat json has all of the sub class objects with prefix; like "{classname}.{property}". { "FirstName": "Joey", "LastName": "Billy", "EmploymentDetails.JobTitle": "JobTitle", "EmploymentDetails.StartDate": "2015-01-01T00:00:00", "ContactDetails.HouseNumberName": "10", "ContactDetails.Road": "Road" } This is my destination class: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public virtual EmploymentDetails EmploymentDetails { get;set;} public virtual

How to define AutoMapper mapping outside code i.e. in a XML file or use different approach for a fully configurable object mapping?

淺唱寂寞╮ 提交于 2019-11-30 15:34:05
EDIT: Originally I intended to use AutoMapper to achieve my goal, but I had to learn that AutoMapper is not intended to work that way. It gives you the possibility to create profiles but in my case (fully configurable) I would need for each parameter combination one profile, so I came up with an own approach, see answers. From the AutoMapper wiki I learned to create a simple mapping like Mapper.CreateMap<CalendarEvent, CalendarEventForm>().ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title)); Mapper.CreateMap<CalendarEvent, CalendarEventForm>().ForMember(dest => dest.EventDate,

DDD - how to rehydrate

强颜欢笑 提交于 2019-11-30 15:19:50
Question : what is the best, efficient and future proof way to rehydrate an aggregate from a repository? What are the pro's and con's of the provided ways and are my perceptions correct? Let's say we have an Aggregate Root with private setters but public getters for accessing state Behaviour is done through methods on the aggregate root. A repository is instructed to load an aggregate. At the moment I see a couple of possible ways to achieve this: set the state through reflection (manual or automatic eg. Automapper) make constructors that accept properties so state is set load the aggregate

Automapper - Bestpractice of mapping a many-to-many association into a flat object

落花浮王杯 提交于 2019-11-30 15:07:46
I have two entities: Employee and Team . What I want is an EmployeeForm that has the Name of the Team . How can I achieve this using AutoMapper ? My current "solution" is the following: Mapper.CreateMap<Employee, EmployeeForm>() .ForMember(dest => dest.TeamName, opt => opt.MapFrom(x => x.GetTeams().FirstOrDefault() != null ? string.Join(", ", x.GetTeams().Select(y=>y.Name)) : "n/a")); In my opinion this is bad readable. What I would like to have is a generic method where I can pass an entity, choosing the collection and saying if collection is null return a default value or otherwise choose

Automapper null properties

大兔子大兔子 提交于 2019-11-30 15:04:50
问题 I map my objects to dtos with Automapper. public class OrderItem : BaseDomain { public virtual Version Version { get; set; } public virtual int Quantity { get; set; } } [DataContract] [Serializable] public class OrderItemDTO { [DataMember] public int Id { get; set; } [DataMember] public string Guid { get; set; } [DataMember] public virtual int? VersionId { get; set; } [DataMember] public virtual string VersionName { get; set; } [DataMember] public virtual int Quantity { get; set; } } So when

Trying to use AutoMapper for model with child collections, getting null error in Asp.Net MVC 3

久未见 提交于 2019-11-30 14:11:17
问题 I'm completely new to AutoMapper, and I have a View that looks like this: @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) <fieldset> <legend>Consultant</legend> <div class="editor-label"> @Html.LabelFor(model => model.FirstName) </div> <div class="editor-field"> @Html.EditorFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </div> <div class="editor-label"> @Html.LabelFor(model =>

Confusion between DTOs (linq2sql) and Class objects!

泄露秘密 提交于 2019-11-30 14:10:42
i have been successfully working with linq2sql and the linq DTOs (the classes that are created by linq2sql) .... I am confused, i have the task of updating an old application and i can see that my DTOs will be used how they should be .... to transport date I am using the repository pattern so i am passing data from the repository to the service via the linq2sql dtos... once i am in the service layer (this is basically my business logic) then I need to pass around class objects .. these class objects are basicaly a mirror image (more or less) of the dtos - there are some changes in some place

Use AutoMapper to map from an interface to a concrete type

一曲冷凌霜 提交于 2019-11-30 14:05:51
I've created a dotNetFiddle that demonstrates the question here . Here's a simplified example of what I'm trying to do... let's say I have an the following interfaces: public interface IPerson { int Id { get; set; } } public interface IModelPerson : IPerson { int BeautyCompetitionsWon { get; set; } } In the real implementation, there are lots of different types of people (e.g. IUglyPerson , etc). These are the contracts for entity types, e.g. as follows: public class PersonEntity : IPerson { public int Id { get; set; } } public class ModelPersonEntity : PersonEntity, IModelPerson { public int

How do I use automapper to map a dataset with multiple tables

旧巷老猫 提交于 2019-11-30 13:59:55
DISCLAIMER: this is a copy paste from an older stackoverflow post that isn't available anymore, but I have exaclty the same problem, so it seemed appropriate to repost it as it was never answered. I have a stored procedure that will return 4 result sets (contacts, addresses, email, phones) which is populated into a dataset. I would like to use AutoMapper to populate a complex object. public class Contact { public Guid ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Address> Addresses { get; set; } public List<Phone> Phones { get;

Automapper null properties

流过昼夜 提交于 2019-11-30 13:12:40
I map my objects to dtos with Automapper. public class OrderItem : BaseDomain { public virtual Version Version { get; set; } public virtual int Quantity { get; set; } } [DataContract] [Serializable] public class OrderItemDTO { [DataMember] public int Id { get; set; } [DataMember] public string Guid { get; set; } [DataMember] public virtual int? VersionId { get; set; } [DataMember] public virtual string VersionName { get; set; } [DataMember] public virtual int Quantity { get; set; } } So when I have OrderItem with null version, i get an exception at: Mapper.Map<OrderItem, OrderItemDTO>(item)