automapper

Automapper: Auto map collection property for a dto object

会有一股神秘感。 提交于 2019-12-30 05:11:05
问题 I have a domain object public class ProductModel { public long Id {get;set;} public string Name {get;set;} public string SerialNumber {get;set;} } Single Dto class: public class ProductDto { public long Id {get;set;} public string Name {get;set;} public string SerialNumber {get;set;} } Single Dto class that is a list of Dto object: public class ProductListDto : List<ProductDto> { public List<ProductDto> Products; public ProductListDto() { Products = new List<ProductDto>(); } } And I'd like to

Use AutoMapper to map from an interface to a concrete type

狂风中的少年 提交于 2019-12-30 04:37:04
问题 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 {

Automapper copy List to List

…衆ロ難τιáo~ 提交于 2019-12-29 12:13:50
问题 I have these classes : public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } I defined this : Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore()); That's work for this : PersonView personView = Mapper.Map<Person,

Automapper copy List to List

北城以北 提交于 2019-12-29 12:12:30
问题 I have these classes : public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } I defined this : Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore()); That's work for this : PersonView personView = Mapper.Map<Person,

How to use Automapper to create complex viewmodel objects from a simple object and a method call

陌路散爱 提交于 2019-12-29 09:28:22
问题 My application needs to display a table of customer data, including data about the customer and about his most recent order from a given warehouse. The customer domain object contains a GetLatestOrder(warehouseId) method. I have a CustomerView viewmodel and want to be able to populate it with some fields from the customer object, and a few fields from the latest order object. Can I do this using Automapper? 回答1: Try this, In Global.asax.cs [ Application_Start ], public static void

ViewModels with asp.net mvc 4 and EntityFramework whats the Point

孤街浪徒 提交于 2019-12-29 07:51:52
问题 I'm debating with myself whats the point in creating ViewModel classes in a project which uses Entity Framework? I currently have a project which uses EntityFramework. My solution is structured basically like this: UI project (contains controllers and Views) Model project (contains EntityFramework model) Services project (contains service classes which talk to Model Project to serve up the entities out of the model project to the UI project) My controllers pass the entities that Entity

Configuring Automapper in Bootstrapper violates Open-Closed Principle?

我们两清 提交于 2019-12-29 02:23:05
问题 I am configuring Automapper in the Bootstrapper and I call the Bootstrap() in the Application_Start() , and I've been told that this is wrong because I have to modify my Bootstrapper class each time I have to add a new mapping, so I am violating the Open-Closed Principle. How do you think, do I really violate this principle? public static class Bootstrapper { public static void BootStrap() { ModelBinders.Binders.DefaultBinder = new MyModelBinder(); InputBuilder.BootStrap();

Convert DataRow to Object with AutoMapper

余生长醉 提交于 2019-12-29 01:39:07
问题 I can successfully map from IDataReader to a List of objects but when I want to take one DataRow it doesn't seem to work as expected. Am I missing something simple here? [TestFixture] public class AutomapperTest { [Test] public void TestMethod1() { DataTable dt = new DataTable("contact"); dt.Columns.Add("FirstName"); dt.Columns.Add("LastName"); dt.Columns.Add("Line1"); dt.Columns.Add("Line2"); dt.Columns.Add("Line3"); dt.Columns.Add("Suburb"); dt.Columns.Add("State"); dt.Columns.Add("Postcode

Deep level mapping using Automapper

巧了我就是萌 提交于 2019-12-28 16:30:36
问题 I am trying to map objects with multi-level members: these are the classes: public class Father { public int Id { get; set; } public Son Son { get; set; } } public class FatherModel { public int Id { get; set; } public int SonId { get; set; } } public class Son { public int Id { get; set; } } This is how I try automap it: AutoMapper.Mapper.CreateMap<FatherModel , Father>() .ForMember(dest => dest.Son.Id, opt => opt.MapFrom(src => src.SonId)); this is the exception that I get: Expression 'dest

How to configure Automapper to automatically ignore properties with ReadOnly attribute?

守給你的承諾、 提交于 2019-12-28 14:06:15
问题 Context: Let's say I have the following "destination" class: public class Destination { public String WritableProperty { get; set; } public String ReadOnlyProperty { get; set; } } and a "source" class with the ReadOnly attribute on one of it's properties: public class Source { public String WritableProperty { get; set; } [ReadOnly(true)] public String ReadOnlyProperty { get; set; } } It's obvious, but to be clear: I am going to map from Source class to Destination class in the following way: