Let's say, we have two source types for mapping, and one destination:
class TSource
{
public float A { get; set; }
public float B { get; set; }
}
class TSupport
{
public float C { get; set; }
public float D { get; set; }
}
class TDestination
{
public float A { get; set; }
public float B { get; set; }
public float C { get; set; }
public float D { get; set; }
}
And instance of TSupport is obtained with some method, using TSource instance:
private TSupport GetSupportInstance(TSource source)
So, usually, I'd do this with double mapping:
Mapper.CreateMap<TSource, TDestination>();
Mapper.CreateMap<TSupport, TDestination>();
//...
var support = GetSupportInstance(source);
var destination = Mapper.Map<TSource, TDestination>(source);
Mapper.Map(support, destination);
This also may be done in single mapping using manual resolve:
Mapper.CreateMap<TSource, TDestination>()
.ForMember(desc => desc.C, opt => opt.ResolveUsing(src =>
{
var support = GetSupportInstance(src);
return support.C;
}))
.ForMember(desc => desc.D, opt => opt.ResolveUsing(src =>
{
var support = GetSupportInstance(src);
return support.D;
}));
As you can see, I'm using GetSupportInstance twice, for each property. So, finally, my question is - is it possible to obtain support in mapping once, store it somehow, and reuse it in mappings for different properties?
How about this:
Mapper.CreateMap<TSource, TDestination>()
.ForMember(d => d.C, x => x.Ignore())
.ForMember(d => d.D, x => x.Ignore())
.AfterMap((src, dst) => {
var support = GetSupportInstance(src);
dst.C = support.C;
dst.D = support.D;
});
I'd probably go about using a custom resolver that caches the instance you need. It would look something like this.
public class MySupportInstanceResolver : ValueResolver<TSource, TDestination>
{
private static readonly ISupportInstance _supportInstance;
[ImportingConstructor]
public MySupportInstanceResolver ()
{
_supportInstance=GetSupportInstance();
}
protected override string ResolveCore(Journal source)
{
return _supportInstance;
}
}
Then you could use it as:
cfg.CreateMap<TSource, Tdest>()
.ForMember(dest => dest.C,
opt => opt.ResolveUsing<MySupportInstanceResolver>().FromMember(name => name.C));
});
Related
I want map Origin.CityId and Origin.StateId properties of
Itinerary class to OriginCityId and OriginStateId properties
of ItineraryModel class.
Ex: Itinerary itinerary = Mapper.Map<Itinerary>(ItineraryModel);
My ViewModel
public class ItineraryModel : BaseModel
{
public int OriginCityId { get; set; }
public int OriginStateId { get; set; }
public bool Published { get; set; }
}
My Entity
public class Itinerary : BaseEntity
{
public City Origin { get; set; }
public bool Published { get; set; }
}
My mapping that tried do
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ItineraryModel, Itinerary>()
.ForPath(x => x.Origin.CityId, opt => opt.MapFrom(src => src.OriginCityId))
.ForPath(x => x.Origin.StateId, opt => opt.MapFrom(src => src.OriginStateId))
.ReverseMap();
}
}
I would like to too .ReverseMap() but can't find right syntax.
You need to add two mappings for mapping to Itenerary
CreateMap<ItineraryModel, City>()
.ForMember(city => city.CityId, expression => expression.MapFrom(itineraryModel => itineraryModel.OriginCityId))
.ForMember(city => city.StateId, expression => expression.MapFrom(itineraryModel => itineraryModel.OriginStateId));
CreateMap<ItineraryModel, Itinerary>()
.ForMember(itinerary => itinerary.Origin, expression => expression.MapFrom(itineraryModel => itineraryModel));
Similarly you can define reverse mappings manually if needed.
BTW ReverseMap() is not recommended by author
https://jimmybogard.com/automapper-usage-guidelines/
I am using automapper in c#.
class A
{
public int Value { get; set; }
public string Code { get; set; }
public B? Details { get; set; }
}
class B
{
public int Id { get; set;}
public string Name { get; set; }
}
class C
{
public int Value { get; set; }
public string Code { get; set; }
public int? DetailId { get; set; }
public string? DetailName { get; set; }
}
And in automapper I used like below:
CreateMap<C, A>()
.ForPath(o => o.Details.Id, b => b.MapFrom(z => z.DetailId))
.ForPath(o => o.Details.Name, b => b.MapFrom(z => z.DetailName))
.ReverseMap();
When I using like the above mapping, I get like output as
"details ": {
"id": 0,
"name": ""
}
I need to get Details value as null instead of an object type if it's members have no value. i.e) DetailId and DetailName have no value. How to get this?
"details" : null
You can user Conditional Mapping
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<C, B>()
.ForMember(o => o.Id, b => b.MapFrom(z => z.DetailId))
.ForMember(o => o.Name, b => b.MapFrom(z => z.DetailName));
cfg.CreateMap<C, A>()
.ForMember(o => o.Details, b => b.MapFrom((c, a, obj, context) => !string.IsNullOrEmpty(c.DetailName) ? context.Mapper.Map<B>(c) : null))
.ReverseMap();
});
You could do this with an AutoMapper after map action.
Something like this:
CreateMap<C, A>()
.ForPath(o => o.Details.Id, b => b.MapFrom(z => z.DetailId))
.ForPath(o => o.Details.Name, b => b.MapFrom(z => z.DetailName))
.AfterMap((src, dest) =>
{
dest.Details = src.DetailId.HasValue && src.DetailName != null
? dest.Details
: null;
})
.ReverseMap());
You can use IValueResolver interface to achieve what you require. Documentation: http://docs.automapper.org/en/stable/Custom-value-resolvers.html
Also there's a similar question: Automapper Mapping Multiple Properties to Single Property
Configuration:
CreateMap<C, A>()
.ForMember(o => o.Details, b => b.MapFrom<DetailsValueResolver>())
.ReverseMap();
Implementation:
// Note: this does not cover ReverseMap() when you would try to convert A to C
public class DetailsValueResolver : IValueResolver<C, A, B>
{
// Runs every time you map C to A
public B Resolve(C source, A destination, B destMember, ResolutionContext context)
{
// Covers cases where you can get null or empty DetailName, as well as null or zero DetailId
if (!string.IsNullOrEmpty(source.DetailName) && source.DetailId > 0)
{
return new B { Id = (int)source.DetailId, Name = source.DetailName };
}
return null;
}
}
You also can omit explicitly setting strings and classes as nullable types with ? as you do here:
public B? Details { get; set; }
public string? DetailName { get; set; }
Because string type and any class is null by default.
public class Complex
{
public A A { get; set; }
public A B { get; set; }
}
public class A
{
public int a1 { get; set; }
public int a2 { get; set; }
}
public class B
{
public int b1 { get; set; }
public int b2 { get; set; }
}
//----------------Source Object End Here---------------------
public class Simple <----[This Simple class has only properties of A class]
{
public int aa1 { get; set; }
public int aa2 { get; set; }
}
//----------------Destination Object End Here---------------------
CreateMap<A, Simple>()
.ForMember(dest => dest.aa1, opt => opt.MapFrom(src => src.a1))
.ForMember(dest => dest.aa2, opt => opt.MapFrom(src => src.a2))
// Mapper IS NOT AVAILABLE HERE AS I AM USING PROFILE BASED CONFIGURATION
CreateMap<Complex, Simple>()
.ConvertUsing(src => Mapper.Map<A, Simple>(src.A)); <------Error at this line
//----------------Automammer config End Here---------------------
How to flatten from Complex to Simple? I don't wish to map Complex.A to Simple one by one again in the Complex to Simple config as it is already configured above.
Finally, I figured out with another overloaded method of ConvertUsing
CreateMap<Complex, Simple>()
.ConvertUsing((src,ctx) => {
return ctx.Mapper.Map<Complex, Simple>(src.A)
});
I feel this overloaded method has quite a multiple possibilities and flexibility. I don't have further issue of accessing Mapper directly as mentioned in the question. This overloaded method has its own context parameter (ResolutionContext). We can use Mapper from this context parameter like ctx.Mapper.Map<Complex, Simple>(src.A)
Here is an example:
class Source
{
public int Value_1 { get; set; }
public int Value_2 { get; set; }
public int MissedValue { get; set; }
}
class Destination
{
public int Value_1 { get; set; }
public int Value_2 { get; set; }
}
void Main()
{
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dst => dst.Value_1, m => m.MapFrom(src => src.Value_1))
.ForMember(dst => dst.Value_2, m => m.MapFrom(src => src.Value_2))
;
});
config.AssertConfigurationIsValid();
}
In the end, i need AssertConfigurationIsValid() to throw an exception when i have missing Source fields and not just destination fields.
Also, how to force it to disable any type and naming conversions? Like int -> string, Test.Id -> TestId, etc.
I need it to be as restrict as possible. Or maybe there is a better alternative for my case?
UPDATE_0:
Ok. Found an answer to the first part.
Just need to use
cfg.CreateMap<Source, Destination>(AutoMapper.MemberList.Source)
It would be great to have support for both validation types alltogether, but it's a good start already.
Now what left is, how to remove all naming conventions and type converters.
I'm trying to map one list to another, but not everything I want is in the source object. Let me explain:
These are my two objects:
public class SourceItem
{
public int Id { get; set;}
public string Name { get; set;}
}
public class TargetItem
{
public int TargetId { get; set;}
public string TargetName { get; set;}
public int Foo { get; set; }
}
This is how I configure my Mapper:
public class MyProfile : Profile
{
public MyProfile()
{
CreateMap<SourceItem, TargetItem>()
.ForMember(dest => dest.TargetId, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.TargetName, opt => opt.MapFrom(src => src.Name));
}
}
When I do this...
var targets = Mapper.Map<List<SourceItem>, List<TargetItem>>(sourceItems);
...the targets list's objects will have Foo set to 0. Obviously. What I'm trying to do is to also initialize Foo (with the parameter). Like this:
fooParameter = 321;
List<TargetItem> targets = Mapper.Map<List<SourceItem>, List<TargetItem>>(sourceItems);
foreach(var target in targets)
{
target.Foo = fooParameter;
}
Is it possible, to do it using Mapper?
Thanks in advance!
You can't do exactly what you want, but you can get pretty close by specifying mapping options when you call Map.
fooParameter = 321;
var targets = Mapper.Map<List<SourceItem>, List<TargetItem>>(sourceItems, opt =>
opt.AfterMap(
(src, dest) => {
foreach (var d in dest)
{
d.Foo = fooParameter;
}
})
);