automapper

Automapper Set Decimals to all be 2 decimals

大憨熊 提交于 2019-12-07 22:34:52
问题 I want to use AutoMapper to link up two of my objects. It is working well but now I want to format my decimal items to all round to 2 decimals. This is what I have. What am I doing wrong? Mapper.CreateMap<Object1, Object2>() .ForMember(x => typeof(decimal), x => x.AddFormatter<RoundDecimalTwo>()); Here is the RoundDecimalTwo Formatter public class RoundDecimalTwo : IValueFormatter { public string FormatValue(ResolutionContext context) { return Math.Round((decimal)context.SourceValue,2)

Autofac, ASP.NET MVC 3 httpRequest scope and AutoMapper: No scope with a Tag matching 'httpRequest' is visible

你。 提交于 2019-12-07 21:43:15
问题 When I use a web type registered with autofac from an automapper mapping, I get this error: No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the

How to register a custom ObjectMapper with AutoMapper

巧了我就是萌 提交于 2019-12-07 21:37:03
问题 I am planning to create my own custom Object Mapper for a type using AutoMapper's IObjectMapper interface, but I don't see any place where we can register the implemented mapper with AutoMapper. Could anyone tell me how to register it. Edit: For more information on this please follow the discussion at AutoMapper-Users group 回答1: One way to go here is to replace the static registry function in the MapperRegistry class. Here's the current version: public static Func<IEnumerable<IObjectMapper>>

Mapping Domain Models to View Models

牧云@^-^@ 提交于 2019-12-07 17:35:52
问题 I'm starting from a point very similar to: Domain Entities, DTO, and View Models. The advised use of DTOs to map between the domain model and the MVC's ViewModel seems consistent expectations. I seek details of how to bridge the domain model (Entity Framework-based project) to the WebAPI mvc project. I'm starting with a project of simple POCOs (generated by EF PowerTools to reverse engineer my existent db) that I want to connect to an MVC4 WebAPI project. I expect I'll be adding business

Configuring AutoMapper to fulfil ITypeConverter<,> constructor dependecies with Autofac

巧了我就是萌 提交于 2019-12-07 16:10:44
问题 My first time working with Autofac to inject AutoMapper's IMapper interface into classes that have an object mapping requirement. I have made some progress, with a little help, getting the various dependencies added to AutoMapper's register using Assembly Scanning: builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()) .AsClosedTypesOf(typeof(ITypeConverter<,>)) .AsImplementedInterfaces(); builder.RegisterAssemblyTypes(typeof(AutoMapperExtensions).Assembly) .AssignableTo

Is it possible to tell automapper to ignore mapping at runtime?

女生的网名这么多〃 提交于 2019-12-07 15:41:11
问题 I'm using Entity Framework 6 and Automapper to map entities to dtos. I have this models public class PersonDto { public int Id { get; set; } public string Name { get; set; } public AddressDto Address { get; set; } } public class AddressDto { public int Id { get; set; } public string Street { get; set; } public string City { get; set; } } I use automapper Queryable Extension to map dto from entity. var personDto = dbContext.People.Project().To<PersonDto>(); The problem with above method is

Serializing a List of Interfaces using the DataContractSerializer

北城以北 提交于 2019-12-07 15:38:14
问题 I have a class and there are some nested classes within it. I serialize it and send it to the wcf service using a method with no problems. Here's the class; public class ComputerDTO { [DataMember] public ComputerTypeDTO Type { get; set; } [DataMember] public string ComputerName { get; set; } [DataMember] public MonitorDTO Monitor { get; set; } } Here's the method; public void Check() { Computer c = new Computer(); ISystemInfoOperations cli = GetServiceClient(); Mapper.CreateMap<Monitor,

How to map single object of type x to array of object of type y using automapper

时光怂恿深爱的人放手 提交于 2019-12-07 15:08:29
问题 Mapper.CreateMap<A, B>() .ForMember(dest => dest.defs, opt => opt.MapFrom(origin => origin.abc)); where defs is array of Def (Def[]) how to map? 回答1: Mapper.CreateMap<A, B>() .ForMember(dest => dest.defs, opt => opt.MapFrom(origin => new[]{ origin.abc })); destination property is array of Def and so the source requries array of something, that's how automapper understands... this works!!! 来源: https://stackoverflow.com/questions/2191768/how-to-map-single-object-of-type-x-to-array-of-object-of

AutoMapper - setting destination string to null actually makes it string.Empty

烈酒焚心 提交于 2019-12-07 13:28:56
问题 With the following mapping: Mapper.CreateMap<ObjectA, ObjectB>() .ForMember(dest => dest.SomeStringProperty, opt => opt.MapFrom(src => null)) SomeStringProperty is now empty string not null (as I would expect) Is this a bug? How can I get it to actually be null? I see that opt.Ignore() will make it null but I actually want to do a conditional null like the following and the above simplified bug(?) is preventing this Mapper.CreateMap<ObjectA, ObjectB>() .ForMember(dest => dest

Injecting a specific instance of an interface using Autofac

為{幸葍}努か 提交于 2019-12-07 12:48:18
问题 I have a controller and it receives a specific instance of an interface. The interface looks something like this: public interface IMyInterface { ... implementation goes here } And then I have some classes that implement this interface like this: public class MyClassA : IMyInterface { ... implementation goes here } public class MyClassB : IMyInterface { ... implementation goes here } In my ControllerA I have the following constructor: private ICustomerService customerService; private