Friday 15 March 2013

c# - AutoMapper custom type convert using ConstructServicesUsing -


According to

, I should be able to create and use an example of a custom type converter in this way: < Previous> var dest = Mapper.Map & lt; Source, Destination & gt; (New source {value = 15}, opt => opt. Constants usage (childContainer.GetInstance);

I have the following source and destination types:

  public square 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 Date Time Price 2 {Receive; Set; } Public Type Value3 {get; Set; }}   

and the following types of converters:

  public class datetime type converters: ITepe Convert & lt; String, DateTime & gt; {Public Date Time Convert (Result Reference Reference) {Return to System. Convert Dondetime (context.SourceValue); }} Public class SourceDestinationTypeConverter: ITypeConverter & lt; Source, Destination & gt; {Convert public destination (resolution reference reference) {var dest = new destination (); // some conversion return destination; }}   

It should be a simple test that one of the date properties changes correctly:

  TestFixture] public class CustomTypeConverterTest {[Test] Public Zero withMap () {Mapper. Createmap & lt; String, DateTime & gt; (). Convertible (new dateTimetype converter ()); Mapper.CreatMap & lt; Source, Destination & gt; (). ConstructUsingServiceLocator (); Var destination = mapper Map & lt; Source, Destination & gt; (New source {value1 = "15", value2 = "01/01/2000"}, option => option.Construct Services tool (type => New SourceDestinationTypeConverter ()); // Exception is thrown here Assert.AreEqual (destination.Value2.Year, 2000); }}   

But before loud I get an exception:

System.InvalidCastException: to type the 'SourceDestinationTypeConverter' type of object to be deleted Unable to 'destination' . . My question is now, how can I use custom code converter ConstructServicesUsing () ?

I tested this code and it worked using the following code:

Pre> Public Zero TestMethod1 () {Mapper.CreateMap & lt; String, DateTime & gt; (). Convertible (new dateTimetype converter ()); Mapper.CreatMap & lt; String, type & gt; (). ConvertUzing (new stringantap कनવર ()); Mapper Createmap & lt; String, int & gt; (). ConvertUzing (new string important ()); Mapper Createmap & lt; Source, Destination & gt; (); Var destination = mapper Map & lt; Source, Destination & gt; (New source {value1 = "15", value 2 = "01/01/2000", value 3 = "system string"}, option => option.Construct services usage (type = & gt; new sourceDestinetype Converter ())); Decide. First (destination value 2. years, 2000); }

and additional converters:

  public class string type converters: ITypeConverter & lt; String, type & gt; {Public type convert (resolution reference context) {return type.GetType (context.SourceValue.ToString ()); }} Public class string import converter: ITypeConverter & lt; String, int & gt; {Public Int Convert (Resolution Contact Reference) {return IN32.Parse (context.SourceValue.ToString ()); }}   

Automapper was unable to type string and mapping for string to int, besides I had to remove the following line

  Mapper. Createmap & lt; Source, Destination & gt; (). ConstructUsingServiceLocator ();   

and

  Mapper. Crescent & lt; Source, Destination & gt; ();   

I do not know about the "ConstructUsingServiceLocator ()" option, but in this case it works except ... (I do not know that leaving it will lead to other problems Until U till now I have not used this option while working with Optopomper.)

Please note that since the Convertment failed I must add the "value 3" parameter ... To change a zero value type Much Mush Can be skewed ... (and I do not know what kind of conversion is happening here ...)

No comments:

Post a Comment