automapper

Automapper: map child property with existing mapper using instance API

家住魔仙堡 提交于 2019-12-02 20:33:01
问题 I'm trying to map one complex object to another using instance API: var config = new MapperConfiguration(cfg => { cfg.CreateMap<Student, PersonType>(); cfg.CreateMap<Professor, PersonType>(); cfg.CreateMap<Branch, BranchType>() .ForMember(x => x.Departments, opt => opt.MapFrom(src => new DepartmentType[] { new DepartmentType { Students = Mapper.Map<Student[], PersonType[]> (src.Students), Professors = Mapper.Map<Professor[], PersonType[]> (src.Professors), Name = src.DepartmentName } }))

Configure AutoMapper to map to concrete types but allow Interfaces in the definition of my class

不问归期 提交于 2019-12-02 20:32:55
I have some code that is similar to what is below. Basically it represents getting data from a web service and converting it into client side objects. void Main() { Mapper.CreateMap<SomethingFromWebService, Something>(); Mapper.CreateMap<HasSomethingFromWebService, HasSomething>(); // Service side var hasSomethingFromWeb = new HasSomethingFromWebService(); hasSomethingFromWeb.Something = new SomethingFromWebService { Name = "Whilly B. Goode" }; // Client Side HasSomething hasSomething=Mapper.Map<HasSomething>(hasSomethingFromWeb); } // Client side objects public interface ISomething { string

Trying to add AutoMapper to Asp.net Core 2?

☆樱花仙子☆ 提交于 2019-12-02 19:56:32
I worked on a asp.net core 1.1 project a while ago and use in projetc AutoMapper. in asp.net core 1.1, I add services.AddAutoMapper() in startup file : StartUp file in asp.net core 1.1: public void ConfigureServices(IServiceCollection services) { //Some Code services.AddMvc(); services.AddAutoMapper(); } And I use AutoMapper in Controller easily. Controller : public async Task<IActionResult> AddEditBook(AddEditBookViewModel model) { Book bookmodel = AutoMapper.Mapper.Map<AddEditBookViewModel, Book>(model); context.books.Add(bookmodel); context.SaveChanges(); } And everything was fine. But I'm

Where is the best place to map from view model to domain model?

假如想象 提交于 2019-12-02 17:41:52
Where is the best place to do mappings from view model to domain model? By mappings I mean from my EditGrantApplicationViewModel to a GrantApplication object. Lets say that I have the following action method (partial code): [HttpPost] public ActionResult Create(EditGrantApplicationViewModel editGrantApplicationViewModel) { if (!ModelState.IsValid) { return View("Create", editGrantApplicationViewModel); } return View("Index"); } Do I need to pass editGrantApplicationViewModel to a service layer method and do the mappings in the method? You should not place any of your mapping logic inside the

After using Automapper to map a ViewModel how and what should I test?

时光毁灭记忆、已成空白 提交于 2019-12-02 17:19:51
I am attempting to test the Index action of a controller. The action uses AutoMapper to map a domain Customer object to a view model TestCustomerForm . While this works I am concerned about the best way to test the results that I am receiving from the Index action. The controller's index action looks like this: public ActionResult Index() { TestCustomerForm cust = Mapper.Map<Customer, TestCustomerForm>(_repository.GetCustomerByLogin(CurrentUserLoginName)); return View(cust); } And its TestMethod looks like this: [TestMethod] public void IndexShouldReturnCustomerWithMachines() { // arrange var

How to scan and auto-configure profiles in AutoMapper?

浪子不回头ぞ 提交于 2019-12-02 17:12:15
Is there any way to auto-configue Automapper to scan for all profiles in namespace/assembly? What I would like to do is to add mapping profiles to AutoMapper from given assembly filtered by given interface, something like Scan Conventions in StructureMap: public static void Configure() { ObjectFactory.Initialize(x => { // Scan Assembly x.Scan( scanner => { scanner.TheCallingAssembly(); scanner.Convention<MyCustomConvention>(); scanner.WithDefaultConventions(); }); // Add Registries x.AddRegistry(new SomeRegistry()); }); Debug.WriteLine(ObjectFactory.WhatDoIHave()); } public class

Automapper IEnumerable within class is not being mapped to RepeatedField

拈花ヽ惹草 提交于 2019-12-02 15:34:19
问题 I want to map between two classes: public class A { public IEnumerable<C> someList } and public class B { public RepeatedField<D> someList } where RepeatedField is a class from Google.Protobuf.Collections that handles gRPC data. EDIT: As it turns out, the way that gRPC creates classes via its prototype is not exactly like creating a class like B. See my answer. I create an Automapper MappingConfiguration like this return new MapperConfiguration(cfg => { cfg.CreateMap<C, D>().ReverseMap(); cfg

Entity Framework + AutoMapper ( Entity to DTO and DTO to Entity )

那年仲夏 提交于 2019-12-02 14:56:13
I've got some problems using EF with AutoMapper. =/ for example : I've got 2 related entities ( Customers and Orders ) and they're DTO classes : class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList< OrderDTO > Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap< Customers, CustomerDTO >(); Mapper.CreateMap< Orders, OrderDTO >(); CustomerDTO custDTO =

Automapper - Dictionary to Object Mapping not working?

大兔子大兔子 提交于 2019-12-02 14:24:01
I'm trying to convert a dictionary to an object. I've tried the following but it doesnt work: public class FormField { public string FieldName { get; set;} public string FieldValue { get; set;} } var formData = new List<FormField> { new FormField {FieldName = "date", FieldValue = "2017-09-14"}, new FormField {FieldName = "name", FieldValue = "Job blogs"}, new FormField {FieldName = "isenabled", FieldValue = "true"} }; public class MyViewModel { [Required] public DateTime Date { get; set; } = DateTime.now; [Required] public string Name { get; set; } public boolean IsEnabled { get; set; } public

.NET CORE AutoMapper使用

早过忘川 提交于 2019-12-02 12:54:40
1、通过nuget安装AutoMapper,版本是7.0.1, 安装AutoMapper.Extensions.Microsoft.DependencyInjection 版本是4.0.1 不是以上版本的容易报错 2、startup注入automapper services.AddAutoMapper(); 3、创建profile文件, public class DingDingProfile:Profile { public DingDingProfile() { CreateMap<OapiProcessListbyuseridResponse.ProcessTopVoDomain, ProgressTemplate>(); } } 4、注入使用映射 5、映射添加到数据库 List<ProgressTemplate> progressTemplates = Mapper.Map<List<ProgressTemplate>>(processTopVoDomains); db.ProgressTemplates.AddRange(progressTemplates); db.SaveChanges(); 来源: https://www.cnblogs.com/topguntopgun/p/11745830.html