2010-11-04 4 views
16

Tôi có hai mô hình cho biểu mẫu của tôi, một ViewModel sẽ đến nó, và một ControlModel đến từ nó. ControlModel có tất cả các tên và phân cấp trường giống nhau, nhưng tất cả các trường là một kiểu dữ liệu chuỗi.AutoMapper: Làm thế nào để phân tích một Int từ một String và có thể tạo ra các quy tắc dựa trên kiểu dữ liệu?

Làm cách nào để mã AutoMapper chuyển đổi trường chuỗi thành số nguyên? Tôi đã thử Int32.Parse (myString) nhưng Int32 không có sẵn trong biểu thức (đưa ra một lỗi).

Mapper.CreateMap<SourceClass, DestinationClass>() 
     .ForMember(dest => dest.myInteger, 
        opt => opt.MapFrom(src => src.myString)); 

Các loại trong lớp và các loại chuyển đổi tương ứng của họ:

chuỗi int, int ?, đôi, đôi ?, DateTime, và bool

Thêm vào đó, là có cách nào để khái quát ánh xạ theo cách mà tất cả các số nguyên trong mục tiêu được phân tích cú pháp với hàm đó? Nói cách khác, có cách nào để tạo ánh xạ cho các kiểu dữ liệu không?

EDIT:

này sẽ hứa hẹn:

AutoMapper.Mapper.CreateMap<string, int>() 
      .ConvertUsing(src => Convert.ToInt32(src)); 

EDIT: post Đây là thực sự hữu ích

+0

Tại sao cậu lại nhận được chuỗi chỉ trong ControlModel của bạn? – Jaime

+0

Điều này cho phép tôi đánh giá liệu dữ liệu có hợp lệ hay không. Nếu không, các giá trị không ánh xạ và bị loại bỏ trong mô hình ràng buộc. –

+0

Lý do khác là MVC 2 Futures không lập bản đồ gấp đôi, số nguyên, và ngày tháng chính xác –

Trả lời

21

tôi đã kết thúc làm một cái gì đó như thế này:

Mapper.CreateMap<string, int>().ConvertUsing<IntTypeConverter>(); 
Mapper.CreateMap<string, int?>().ConvertUsing<NullIntTypeConverter>(); 
Mapper.CreateMap<string, decimal?>().ConvertUsing<NullDecimalTypeConverter>(); 
Mapper.CreateMap<string, decimal>().ConvertUsing<DecimalTypeConverter>(); 
Mapper.CreateMap<string, bool?>().ConvertUsing<NullBooleanTypeConverter>(); 
Mapper.CreateMap<string, bool>().ConvertUsing<BooleanTypeConverter>(); 
Mapper.CreateMap<string, Int64?>().ConvertUsing<NullInt64TypeConverter>(); 
Mapper.CreateMap<string, Int64>().ConvertUsing<Int64TypeConverter>(); 
Mapper.CreateMap<string, DateTime?>().ConvertUsing<NullDateTimeTypeConverter>(); 
Mapper.CreateMap<string, DateTime>().ConvertUsing<DateTimeTypeConverter>(); 

Mapper.CreateMap<SourceClass, DestClass>(); 

Mapper.Map(mySourceObject, myDestinationObject); 

Và các lớp mà nó tham chiếu (dự thảo đầu tiên):

// TODO: Boil down to two with Generics if possible 
#region AutoMapTypeConverters 
// Automap type converter definitions for 
// int, int?, decimal, decimal?, bool, bool?, Int64, Int64?, DateTime 
// Automapper string to int? 
private class NullIntTypeConverter : TypeConverter<string, int?> 
{ protected override int? ConvertCore(string source) 
    { if (source == null) 
      return null; 
     else 
     { int result; 
      return Int32.TryParse(source, out result) ? (int?) result : null; 
} } } 
// Automapper string to int 
private class IntTypeConverter : TypeConverter<string, int> 
{ protected override int ConvertCore(string source) 
    { if (source == null) 
      throw new MappingException("null string value cannot convert to non-nullable return type."); 
     else 
      return Int32.Parse(source); 
} } 
// Automapper string to decimal? 
private class NullDecimalTypeConverter : TypeConverter<string, decimal?> 
{ protected override decimal? ConvertCore(string source) 
    { if (source == null) 
      return null; 
     else 
     { decimal result; 
      return Decimal.TryParse(source, out result) ? (decimal?) result : null; 
} } } 
// Automapper string to decimal 
private class DecimalTypeConverter : TypeConverter<string, decimal> 
{ protected override decimal ConvertCore(string source) 
    { if (source == null) 
      throw new MappingException("null string value cannot convert to non-nullable return type."); 
     else 
      return Decimal.Parse(source); 
} } 
// Automapper string to bool? 
private class NullBooleanTypeConverter : TypeConverter<string, bool?> 
{ protected override bool? ConvertCore(string source) 
    { if (source == null) 
      return null; 
     else 
     { bool result; 
      return Boolean.TryParse(source, out result) ? (bool?) result : null; 
} } } 
// Automapper string to bool 
private class BooleanTypeConverter : TypeConverter<string, bool> 
{ protected override bool ConvertCore(string source) 
    { if (source == null) 
      throw new MappingException("null string value cannot convert to non-nullable return type."); 
     else 
      return Boolean.Parse(source); 
} } 
// Automapper string to Int64? 
private class NullInt64TypeConverter : TypeConverter<string, Int64?> 
{ protected override Int64? ConvertCore(string source) 
    { if (source == null) 
      return null; 
     else 
     { Int64 result; 
      return Int64.TryParse(source, out result) ? (Int64?)result : null; 
} } } 
// Automapper string to Int64 
private class Int64TypeConverter : TypeConverter<string, Int64> 
{ protected override Int64 ConvertCore(string source) 
    { if (source == null) 
      throw new MappingException("null string value cannot convert to non-nullable return type."); 
     else 
      return Int64.Parse(source); 
} } 
// Automapper string to DateTime? 
// In our case, the datetime will be a JSON2.org datetime 
// Example: "/Date(1288296203190)/" 
private class NullDateTimeTypeConverter : TypeConverter<string, DateTime?> 
{ protected override DateTime? ConvertCore(string source) 
    { if (source == null) 
      return null; 
     else 
     { DateTime result; 
      return DateTime.TryParse(source, out result) ? (DateTime?) result : null; 
} } } 
// Automapper string to DateTime 
private class DateTimeTypeConverter : TypeConverter<string, DateTime> 
{ protected override DateTime ConvertCore(string source) 
    { if (source == null) 
      throw new MappingException("null string value cannot convert to non-nullable return type."); 
     else 
      return DateTime.Parse(source); 
} } 
#endregion 
+0

Chiến lược khoảng trắng ở đây khiến mắt tôi đau. – Gusdor

6

Bạn có thể tạo các thuộc tính trong lớp nguồn của bạn mà đúc lĩnh vực để các loại như chúng tồn tại ở đích. Sau đó, sử dụng AutoMapper theo cách đơn giản.

public class source 
{ 
    public int _myfield; 

    public string MyField 
    { 
    get 
    { 
     return _myfield.ToString(); 
    } 
    } 
} 

public class destination 
{ 
    public string MyField { get; set; } 
} 
+2

Đó là một ý tưởng rất khéo léo. –

+0

Kiểm tra câu trả lời có thể tôi vừa thêm vào. –