Created
September 22, 2013 11:44
-
-
Save ichiroku11/6659180 to your computer and use it in GitHub Desktop.
AutoMapperを試してみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| using AutoMapper; | |
| namespace Test { | |
| class User1 { | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| } | |
| class User2 { | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| } | |
| class User3 { | |
| public int Id { get; set; } | |
| public string FullName { get; set; } | |
| } | |
| [TestClass] | |
| public class AutoMapperTest { | |
| [TestInitialize] | |
| public void InitTest() { | |
| // マッピング設定をリセット | |
| Mapper.Reset(); | |
| } | |
| [Description("シンプルなマッピング")] | |
| [TestMethod] | |
| public void Test_SimpleMapping() { | |
| // User1 => User2 のマッピング設定を作成 | |
| Mapper.CreateMap<User1, User2>(); | |
| // User1 => User2 へマッピングを実行 | |
| var actual = Mapper.Map<User1, User2>(new User1 { Id = 1, Name = "Aaa" }); | |
| Assert.AreEqual(1, actual.Id); | |
| Assert.AreEqual("Aaa", actual.Name); | |
| } | |
| [Description("AssertConfigurationIsValid")] | |
| [TestMethod] | |
| public void Test_ThrowAutoMapperConfigurationException() { | |
| // User1 => User3 | |
| Mapper.CreateMap<User1, User3>(); | |
| try { | |
| // マッピングを検証 | |
| Mapper.AssertConfigurationIsValid(); | |
| Assert.Fail(); | |
| } catch(AutoMapperConfigurationException exception) { | |
| // "FullName"のマッピング設定がないよ! | |
| var actual = exception.Errors.First().UnmappedPropertyNames.First(); | |
| Assert.AreEqual("FullName", actual); | |
| } | |
| } | |
| [Description("ForMemberによるマッピングのカスタマイズ1")] | |
| [TestMethod] | |
| public void Test_ForMemberIgnore() { | |
| Mapper.CreateMap<User1, User3>() | |
| // User3.FullNameはマッピングせず、バリデーションの対象外に | |
| .ForMember( | |
| dst => dst.FullName, | |
| opt => opt.Ignore()); | |
| Mapper.AssertConfigurationIsValid(); | |
| var actual = Mapper.Map<User1, User3>(new User1 { Id = 1, Name = "Aaa" }); | |
| Assert.AreEqual(1, actual.Id); | |
| Assert.IsNull(actual.FullName); | |
| } | |
| [Description("ForMemberによるマッピングのカスタマイズ2")] | |
| [TestMethod] | |
| public void Test_ForMemberMapFrom() { | |
| Mapper.CreateMap<User1, User3>() | |
| // マッピングを指定(User1.Name => User3.FullName) | |
| .ForMember( | |
| dst => dst.FullName, | |
| opt => opt.MapFrom(src => src.Name)); | |
| Mapper.AssertConfigurationIsValid(); | |
| var actual = Mapper.Map<User1, User3>(new User1 { Id = 1, Name = "Aaa" }); | |
| Assert.AreEqual(1, actual.Id); | |
| Assert.AreEqual("Aaa", actual.FullName); | |
| } | |
| [Description("ForMemberによるマッピングのカスタマイズ3")] | |
| [TestMethod] | |
| public void Test_ForMemberUseValue() { | |
| Mapper.CreateMap<User1, User3>() | |
| // マッピングを指定(User3.FullNameは固定値) | |
| .ForMember( | |
| dst => dst.FullName, | |
| opt => opt.UseValue("Bbb")); | |
| Mapper.AssertConfigurationIsValid(); | |
| var actual = Mapper.Map<User1, User3>(new User1 { Id = 1, Name = "Aaa" }); | |
| Assert.AreEqual(1, actual.Id); | |
| Assert.AreEqual("Bbb", actual.FullName); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AutoMapper