automapper

How to map View Model back to Domain Model in a POST action?

Every article found in the Internet on using ViewModels and utilizing Automapper gives the guidelines of the "Controller -> View" direction mapping. You take a domain model along with all Select Lists into one specialized ViewModel and pass it to the view. That's clear and fine. The view has a form, and eventually we are in the POST act...

Does AutoMapper support Linq?

I am very interested in Linq to SQL with Lazy load feature. And in my project I used AutoMapper to map DB Model to Domain Model (from DB_RoleInfo to DO_RoleInfo). In my repository code as below: public DO_RoleInfo SelectByKey(Guid Key) { return SelectAll().Where(x => x.Id == Key).SingleOrDefault(); } public IQue...

ASP.net MVC: Creating SelectList in the view or action?

Hey guys I'm just wondering where people are creating their SelectList - in the action or the view. I have seen examples of both and the one that makes the most sense to me is doing it in the action and have the view model have a property of type SelectList. On the other hand, I have seen examples where people have the view model h...

AutoMapper - Adding Custom Formatters

I am using AutoMapper 1.0 RTW and adding a couple of custom formatters: Mapper.AddFormatter<AlertTypeFormatter>(); Mapper.AddFormatter<DateStringFormatter>(); The destination types are both string and the formatters work individually. But put them together and only the first formatter gets called. In this case the "AlertTypeF...

AutoMapper - Mapping objects in a class library to objects in a silverlight class library

Hey all, I am trying to use automapper to map a list of objects in a class library to a list of objects in a silverlight class library. I am getting a security exception as AutoMapper is trying to reflect over the silverlight class library. Does anyone know of a way around this? Thanks. ...

Automapper - how to map to constructor parameters instead of property setters

In cases where my destination setters are private, I might want to map to the object using the destination object's constructor. How would you do this using Automapper? ...

AutoMapper with c# 2.0 syntax

I'm trying to create a custom mapping with AutoMapper, but I can't use 3.0 syntax with lambdas. How would one convert this 3.0 code into 2.0 ? Mapper.CreateMap<MyClass, MyDto>() .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CompanyName)) Edit: Since there was no better solution, we are now using vs2008 on one workstati...

AutoMapper: setup member name matching convention

I tried setting up a member name mapping convention so that the source members ending with a "Id" are mapped to destination members without Id. For example UserId -> User How does one do this? I tried using SourceMemberNameTransformer without success. Also tried using RecognizePostfixes(). this.SourceMemberNameTransformer = s => ...

Can Automapper map a complex source graph to a flat destination without prefixes in the destination properties and without custom mappings?

Is there a way to get Automapper to map a complex source graph like: public class Source { public string Name { get; set; } public SourceSub Sub { get; set; } } public class SourceSub { public string ValA { get; set; } public string ValB { get; set; } } to a flat destination that looks like: public class Dest { p...

ASP. NET MVC: Mapping Entities to View Model

I'm trying to clean up my action methods in an ASP.NET MVC project by making use of view models. Currently, my view models contain entities that might have relationships with other entities. For example, ContactViewModel class might have a Contact, which might have an Address, both of which are separate entities. To query for a list of C...

How can I map this parent-child relationship in AutoMapper?

I have parent and child objects that are derived from LINQ to SQL entities. I want to map these onto some domain-friendlier DTOs. My SQL entity classes look somewhat like this: public class SqlEntityParent { public int ParentId { get; set; } public string Name { get; set; } public EntitySet<SqlEntityChild> Children { get; ...

AutoMapper: Map Source property of concrete subclass to an EnumValue in Destination class

I would like to define a mapping (or even a TypeConverter/Resolver) for the following classes: Destination: public class Destination { public DestinationEnum EnumProperty { get; set; } public Destination() { EnumProperty = DestinationEnum.undefined; } } public enum Destination...

Fluent NHibernate automapper: skip an intermediate class w/ table-per-subclass

I'm using the Fluent NHibernate mapper with table-per-subclass to map the following structure: public abstract class A { // properties here } public abstract class B : A { // methods here } public class C : B { // properties here } My database only has tables to represent class A and class C. Class B exists only in my mo...

Is using DTO's and Entities breach of DRY principle ?

I was looking at a library called Automapper. I am having a few concerns with this: We dont want to expose our data model (GOOD). Why should the datamodel closely resemble your DB? using lightweight DTOs instead of your entities. (GOOD) Now I need to map my entities to these DTOs. Am i respecting the DRY principle?? ...

Automapping inheritance: How to add Discriminator convention for base class

By implementing ISubclassConvention, I can change the Discriminator Value for the subclasses in my class hierarchy. I'm now looking for a way to set the Discriminator Value for my base classes as well. Is there a way to change it with a convention override or do I have to add a manual mapping for my hierarchy? (The IClassConvention prov...

Asp.Net MVC + NHibernate + Widget Plugin Architecture

Updated: This is a re-write from my original question (or lack of), the main problem I am having at the moment is that when mapping my Widget domain models to the correct ViewModel I am having to do it like this which is definetly not the right solution... public ProfileWidgetViewModel MapFrom(ProfileWidget input, Account userAccount) ...

AutoMapper : Map a many-to-many association into a DTO (Is flattening of collection items supported?)

Hi! I have a many to many association between a Team and an Employee. public class Employee : Entity { public virtual string LastName { get; set; } public virtual string FirstName { get; set; } public virtual string EMail { get; set; } public virtual IList<LoanedItem> LoanedItems { get; private set; } public virtual...

Mapping list of one type, to another

For my objects, I am using Csla, which has a BrokenRulesCollection property. I would like to convert that to my own DTO which has StatusMessages property. I created my own resolver: public class BrokenRulesCollectionResolver : ValueResolver<Csla.Validation.BrokenRulesCollection, StatusMessageList> { protected override StatusMessag...

BestPractice: How to display a many-to-many association in a view in a clean way?

Hi! I am using NHibernate and have a many-to-many association between an Employee and a Team. Now I want to display all Employees with the name of its Team member. Possibility 1: using AutoMapper and create a DTO containing the Employee properties and the name of the Team (eager load the Team) display the DTO in the view Possibili...

How to use a resolver for collections in AutoMapper?

I want to map some of my domain objects back to ORM (LLBLGen) entities, for this I'm using AutoMapper. The domain objects contain collections, and the AutoMapper documentation states that we don't have to worry about them, just about the types they contain. This isn't always the case... When trying to map to the collections I'm getting ...