2012-08-15 146 views
0

我在VB中有以下源代碼類。爲什麼AutoMapper不能映射這些嵌套集合?

Public Class Product 
... 

    Public Property itemizedSize As Size() 
     Get 
      Return _arrItemizedSizes 
     End Get 
     Set(ByVal value As Size()) 
      _arrItemizedSizes = value 
     End Set 
    End Property 
... 
End Class 


Public Class Size 
    Private _strName As String 
    Private _intQuantity As Integer 
    Private _intScale As Integer 

    Public Property name() As String 
     Get 
      Return _strName 
     End Get 
     Set(ByVal value As String) 
      _strName = value 
     End Set 
    End Property 

    Public Property quantity() As Integer 
     Get 
      Return _intQuantity 
     End Get 
     Set(ByVal value As Integer) 
      _intQuantity = value 
     End Set 
    End Property 

    Public Property Scale() As Integer 
     Get 
      Return _intScale 
     End Get 
     Set(ByVal value As Integer) 
      _intScale = value 
     End Set 
    End Property 

    Public Sub New() 
     _strName = "" 
     _intQuantity = 0 
     _intScale = 0 
    End Sub 

    Public Sub New(ByVal name As String, ByVal quantity As Integer, Optional ByVal Scale As Integer = 0) 
     _strName = name 
     _intQuantity = quantity 
     _intScale = Scale 
    End Sub 
End Class 

,我試圖

public class ProductsViewModel : List<ProductViewModel> 
{ 
    ... 
} 

public class ProductViewModel 
{ 
    ... 
    public SizeViewModel[] ItemizedSize { get; set; } 
    ... 
} 

public class SizeViewModel 
{ 
    public string Name { get; set; } 
    public int Quantity { get; set; } 
} 

我使用下面的代碼做我的映射將其映射到這些C#類......不過,我得到一個異常說,映射不定義從尺寸到尺寸查看模型

AutoMapper.Mapper.CreateMap<Size, SizeViewModel>(); 
AutoMapper.Mapper.CreateMap<Product, ProductViewModel>(); 

ProductsViewModel model = AutoMapper.Mapper.Map<List<Product>, ProductsViewModel>(productDetails); 
AutoMapper.Mapper.AssertConfigurationIsValid(); 

有什麼,我失蹤了?任何幫助將不勝感激..謝謝!

+1

.Net有班級規模。改變你班級的名字。也許它工作。 – 2012-08-15 01:14:15

+0

好點 - 將鼠標懸停在VS中的Size上,看看你得到的是什麼名字空間... automapper並不在乎什麼類型被映射,但你確實:) – Charleh 2012-08-15 09:55:55

+0

是的,我想過...它是Size類在我的名字空間 – ntsue 2012-08-15 11:29:56

回答

0

我剛剛運行你的代碼,一切都完美的爲我工作。你使用的是什麼版本的AutoMapper?我使用的是2.0.0.0版本。

這裏是我的VB代碼:

Public Class Product 


    Public Property itemizedSize As Size() 
     Get 
      Return _arrItemizedSizes 
     End Get 
     Set(ByVal value As Size()) 
      _arrItemizedSizes = value 
     End Set 
    End Property 

    Private Property _arrItemizedSizes As Size() 

End Class 


Public Class Size 
    Private _strName As String 
    Private _intQuantity As Integer 
    Private _intScale As Integer 

    Public Property name() As String 
     Get 
      Return _strName 
     End Get 
     Set(ByVal value As String) 
      _strName = value 
     End Set 
    End Property 

    Public Property quantity() As Integer 
     Get 
      Return _intQuantity 
     End Get 
     Set(ByVal value As Integer) 
      _intQuantity = value 
     End Set 
    End Property 

    Public Property Scale() As Integer 
     Get 
      Return _intScale 
     End Get 
     Set(ByVal value As Integer) 
      _intScale = value 
     End Set 
    End Property 

    Public Sub New() 
     _strName = "" 
     _intQuantity = 0 
     _intScale = 0 
    End Sub 

    Public Sub New(ByVal name As String, ByVal quantity As Integer, Optional ByVal Scale As Integer = 0) 
     _strName = name 
     _intQuantity = quantity 
     _intScale = Scale 
    End Sub 
End Class 

這裏是C#:

using System; 
using System.Collections.Generic; 
using SimpleLib; //That is the VB assembly 

namespace ConsoleApplication1 
{ 
    public class ProductsViewModel : List<ProductViewModel> 
    { 

    } 

    public class ProductViewModel 
    { 
     public SizeViewModel[] ItemizedSize { get; set; } 
    } 

    public class SizeViewModel 
    { 
     public string Name { get; set; } 
     public int Quantity { get; set; } 

     public override string ToString() 
     { 
      return this.Name + " " + this.Quantity.ToString(); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      AutoMapper.Mapper.CreateMap<Size, SizeViewModel>(); 
      AutoMapper.Mapper.CreateMap<Product, ProductViewModel>(); 

      List<Product> productDetails = new List<Product>() 
               { 
                new Product() {itemizedSize = new Size[1] {new Size("hello", 2, 5)}}, 
                new Product() {itemizedSize = new Size[1] {new Size("hello2", 4, 10)}} 
               }; 

      ProductsViewModel model = AutoMapper.Mapper.Map<List<Product>, ProductsViewModel>(productDetails); 
      AutoMapper.Mapper.AssertConfigurationIsValid(); 

      Console.WriteLine("Count: {0}", model.Count); 
      Console.WriteLine("First Product: {0}", model[0].ItemizedSize[0].ToString()); 
      Console.WriteLine("Second Product: {0}", model[1].ItemizedSize[0].ToString()); 

      Console.ReadLine(); 
     } 
    } 
} 

正如你所看到的,我使用一個控制檯應用程序進行測試。我通過正確的映射獲得了一切,AutoMapper沒有配置錯誤。

這是輸出我得到:

Count: 2 
First Product: hello 2 
Second Product: hello2 4