2012-11-25 64 views
0

如何映射名爲Unit的c#類,該單元又具有List<Unit>使用AutoMapper映射層次結構對象

具體的場景是一個rootUnit對象,它包含一個List,它們是第一級的子級。

第一級兒童單元對象將不包含任何其他單位,因此層次結構中不會有遞歸。

public class Unit 
    { 
     public Unit() 
     { 
      // Explicitly set the default value for the first unit in a hierarchy 
      HierarchyIndex = 0; 
      Units = new List<Unit>(); 
     } 

     public List<Unit> Units { get; set; } 

     public int UnitId { get; set; } 
     public string Name { get; set; }  
     public Nullable<int> ParentId { get; set; } 
     public int TemplateId { get; set; }  
     public bool HasChildren { get; set; } 
     public bool IsFolder { get; set; } 
     public DateTime CreatedAt { get; set; } 
     public int HierarchyIndex { get; set; } 
    } 

的單位地圖上面這個視圖模型:

public class UnitTreeViewModel 
{ 
    [JsonProperty("key")] 
    public int UnitId { get; set; } 
    [JsonProperty("title")] 
    public string Name { get; set; } 
    [JsonProperty("isLazy")] 
    public bool HasChildren { get; set; } 
    [JsonProperty("isFolder")] 
    public bool IsFolder { get; set; } 
} 
+0

你需要很多'UnitTreeViewModel'每個'Unit'? (即,一個爲父母,一個爲列表項目?) – Mightymuke

回答

1

假設的答案,我在評論上述問題是肯定的,那麼你就需要應用映射幾次 - 與此類似問題:AutoMapper and flattening nested arrays

像這樣的東西可能會奏效:

AutoMapperConfigurator.cs

namespace StackOverflow.ListUnit 
{ 
    using AutoMapper; 

    public class MyProfile : Profile 
    { 
     public override string ProfileName 
     { 
      get 
      { 
       return "MyProfile"; 
      } 
     } 

     protected override void Configure() 
     { 
      Mapper.CreateMap<Unit, UnitTreeViewModel>(); 
     } 
    } 
} 

MappingTests.cs

namespace StackOverflow.ListUnit 
{ 
    using System.Collections.Generic; 
    using System.Linq; 

    using AutoMapper; 

    using NUnit.Framework; 

    [TestFixture] 
    public class MappingTests 
    { 
     [Test] 
     public void AutoMapper_Configuration_IsValid() 
     { 
      Mapper.Initialize(m => m.AddProfile<MyProfile>()); 
      Mapper.AssertConfigurationIsValid(); 
     } 

     [Test] 
     public void AutoMapper_Mapping_IsValid() 
     { 
      Mapper.Initialize(m => m.AddProfile<MyProfile>()); 
      Mapper.AssertConfigurationIsValid(); 

      var unit = new Unit 
       { 
        UnitId = 123, 
        Name = "Stack Overflow Rocks", 
        HasChildren = true, 
        IsFolder = true, 
        Units = 
         new List<Unit> 
          { 
           new Unit 
            { 
             UnitId = 123123, 
             Name = "I'm the first baby", 
             HasChildren = false, 
             IsFolder = false, 
            }, 
           new Unit 
            { 
             UnitId = 123321, 
             Name = "I'm the second baby", 
             HasChildren = false, 
             IsFolder = false, 
            } 
          } 
       }; 

      var unitViewModels = new List<UnitTreeViewModel> 
       { 
        Mapper.Map<Unit, UnitTreeViewModel>(unit) 
       }; 
      unitViewModels.AddRange(
       unit.Units.Select(Mapper.Map<Unit, UnitTreeViewModel>)); 

      Assert.That(unitViewModels, Is.Not.Null); 
      Assert.That(unitViewModels.Count(), Is.EqualTo(3)); 
      var unitViewModel = unitViewModels.First(x => x.UnitId == 123); 
      Assert.That(unitViewModel, Is.Not.Null); 
      Assert.That(unitViewModel.Name, Is.EqualTo("Stack Overflow Rocks")); 
      unitViewModel = unitViewModels.First(x => x.UnitId == 123123); 
      Assert.That(unitViewModel, Is.Not.Null); 
      Assert.That(unitViewModel.Name, Is.EqualTo("I'm the first baby")); 
      unitViewModel = unitViewModels.First(x => x.UnitId == 123321); 
      Assert.That(unitViewModel, Is.Not.Null); 
      Assert.That(unitViewModel.Name, Is.EqualTo("I'm the second baby")); 
     } 
    } 
} 
+0

這很有趣。我使用了以前的默認映射(Mapper.CreateMap ();)。沒想到這會起作用。但它的確如此。感謝這個想法。 – Elisabeth