automapper

Using DTO's with OData & Web API

若如初见. 提交于 2019-12-04 01:54:37
Using Web API and OData, I have a service which exposes Data Transfer Objects instead of the Entity Framework entities. I use AutoMapper to transform the EF Entities into their DTO counter parts using ProjectTo() : public class SalesOrdersController : ODataController { private DbContext _DbContext; public SalesOrdersController(DbContext context) { _DbContext = context; } [EnableQuery] public IQueryable<SalesOrderDto> Get(ODataQueryOptions<SalesOrderDto> queryOptions) { return _DbContext.SalesOrders.ProjectTo<SalesOrderDto>(AutoMapperConfig.Config); } [EnableQuery] public IQueryable

automapper - ignore mapping if property type is different with same property name - C#

若如初见. 提交于 2019-12-04 01:19:02
问题 How can I ignore mapping if property type is different with same property name? By default it's throwing error. Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>(); Model = Mapper.Map<EntityAttribute, LeadManagementService.LeadEntityAttribute>(EntityAttribute); I know a way to specify the property name to ignore but that's not what I want. .ForMember(d=>d.Field, m=>m.Ignore()); Because in the future I might add new properties. So i need to ignore mapping for all

Automapper - ignore all items of IEnumerable<SelectListItem>

£可爱£侵袭症+ 提交于 2019-12-04 01:13:20
Is there anyway of Automapper to ignore all properties of a certain type? We are trying to improve the quality of our code by validating the Automapper mappings but having to put an .Ignore() for all IEnumerable<SelectListItem> which are always manually created is creating friction and slowing down development. Any ideas? Possible Idea after creating mappings: var existingMaps = Mapper.GetAllTypeMaps(); foreach (var property in existingMaps) { foreach (var propertyInfo in property.DestinationType.GetProperties()) { if (propertyInfo.PropertyType == typeof(List<SelectListItem>) || propertyInfo

AutoMapper: create instance of destination type if source == null

孤者浪人 提交于 2019-12-04 00:15:42
Is it possible to configure AutoMapper to return a new instance of the destination type if the source object is null? Source source = null; Dest d1 = AutoMapper.Mapper.Map<Source, Dest>(source); // d1 == null // I'm looking for a way to configure AutoMapper to // eliminate this code: Dest d2 = AutoMapper.Mapper.Map<Source, Dest>(source) ?? new Dest(); Answering my own question (partially): AutoMapper has a configuration property named AllowNullDestinationValues which is set to true by default. By setting this to false , I get the behavior shown in the question, e.g: Mapper.Configuration

AutoMapper

↘锁芯ラ 提交于 2019-12-03 23:01:03
using AutoMapper; using System; using System.Collections.Generic; namespace ConsoleApp4 { class Program { static void Main(string[] args) { //第一步初始化 Mapper.Initialize(cfg => { cfg.CreateMap<WorldA, WorldB>(); }); //声明一个集合 List<WorldA> worldAs = new List<WorldA>(); //声明一个对象,并赋值 WorldA world = new WorldA() { id = 1, wewe = 1.ToString() }; //将对象加入集合内部 worldAs.Add(world); //开始搬运 var b = Mapper.Map<List<WorldA>, List<WorldB>>(worldAs); //输出搬运后的值 Console.WriteLine(b[0].id); Console.WriteLine("按任意键结束"); Console.ReadKey(); } } public class WorldA { public int id { get; set; } public string wewe { get;

AutoFixture: how to CreateAnonymous from a System.Type

左心房为你撑大大i 提交于 2019-12-03 22:40:08
I need to create an object from AutoFixture using nothing more than a System.Type. However, there doesn't appear to be an overload of CreateAnonymous() that simply takes a type. They all expect a compile time generic T. Is there a way to convert a System.Type to T? Edit with usage details: I'm using AutoMapper, which has a hook for injecting components to support complex mapping scenarios: void ConstructServicesUsing(System.Func<Type,object> constructor) As you can see from the signature, clients can register a Func which AutoMapper invokes anytime it needs an injected service (mostly

Automapper 3.0 - This type is not supported on this platform IMapperRegistry

我的梦境 提交于 2019-12-03 22:03:16
I updated my project to use Automapper 3.0.0 and now my TFS build is not succeeding. The error is the following: " ...System.PlatformNotSupportedException: System.PlatformNotSupportedException: This type is not supported on this platform IMapperRegistry. " Is there anyone that can help me resolve this issue. In the mean time, I am going to revert to previous version since that one seems to work fine. jni We had the same issue on our build server. MsTest seemed to remove DLLs it deemed unnecessary (note : this claim is only an educated guess). To fix it, add an explicit call to something in

How do I get AutoMapper to map this?

心不动则不痛 提交于 2019-12-03 21:44:21
Say I have this class: public class Account { public int AccountID { get; set; } public Enterprise Enterprise { get; set; } public List<User> UserList { get; set; } } When I use AutoMapper to map the Account class, I would also like it to map the Enterprise class, and the list of users (UserList) in the returned object. How can I get AutoMapper to do this? Thanks! AutoMapper does that out of-the-box if you provide a configuration for the Enterprise and User type. Configuration looks like this: Mapper.CreateMap<Account, AccountDto>(); Mapper.CreateMap<Enterprise, EnterpriseDto>(); Mapper

How to map EditModel to Command Messages?

你。 提交于 2019-12-03 21:21:28
Jimmy Bogard at Los Techies says that he maps his EditModel to Command Messages instead of mapping EditModel to DomainModel. Can anyone explain this further? I'd guess it'd be a version of the command pattern that performs the necessary manipulations on the relevant domain objects based on the supplied message. e.g. Something like public PromoteEmployeeCommand : ICommand { private readonly PromotionMessage _message; private readonly IEmployeeRepository _repository; public PromoteEmployeeCommand(PromotionMessage message, IEmployeeRepository repository) { _message = message; _repository =

Automapper 5.0 global configuration

人盡茶涼 提交于 2019-12-03 21:11:17
I am using the code below in AutoMapperConfig.cs in App_Start folder. I initialized it in Global.asax as AutoMapperConfiguration.Configure() But I am unable to use the Mapper.Map<Hospital, MongoHospital> in my controller. It is throwing an exception that no mappings are defined. It was working in previous versions of Automapper which were supporting Mapper.CreateMap<> methods. I am confused how to use MapperConfiguration instance. public static class AutoMapperConfiguration { public static void Configure() { Mapper.Initialize( cfg => { cfg.AddProfile<HospitalProfile>(); } ); Mapper