automapper

Trying to add AutoMapper to Asp.net Core 2?

≡放荡痞女 提交于 2019-12-03 05:32:13
问题 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,

Do i need to create automapper createmap both ways?

放肆的年华 提交于 2019-12-03 05:15:44
This might be a stupid question! (n00b to AutoMapper and time-short!) I want to use AutoMapper to map from EF4 entities to ViewModel classes. 1) If I call CreateMap<ModelClass, ViewModelClass>() then do I also need to call CreateMap<ViewModelClass, ModelClass>() to perform the reverse? 2) If two classes have the same property names, then do I need a CreateMap statement at all, or is this just for "specific/custom" mappings? In AutoMapper you have a Source type and a Destination type. So you will be able to map between this Source type and Destination type only if you have a corresponding

AutoMapper: How to parse an Int from a String and possible to creating rules based on data type?

自作多情 提交于 2019-12-03 05:11:51
I have two models for my form, a ViewModel going to it, and a ControlModel coming from it. The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type. How would you code AutoMapper to convert a string field to integer? I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error). Mapper.CreateMap<SourceClass, DestinationClass>() .ForMember(dest => dest.myInteger, opt => opt.MapFrom(src => src.myString)); The types in the class and their corresponding conversion types: string to int, int?, double, double?,

Automapper in WebAPI Controller

和自甴很熟 提交于 2019-12-03 05:08:54
问题 I have a Car WebAPI controller method as below - note _carService.GetCarData returns a collection of CarDataDTO objects [HttpGet] [Route("api/Car/Retrieve/{carManufacturerID}/{year}")] public IEnumerable<CarData> RetrieveTest(int carManufacturerID, int year) { //Mapper.Map<> var cars = _carService.GetCarData(carManufacturerID, year); //var returnData = Mapper.Map<CarData, CarDataDTO>(); return cars; } CarData is a WebAPI model I have created. public class CarData { public string Model { get;

Globally apply value resolver with AutoMapper

限于喜欢 提交于 2019-12-03 05:05:05
I'm trying to have AutoMapper take care of localizing all DateTime properties on our view models for us. We use UTC everywhere in our system and store everything in UTC in the database, but we'd like to automatically convert that to a user's time zone for display. After looking at all the options, I settled on using a ValueResolver. Here's the gist of the resolver: public class LocalizedDateTimeFormatter : ValueResolver<DateTime, DateTime> { protected override DateTime ResolveCore(DateTime source) { // get company return company.TimeZone.ConvertFromUtc(source); } } I'm setting up the mapping

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

社会主义新天地 提交于 2019-12-03 03:56:24
问题 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

When using DTOs, Automapper & Nhibernate reflecting changes in child collections of DTO in domain object being updated

对着背影说爱祢 提交于 2019-12-03 03:55:41
I'm not massively familiar with this design but I am hoping to get some guidance. I have a backend service that sends out DTOs to a WPF smart client. On the WPF smart client the user will change,delete and modify items and then the changes are sent back (client --> server). As an example, currently I am working on the Customer details form and the user has the ability to add,remove and change categories belonging to a customer in a datagrid. When the DTO is sent back to the server I would like to load in the domain object that is related to the ID in the DTO and apply the changes made on the

automapper map dynamic object

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am working with Automapper and need to achieve the following mapping but not sure how it can be done. I want to map a Dictionary object to a dynamic object, so that the key is the property on the object and the value of the dictionary is the value of property in dynamic object. Can this be achieve with automapper and if so, how? 回答1: You can simply get Dictionary from ExpandoObject and fill it with original dictionary values void Main() { AutoMapper.Mapper.CreateMap<Dictionary<string, object>, dynamic>() .ConstructUsing

Map async result with automapper

和自甴很熟 提交于 2019-12-03 03:01:15
We are createing a Web.Api application of a angularjs application. The Web.Api returns a json result. Step one was getting the data: public List<DataItem>> GetData() { return Mapper.Map<List<DataItem>>(dataRepository.GetData()); } That worked like a charm. Then we made the data repo async and we changed to code to work with it. public List<DataItem>> GetData() { return Mapper.Map<List<DataItem>>(dataRepository.GetDataAsync().Result); } Stil no problems. Now we wanted to make my Web.Api full async awayt so we changed it to: public async Task<List<DataItem>> GetData() { return await Mapper.Map

AutoMapper 4.2 and Ninject 3.2

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm updating a project of mine to use AutoMapper 4.2, and I'm running into breaking changes. While I seem to have resolved said changes, I'm not entirely convinced I've done so in the most appropriate way. In the old code, I have a NinjectConfiguration , and an AutoMapperConfiguration class that are each loaded by WebActivator. In the new version the AutoMapperConfiguration drops out and I instead instance a MapperConfiguration directly in the NinjectConfiguration class where the bindings are happening, like so: private static void