public void Test0_1() { IServiceCollection sc = new ServiceCollection() .AddLightweightMapper(); IServiceProvider sp = sc.BuildServiceProvider(); IMapperProvider provider = sp.GetRequiredService <IMapperProvider>(); Assert.Equal(10, provider.Convert <int, int>(10)); Assert.Null(provider.Convert <int?, int?>(null)); Assert.Equal(10, provider.Convert <int?, int>(10)); Assert.Null(provider.Convert <int, byte?>(300)); Assert.Equal(0, provider.Convert <int, byte>(300)); Assert.Equal("A", provider.Convert <string, string>("A")); Assert.Equal(new DateTime(2010, 1, 1), provider.Convert <DateTime, DateTime>(new DateTime(2010, 1, 1))); Assert.Equal(new DateTime(), provider.Convert <DateTime?, DateTime>(null)); Assert.Null(provider.Convert <DateTime?, DateTime?>(null)); var list = provider.ConvertList <string, string>(new List <string>() { "A", "B" }); Assert.Equal("B", list[1]); var dic = provider.Convert <Dictionary <string, int>, Dictionary <string, byte?> >(new Dictionary <string, int>() { { "A", 300 }, { "B", 10 } }); Assert.Null(dic["A"]); Assert.Equal(10, dic["B"].Value); }
public void Test9() { IServiceCollection sc = new ServiceCollection() .AddLightweightMapper(); IServiceProvider sp = sc.BuildServiceProvider(); IMapperProvider provider = sp.GetRequiredService <IMapperProvider>(); S5 s5 = new S5() { S1 = new S1() { A = 10 } }; s5.Self = s5; T5 t5 = provider.Convert <S5, T5>(s5); Assert.Equal(t5.Self, t5); Assert.Equal(10, t5.T1.A); // List List <S5> list = new List <S5>(); list.Add(s5); list.Add(s5); list.Add(s5); List <T5> t5List = provider.ConvertList <S5, T5>(list); Assert.NotNull(t5List); Assert.Equal(3, t5List.Count); Assert.Equal(t5List[0], t5List[1]); Assert.Equal(t5List[0], t5List[2]); Dictionary <S5, S5> dic = new Dictionary <S5, S5>() { { s5, s5 }, { new S5() { S1 = new S1 { A = 11 } }, s5 } }; var targetDic = provider.Convert <Dictionary <S5, S5>, Dictionary <T5, T5> >(dic); Assert.NotNull(targetDic); Assert.Equal(2, targetDic.Count); var item1 = dic.First(); var item2 = dic.Last(); Assert.Equal(item1.Value, item2.Value); Assert.Equal(item1.Key, item1.Value); Assert.Equal(11, item2.Key.S1.A); }
public void TestMapper() { // SourceA --> TargetA // A==>A B==>D C==>E A==>X1 C==>X2 IServiceCollection sc = new ServiceCollection() .AddLightweightMapper(); IServiceProvider sp = sc.BuildServiceProvider(); IMapperProvider provider = sp.GetRequiredService <IMapperProvider>(); SourceA a = new SourceA() { A = "A", B = "B", C = "C", SA = new StructA() { X = 1, Y = 2 } }; var ta = provider.Convert <SourceA, TargetA>(a); Assert.Equal("A", ta.A); Assert.Equal("B", ta.D); Assert.Equal("C", ta.E); Assert.Equal("A", ta.X1); Assert.Equal("C", ta.X2); Assert.False(Object.ReferenceEquals(ta.TSA, a.SA)); Assert.Equal(1, ta.TSA.X); Assert.Equal(2, ta.TSA.Y); }