Custom AutoMapper Type Conversion Using ConstructServicesUsing

According to AutoMapper Documentation , I would have to create and use an instance of a custom type converter using this:

var dest = Mapper.Map<Source, Destination>(new Source { Value = 15 }, opt => opt.ConstructServicesUsing(childContainer.GetInstance)); 

I have the following types of sources and destinations:

 public class Source { public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } } public class Destination { public int Value1 { get; set; } public DateTime Value2 { get; set; } public Type Value3 { get; set; } } 

And the following type converters:

 public class DateTimeTypeConverter : ITypeConverter<string, DateTime> { public DateTime Convert(ResolutionContext context) { return System.Convert.ToDateTime(context.SourceValue); } } public class SourceDestinationTypeConverter : ITypeConverter<Source, Destination> { public Destination Convert(ResolutionContext context) { var dest = new Destination(); // do some conversion return dest; } } 

This simple test should state that one of the date properties is converted correctly:

 [TestFixture] public class CustomTypeConverterTest { [Test] public void ShouldMap() { Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter()); Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator(); var destination = Mapper.Map<Source, Destination>( new Source { Value1 = "15", Value2 = "01/01/2000", }, options => options.ConstructServicesUsing( type => new SourceDestinationTypeConverter()) ); // exception is thrown here Assert.AreEqual(destination.Value2.Year, 2000); } } 

But I am already getting an exception before the statement happens:

System.InvalidCastException : Unable to cast object of type 'SourceDestinationTypeConverter ' to type 'Destination' .

Now my question is: how to use a custom type converter using ConstructServicesUsing() ?

+4
source share
2 answers

I tested this code and got this working using the following code:

 public void TestMethod1() { Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter()); Mapper.CreateMap<string, Type>().ConvertUsing(new StringTypeConverter()); Mapper.CreateMap<string, int>().ConvertUsing(new StringIntConverter()); Mapper.CreateMap<Source, Destination>(); var destination = Mapper.Map<Source, Destination>( new Source { Value1 = "15", Value2 = "01/01/2000", Value3 = "System.String" }, options => options.ConstructServicesUsing(type => new SourceDestinationTypeConverter()) ); Assert.AreEqual(destination.Value2.Year, 2000); } 

And additional converters:

  public class StringTypeConverter : ITypeConverter<string, Type> { public Type Convert(ResolutionContext context) { return Type.GetType(context.SourceValue.ToString()); } } public class StringIntConverter : ITypeConverter<string, int> { public int Convert(ResolutionContext context) { return Int32.Parse(context.SourceValue.ToString()); } } 

Automapper did not have a mapping for String to Type and String to Int. I also had to delete the following line

 Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator(); 

and replace it with

 Mapper.CreateMap<Source, Destination>(); 

I don’t know about the option “ConstructUsingServiceLocator ()”, but in this case it doesn’t work) (I have no idea if it will leave this, which will cause other problems for u. So far I haven’t but used this parameter when working with Automapper.)

Please note that I had to add the "Value3" parameter because the conversion failed ... Converting a NULL value to a type can be quite complicated ... (And I don't know which conversion should happen here ...)

+7
source

I encountered an InvalidCastException error: It is not possible to cast an object of type 'xxxTypeConverter' to type 'AutoMapper.ITypeConverter'2 [System.String, System.DateTime]' when executing an Automapper sample for CustomTypeConverter. I fixed the problem by replacing

 public class DateTimeTypeConverter : ITypeConverter<string, DateTime> 

with

  public class DateTimeTypeConverter : AutoMapper.ITypeConverter<string, DateTime> 

and similarly

 public class TypeTypeConverter : ITypeConverter<string, Type> 

with

 public class TypeTypeConverter : AutoMapper.ITypeConverter<string, Type> 
0
source

Source: https://habr.com/ru/post/1487532/


All Articles