瀏覽代碼

began LuObjectConverter

develop
Robin Thoni 5 年之前
父節點
當前提交
1fa0d152d6

+ 17
- 3
.idea/.idea.luticate2/.idea/contentModel.xml 查看文件

@@ -47,12 +47,15 @@
47 47
               <e p="LuExpressionConverterOptions.cs" t="Include" />
48 48
               <e p="LuExpressionConverterVisitor.cs" t="Include" />
49 49
               <e p="LuExpressionParamReplaceVisitor.cs" t="Include" />
50
+              <e p="LuObjectConverterIdentity.cs" t="Include" />
50 51
             </e>
51 52
             <e p="Fields" t="Include">
52 53
               <e p="LuFieldsExtensions.cs" t="Include" />
53 54
               <e p="LuPartialFieldsParser.cs" t="Include" />
54 55
             </e>
55
-            <e p="LuExpressionUtils.cs" t="Include" />
56
+            <e p="ObjectConverter" t="Include">
57
+              <e p="LuObjectConverterPoco.cs" t="Include" />
58
+            </e>
56 59
             <e p="ObjectConverterDescriptor" t="Include">
57 60
               <e p="LuObjectConverterDescriptor.cs" t="Include" />
58 61
               <e p="LuObjectConverterDescriptorDbSet.cs" t="Include" />
@@ -74,6 +77,10 @@
74 77
                 <e p="LuSerializerContext.cs" t="Include" />
75 78
               </e>
76 79
             </e>
80
+            <e p="Utils" t="Include">
81
+              <e p="LuCoreExtension.cs" t="Include" />
82
+              <e p="LuExpressionUtils.cs" t="Include" />
83
+            </e>
77 84
           </e>
78 85
           <e p="Dbo" t="Include">
79 86
             <e p="Fields" t="Include">
@@ -102,6 +109,7 @@
102 109
           </e>
103 110
           <e p="Interfaces" t="Include">
104 111
             <e p="ILuCrud.cs" t="Include" />
112
+            <e p="ILuObjectConverter.cs" t="Include" />
105 113
             <e p="ILuObjectConverterDescriptor.cs" t="Include" />
106 114
             <e p="ILuPartialObjectCopier.cs" t="Include" />
107 115
           </e>
@@ -134,17 +142,23 @@
134 142
       </e>
135 143
       <e p="Luticate2.Auth.Tests" t="IncludeRecursive">
136 144
         <e p="Business" t="Include">
145
+          <e p="ExpressionConverter" t="Include">
146
+            <e p="ExpressionConverterTests.cs" t="Include" />
147
+            <e p="Objects.cs" t="Include" />
148
+          </e>
137 149
           <e p="Fields" t="Include">
138 150
             <e p="LuFieldDboTests.cs" t="Include" />
139 151
           </e>
140
-          <e p="LuExpressionUtilsTests.cs" t="Include" />
141 152
           <e p="ObjectConverter" t="Include">
142
-            <e p="ObjectConverterTests.cs" t="Include" />
153
+            <e p="ObjectConverter.cs" t="Include" />
143 154
             <e p="Objects.cs" t="Include" />
144 155
           </e>
145 156
           <e p="Pagination" t="Include">
146 157
             <e p="LuFilterTests.cs" t="Include" />
147 158
           </e>
159
+          <e p="Utils" t="Include">
160
+            <e p="LuExpressionUtilsTests.cs" t="Include" />
161
+          </e>
148 162
         </e>
149 163
         <e p="Luticate2.Auth.Tests.csproj" t="IncludeRecursive" />
150 164
         <e p="RandomTests.cs" t="Include" />

+ 231
- 0
Luticate2.Auth.Tests/Business/ExpressionConverter/ExpressionConverterTests.cs 查看文件

@@ -0,0 +1,231 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Linq.Expressions;
5
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
6
+using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
7
+using Luticate2.Auth.Utils.Interfaces;
8
+using Microsoft.Extensions.DependencyInjection;
9
+using Xunit;
10
+
11
+namespace Luticate2.Auth.Tests.Business.ExpressionConverter
12
+{
13
+    public class ExpressionConverterTests
14
+    {
15
+
16
+        protected IServiceProvider GetServiceProvider()
17
+        {
18
+            var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
19
+            var typeILuObjectConverterDescriptorEnumerable =
20
+                typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
21
+
22
+            var services = new ServiceCollection();
23
+            services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
24
+            services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
25
+            services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
26
+            services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
27
+
28
+            var serviceProvider = services.BuildServiceProvider();
29
+            return serviceProvider;
30
+        }
31
+
32
+        protected LuExpressionConverterOptions GetConverterOptions()
33
+        {
34
+            var options = new LuExpressionConverterOptions
35
+            {
36
+                Parameters = new Dictionary<ParameterExpression, Expression>(),
37
+                Types = new Dictionary<Type, Type>
38
+                {
39
+                    {typeof(TestDbo1), typeof(TestModel1)},
40
+                    {typeof(TestDbo2), typeof(TestModel2)}
41
+                }
42
+            };
43
+            return options;
44
+        }
45
+
46
+        protected LuExpressionConverterVisitor GetConverter()
47
+        {
48
+            return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider());
49
+        }
50
+
51
+        [Fact]
52
+        public void TestSimpleProperty1()
53
+        {
54
+            Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
55
+            Expression<Func<TestModel2, Guid>> expModel = (Param_0 => Param_0.test_model1.id);
56
+            var converter = GetConverter();
57
+            var result = converter.Visit(expDbo);
58
+            Assert.Equal(expModel.ToString(), result?.ToString());
59
+        }
60
+
61
+        [Fact]
62
+        public void TestSimpleProperty2()
63
+        {
64
+            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Id == Guid.Empty);
65
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.id == Guid.Empty);
66
+            var converter = GetConverter();
67
+            var result = converter.Visit(expDbo);
68
+            Assert.Equal(expModel.ToString(), result?.ToString());
69
+        }
70
+
71
+        [Fact]
72
+        public void TestSimpleProperty3()
73
+        {
74
+            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 2);
75
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Length == 2);
76
+            var converter = GetConverter();
77
+            var result = converter.Visit(expDbo);
78
+            Assert.Equal(expModel.ToString(), result?.ToString());
79
+        }
80
+
81
+        [Fact]
82
+        public void TestStaticProperty1()
83
+        {
84
+            Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField);
85
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField);
86
+            var converter = GetConverter();
87
+            var result = converter.Visit(expDbo);
88
+            Assert.Equal(expModel.ToString(), result?.ToString());
89
+        }
90
+
91
+        [Fact]
92
+        public void TestStaticProperty2()
93
+        {
94
+            Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty);
95
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty);
96
+            var converter = GetConverter();
97
+            var result = converter.Visit(expDbo);
98
+            Assert.Equal(expModel.ToString(), result?.ToString());
99
+        }
100
+
101
+        [Fact]
102
+        public void TestVirtualProperty1()
103
+        {
104
+            Expression<Func<TestDbo2, string>> expDbo = (x => x.NameVirtual);
105
+            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.name + " " + Param_0.name);
106
+            var converter = GetConverter();
107
+            var result = converter.Visit(expDbo);
108
+            Assert.Equal(expModel.ToString(), result?.ToString());
109
+        }
110
+
111
+        [Fact]
112
+        public void TestVirtualProperty2()
113
+        {
114
+            Expression<Func<TestDbo2, string>> expDbo = (x => x.Parent.NameVirtual);
115
+            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.parent.name + " " + Param_0.parent.name);
116
+            var converter = GetConverter();
117
+            var result = converter.Visit(expDbo);
118
+            Assert.Equal(expModel.ToString(), result?.ToString());
119
+        }
120
+
121
+        [Fact]
122
+        public void TestStaticValue1()
123
+        {
124
+            Expression<Func<TestDbo2, int>> expDbo = (x => 0);
125
+            Expression<Func<TestModel2, int>> expModel = (Param_0 => 0);
126
+            var converter = GetConverter();
127
+            var result = converter.Visit(expDbo);
128
+            Assert.Equal(expModel.ToString(), result?.ToString());
129
+        }
130
+
131
+        [Fact]
132
+        public void TestStaticValue2()
133
+        {
134
+            Expression<Func<TestDbo2, TestDbo2>> expDbo = (x => x);
135
+            Expression<Func<TestModel2, TestModel2>> expModel = (Param_0 => Param_0);
136
+            var converter = GetConverter();
137
+            var result = converter.Visit(expDbo);
138
+            Assert.Equal(expModel.ToString(), result?.ToString());
139
+        }
140
+
141
+        [Fact]
142
+        public void TestStaticValue3()
143
+        {
144
+            Expression<Func<TestDbo2, bool>> expDbo = (x => StaticDbo.StaticField);
145
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => StaticDbo.StaticField);
146
+            var converter = GetConverter();
147
+            var result = converter.Visit(expDbo);
148
+            Assert.Equal(expModel.ToString(), result?.ToString());
149
+        }
150
+
151
+        [Fact]
152
+        public void TestComplexExpression1()
153
+        {
154
+            Expression<Func<TestDbo2, int>> expDbo = (x => (x.Name + " " + x.Name).Length);
155
+            Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name).Length);
156
+            var converter = GetConverter();
157
+            var result = converter.Visit(expDbo);
158
+            Assert.Equal(expModel.ToString(), result?.ToString());
159
+        }
160
+
161
+        [Fact]
162
+        public void TestComplexExpression2()
163
+        {
164
+            Expression<Func<TestDbo2, int>> expDbo = (x => (x.NameVirtual + " " + x.Name).Length);
165
+            Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name + " " + Param_0.name).Length);
166
+            var converter = GetConverter();
167
+            var result = converter.Visit(expDbo);
168
+            Assert.Equal(expModel.ToString(), result?.ToString());
169
+        }
170
+
171
+        [Fact]
172
+        public void TestSimpleMethodSimpleArg1()
173
+        {
174
+            Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
175
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.name.Contains("s"));
176
+            var converter = GetConverter();
177
+            var result = converter.Visit(expDbo);
178
+            Assert.Equal(expModel.ToString(), result?.ToString());
179
+        }
180
+
181
+        [Fact]
182
+        public void TestSimpleMethodSimpleArg2()
183
+        {
184
+            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
185
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains("s"));
186
+            var converter = GetConverter();
187
+            var result = converter.Visit(expDbo);
188
+            Assert.Equal(expModel.ToString(), result?.ToString());
189
+        }
190
+
191
+        [Fact]
192
+        public void TestSimpleMethodSimpleArg3()
193
+        {
194
+            Expression<Func<TestDbo2, string>> expDbo = (x => x.TestDbo1.ToString());
195
+            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name);
196
+            var converter = GetConverter();
197
+            var result = converter.Visit(expDbo);
198
+            Assert.Equal(expModel.ToString(), result?.ToString());
199
+        }
200
+
201
+        [Fact]
202
+        public void TestSimpleMethodAdvArg1()
203
+        {
204
+            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
205
+            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains(Param_0.name));
206
+            var converter = GetConverter();
207
+            var result = converter.Visit(expDbo);
208
+            Assert.Equal(expModel.ToString(), result?.ToString());
209
+        }
210
+
211
+        [Fact]
212
+        public void TestAdvMethodSimpleArg1()
213
+        {
214
+            Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
215
+            Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any());
216
+            var converter = GetConverter();
217
+            var result = converter.Visit(expDbo);
218
+            Assert.Equal(expModel.ToString(), result?.ToString());
219
+        }
220
+
221
+        [Fact]
222
+        public void TestAdvMethodAdvArg1()
223
+        {
224
+            Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
225
+            Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")));
226
+            var converter = GetConverter();
227
+            var result = converter.Visit(expDbo);
228
+            Assert.Equal(expModel.ToString(), result?.ToString());
229
+        }
230
+    }
231
+}

+ 91
- 0
Luticate2.Auth.Tests/Business/ExpressionConverter/Objects.cs 查看文件

@@ -0,0 +1,91 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq.Expressions;
4
+using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
5
+
6
+namespace Luticate2.Auth.Tests.Business.ExpressionConverter
7
+{
8
+    public static class StaticDbo
9
+    {
10
+        public static bool StaticField;
11
+
12
+        public static bool StaticProperty { get; set; }
13
+    }
14
+
15
+    public class TestDbo1
16
+    {
17
+        public Guid Id { get; set; }
18
+
19
+        public string Name { get; set; }
20
+
21
+        public IList<TestDbo2> TestDbo2s { get; set; }
22
+
23
+        public string ToString()
24
+        {
25
+            throw new NotImplementedException();
26
+        }
27
+    }
28
+
29
+    public class TestDbo2
30
+    {
31
+        public Guid Id { get; set; }
32
+
33
+        public string Name { get; set; }
34
+
35
+        public string NameVirtual { get; set; }
36
+
37
+        public TestDbo1 TestDbo1 { get; set; }
38
+
39
+        public TestDbo2 Parent { get; set; }
40
+
41
+        public TestModel1 Unused { get; set; }
42
+    }
43
+
44
+    public class TestModel1
45
+    {
46
+        public Guid id { get; set; }
47
+
48
+        public string name { get; set; }
49
+
50
+        public ICollection<TestModel2> test_model2 { get; set; }
51
+    }
52
+
53
+    public class TestModel2
54
+    {
55
+        public Guid id { get; set; }
56
+
57
+        public string name { get; set; }
58
+
59
+        public Guid test_model1_id { get; set; }
60
+
61
+        public TestModel1 test_model1 { get; set; }
62
+
63
+        public TestModel2 parent { get; set; }
64
+    }
65
+
66
+    public class LuOcdTest1 : LuObjectConverterDescriptor<TestDbo1, TestModel1>
67
+    {
68
+        public LuOcdTest1()
69
+        {
70
+            AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
71
+            AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
72
+            AddMemberConverter(x => x.TestDbo2s, y => y.test_model2, PropertyType.Object);
73
+            AddStaticMethodConverter(
74
+                (Expression<Func<TestDbo1, string>>)(x => x.ToString()),
75
+                (Expression<Func<TestModel1, string>>)(x => x.id + ": " + x.name)
76
+            );
77
+        }
78
+    }
79
+
80
+    public class LuOcdTest2 : LuObjectConverterDescriptor<TestDbo2, TestModel2>
81
+    {
82
+        public LuOcdTest2()
83
+        {
84
+            AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
85
+            AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
86
+            AddMemberConverter(x => x.NameVirtual, y => y.name + " " + y.name, PropertyType.Value);
87
+            AddMemberConverter(x => x.TestDbo1, y => y.test_model1, PropertyType.Object);
88
+            AddMemberConverter(x => x.Parent, y => y.parent, PropertyType.Object);
89
+        }
90
+    }
91
+}

+ 39
- 182
Luticate2.Auth.Tests/Business/ObjectConverter/ObjectConverterTests.cs 查看文件

@@ -3,7 +3,12 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Linq.Expressions;
5 5
 using Luticate2.Auth.Utils.Business.ExpressionConverter;
6
+using Luticate2.Auth.Utils.Business.Fields;
7
+using Luticate2.Auth.Utils.Business.ObjectConverter;
6 8
 using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
9
+using Luticate2.Auth.Utils.Dbo;
10
+using Luticate2.Auth.Utils.Dbo.Fields;
11
+using Luticate2.Auth.Utils.Dbo.Result;
7 12
 using Luticate2.Auth.Utils.Interfaces;
8 13
 using Microsoft.Extensions.DependencyInjection;
9 14
 using Xunit;
@@ -12,7 +17,6 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
12 17
 {
13 18
     public class ObjectConverterTests
14 19
     {
15
-
16 20
         protected IServiceProvider GetServiceProvider()
17 21
         {
18 22
             var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
@@ -25,6 +29,11 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
25 29
             services.AddSingleton<ILuObjectConverterDescriptor<TestDbo1, TestModel1>, LuOcdTest1>();
26 30
             services.AddSingleton<ILuObjectConverterDescriptor<TestDbo2, TestModel2>, LuOcdTest2>();
27 31
 
32
+
33
+            services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
34
+            services.AddSingleton<ILuObjectConverter<TestModel1, TestDbo1>, LuObjectConverterPoco<TestModel1, TestDbo1>>();
35
+            services.AddSingleton<ILuObjectConverter<TestModel2, TestDbo2>, LuObjectConverterPoco<TestModel2, TestDbo2>>();
36
+
28 37
             var serviceProvider = services.BuildServiceProvider();
29 38
             return serviceProvider;
30 39
         }
@@ -38,194 +47,42 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
38 47
                 {
39 48
                     {typeof(TestDbo1), typeof(TestModel1)},
40 49
                     {typeof(TestDbo2), typeof(TestModel2)}
50
+                },
51
+                Allocator = type =>
52
+                {
53
+                    return Activator.CreateInstance(type);
41 54
                 }
42 55
             };
43 56
             return options;
44 57
         }
45 58
 
46
-        protected LuExpressionConverterVisitor GetConverter()
47
-        {
48
-            return new LuExpressionConverterVisitor(GetConverterOptions(), GetServiceProvider());
49
-        }
50
-
51
-        [Fact]
52
-        public void TestSimpleProperty1()
53
-        {
54
-            Expression<Func<TestDbo2, Guid>> expDbo = (x => x.TestDbo1.Id);
55
-            Expression<Func<TestModel2, Guid>> expModel = (Param_0 => Param_0.test_model1.id);
56
-            var converter = GetConverter();
57
-            var result = converter.Visit(expDbo);
58
-            Assert.Equal(expModel.ToString(), result?.ToString());
59
-        }
60
-
61
-        [Fact]
62
-        public void TestSimpleProperty2()
63
-        {
64
-            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Id == Guid.Empty);
65
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.id == Guid.Empty);
66
-            var converter = GetConverter();
67
-            var result = converter.Visit(expDbo);
68
-            Assert.Equal(expModel.ToString(), result?.ToString());
69
-        }
70
-
71
-        [Fact]
72
-        public void TestSimpleProperty3()
73
-        {
74
-            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Length == 2);
75
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Length == 2);
76
-            var converter = GetConverter();
77
-            var result = converter.Visit(expDbo);
78
-            Assert.Equal(expModel.ToString(), result?.ToString());
79
-        }
80
-
81
-        [Fact]
82
-        public void TestStaticProperty1()
83
-        {
84
-            Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticField);
85
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticField);
86
-            var converter = GetConverter();
87
-            var result = converter.Visit(expDbo);
88
-            Assert.Equal(expModel.ToString(), result?.ToString());
89
-        }
90
-
91
-        [Fact]
92
-        public void TestStaticProperty2()
93
-        {
94
-            Expression<Func<TestDbo2, bool>> expDbo = (x => (x.TestDbo1.Name == "42") == StaticDbo.StaticProperty);
95
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => (Param_0.test_model1.name == "42") == StaticDbo.StaticProperty);
96
-            var converter = GetConverter();
97
-            var result = converter.Visit(expDbo);
98
-            Assert.Equal(expModel.ToString(), result?.ToString());
99
-        }
100
-
101
-        [Fact]
102
-        public void TestVirtualProperty1()
103
-        {
104
-            Expression<Func<TestDbo2, string>> expDbo = (x => x.NameVirtual);
105
-            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.name + " " + Param_0.name);
106
-            var converter = GetConverter();
107
-            var result = converter.Visit(expDbo);
108
-            Assert.Equal(expModel.ToString(), result?.ToString());
109
-        }
110
-
111
-        [Fact]
112
-        public void TestVirtualProperty2()
113
-        {
114
-            Expression<Func<TestDbo2, string>> expDbo = (x => x.Parent.NameVirtual);
115
-            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.parent.name + " " + Param_0.parent.name);
116
-            var converter = GetConverter();
117
-            var result = converter.Visit(expDbo);
118
-            Assert.Equal(expModel.ToString(), result?.ToString());
119
-        }
120
-
121 59
         [Fact]
122
-        public void TestStaticValue1()
60
+        void Test1()
123 61
         {
124
-            Expression<Func<TestDbo2, int>> expDbo = (x => 0);
125
-            Expression<Func<TestModel2, int>> expModel = (Param_0 => 0);
126
-            var converter = GetConverter();
127
-            var result = converter.Visit(expDbo);
128
-            Assert.Equal(expModel.ToString(), result?.ToString());
129
-        }
130
-
131
-        [Fact]
132
-        public void TestStaticValue2()
133
-        {
134
-            Expression<Func<TestDbo2, TestDbo2>> expDbo = (x => x);
135
-            Expression<Func<TestModel2, TestModel2>> expModel = (Param_0 => Param_0);
136
-            var converter = GetConverter();
137
-            var result = converter.Visit(expDbo);
138
-            Assert.Equal(expModel.ToString(), result?.ToString());
139
-        }
140
-
141
-        [Fact]
142
-        public void TestStaticValue3()
143
-        {
144
-            Expression<Func<TestDbo2, bool>> expDbo = (x => StaticDbo.StaticField);
145
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => StaticDbo.StaticField);
146
-            var converter = GetConverter();
147
-            var result = converter.Visit(expDbo);
148
-            Assert.Equal(expModel.ToString(), result?.ToString());
149
-        }
150
-
151
-        [Fact]
152
-        public void TestComplexExpression1()
153
-        {
154
-            Expression<Func<TestDbo2, int>> expDbo = (x => (x.Name + " " + x.Name).Length);
155
-            Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name).Length);
156
-            var converter = GetConverter();
157
-            var result = converter.Visit(expDbo);
158
-            Assert.Equal(expModel.ToString(), result?.ToString());
159
-        }
160
-
161
-        [Fact]
162
-        public void TestComplexExpression2()
163
-        {
164
-            Expression<Func<TestDbo2, int>> expDbo = (x => (x.NameVirtual + " " + x.Name).Length);
165
-            Expression<Func<TestModel2, int>> expModel = (Param_0 => (Param_0.name + " " + Param_0.name + " " + Param_0.name).Length);
166
-            var converter = GetConverter();
167
-            var result = converter.Visit(expDbo);
168
-            Assert.Equal(expModel.ToString(), result?.ToString());
169
-        }
170
-
171
-        [Fact]
172
-        public void TestSimpleMethodSimpleArg1()
173
-        {
174
-            Expression<Func<TestDbo2, bool>> expDbo = (x => x.Name.Contains("s"));
175
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.name.Contains("s"));
176
-            var converter = GetConverter();
177
-            var result = converter.Visit(expDbo);
178
-            Assert.Equal(expModel.ToString(), result?.ToString());
179
-        }
180
-
181
-        [Fact]
182
-        public void TestSimpleMethodSimpleArg2()
183
-        {
184
-            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains("s"));
185
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains("s"));
186
-            var converter = GetConverter();
187
-            var result = converter.Visit(expDbo);
188
-            Assert.Equal(expModel.ToString(), result?.ToString());
189
-        }
190
-
191
-        [Fact]
192
-        public void TestSimpleMethodSimpleArg3()
193
-        {
194
-            Expression<Func<TestDbo2, string>> expDbo = (x => x.TestDbo1.ToString());
195
-            Expression<Func<TestModel2, string>> expModel = (Param_0 => Param_0.test_model1.id + ": " + Param_0.test_model1.name);
196
-            var converter = GetConverter();
197
-            var result = converter.Visit(expDbo);
198
-            Assert.Equal(expModel.ToString(), result?.ToString());
199
-        }
200
-
201
-        [Fact]
202
-        public void TestSimpleMethodAdvArg1()
203
-        {
204
-            Expression<Func<TestDbo2, bool>> expDbo = (x => x.TestDbo1.Name.Contains(x.Name));
205
-            Expression<Func<TestModel2, bool>> expModel = (Param_0 => Param_0.test_model1.name.Contains(Param_0.name));
206
-            var converter = GetConverter();
207
-            var result = converter.Visit(expDbo);
208
-            Assert.Equal(expModel.ToString(), result?.ToString());
209
-        }
210
-
211
-        [Fact]
212
-        public void TestAdvMethodSimpleArg1()
213
-        {
214
-            Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any());
215
-            Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any());
216
-            var converter = GetConverter();
217
-            var result = converter.Visit(expDbo);
218
-            Assert.Equal(expModel.ToString(), result?.ToString());
219
-        }
220
-
221
-        [Fact]
222
-        public void TestAdvMethodAdvArg1()
223
-        {
224
-            Expression<Func<TestDbo1, bool>> expDbo = (x => x.TestDbo2s.Any(y => y.Name.Contains(x.Name + "s")));
225
-            Expression<Func<TestModel1, bool>> expModel = (Param_0 => Param_0.test_model2.Any(Param_1 => Param_1.name.Contains(Param_0.name + "s")));
226
-            var converter = GetConverter();
227
-            var result = converter.Visit(expDbo);
228
-            Assert.Equal(expModel.ToString(), result?.ToString());
62
+            var serviceProvider = GetServiceProvider();
63
+            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
64
+            var model = new TestModel1
65
+            {
66
+                id = Guid.NewGuid(),
67
+                name = "Test.",
68
+                test_model2 = new List<TestModel2>
69
+                {
70
+                    new TestModel2
71
+                    {
72
+                        id = Guid.NewGuid(),
73
+                        name = "bla",
74
+                        parent = null,
75
+                        test_model1 = null,
76
+                        test_model1_id = Guid.Empty
77
+                    }
78
+                }
79
+            };
80
+            var result = converter.Convert(model, LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
81
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
82
+            var dbo = result.Data as TestDbo1;
83
+            Assert.NotNull(dbo);
84
+            Assert.Equal(model.id, dbo.Id);
85
+            Assert.Equal(model.name, dbo.Name);
229 86
         }
230 87
     }
231 88
 }

+ 2
- 2
Luticate2.Auth.Tests/Business/ObjectConverter/Objects.cs 查看文件

@@ -18,7 +18,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
18 18
 
19 19
         public string Name { get; set; }
20 20
 
21
-        public IList<TestDbo2> TestDbo2s { get; set; }
21
+//        public IList<TestDbo2> TestDbo2s { get; set; } // TODO
22 22
 
23 23
         public string ToString()
24 24
         {
@@ -69,7 +69,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
69 69
         {
70 70
             AddMemberConverter(x => x.Id, y => y.id, PropertyType.Value);
71 71
             AddMemberConverter(x => x.Name, y => y.name, PropertyType.Value);
72
-            AddMemberConverter(x => x.TestDbo2s, y => y.test_model2, PropertyType.Object);
72
+//            AddMemberConverter(x => x.TestDbo2s, y => y.test_model2, PropertyType.Object); // TODO
73 73
             AddStaticMethodConverter(
74 74
                 (Expression<Func<TestDbo1, string>>)(x => x.ToString()),
75 75
                 (Expression<Func<TestModel1, string>>)(x => x.id + ": " + x.name)

Luticate2.Auth.Tests/Business/LuExpressionUtilsTests.cs → Luticate2.Auth.Tests/Business/Utils/LuExpressionUtilsTests.cs 查看文件

@@ -1,10 +1,10 @@
1 1
 using System;
2 2
 using System.Linq;
3 3
 using System.Linq.Expressions;
4
-using Luticate2.Auth.Utils.Business;
4
+using Luticate2.Auth.Utils.Business.Utils;
5 5
 using Xunit;
6 6
 
7
-namespace Luticate2.Auth.Tests.Business
7
+namespace Luticate2.Auth.Tests.Business.Utils
8 8
 {
9 9
     public class TestDbo1
10 10
     {

+ 2
- 0
Luticate2.Auth/Utils/Business/ExpressionConverter/LuExpressionConverterOptions.cs 查看文件

@@ -9,5 +9,7 @@ namespace Luticate2.Auth.Utils.Business.ExpressionConverter
9 9
         public IDictionary<ParameterExpression, Expression> Parameters { get; set; }
10 10
 
11 11
         public IDictionary<Type, Type> Types { get; set; }
12
+
13
+        public Func<Type, object> Allocator { get; set; }
12 14
     }
13 15
 }

+ 5
- 6
Luticate2.Auth/Utils/Business/ExpressionConverter/LuExpressionConverterVisitor.cs 查看文件

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Linq.Expressions;
5
+using Luticate2.Auth.Utils.Business.Utils;
5 6
 using Luticate2.Auth.Utils.Dbo;
6 7
 using Luticate2.Auth.Utils.Dbo.Result;
7 8
 using Luticate2.Auth.Utils.Exceptions;
@@ -70,7 +71,7 @@ namespace Luticate2.Auth.Utils.Business.ExpressionConverter
70 71
                     $"Could not find converter descriptor for {typeFrom} => {typeTo}");
71 72
             }
72 73
 
73
-            var valueExpression = descriptor.GetMemberValueExpression(memberExpression.Member, Options.Types);
74
+            var valueExpression = descriptor.GetMemberValueExpression(memberExpression.Member, Options);
74 75
             if (valueExpression == null)
75 76
             {
76 77
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
@@ -88,8 +89,7 @@ namespace Luticate2.Auth.Utils.Business.ExpressionConverter
88 89
             }
89 90
 
90 91
             var visitor = new LuExpressionParamReplaceVisitor(Options);
91
-            var replacedExpression = visitor.Visit(valueExpression.Body);
92
-            newExpression = LuExpressionUtils.GetFromConvert(replacedExpression);
92
+            newExpression = visitor.Visit(valueExpression.Body);
93 93
 
94 94
             if (newExpression != null)
95 95
             {
@@ -109,7 +109,7 @@ namespace Luticate2.Auth.Utils.Business.ExpressionConverter
109 109
                     $"Could not find converter descriptor for {typeFrom} => {typeTo}");
110 110
             }
111 111
 
112
-            var valueExpression = descriptor.GetMethodValueExpression(methodCallExpression.Method, Options.Types);
112
+            var valueExpression = descriptor.GetMethodValueExpression(methodCallExpression.Method, Options);
113 113
             if (valueExpression == null)
114 114
             {
115 115
                 return LuResult<Expression>.Error(LuStatus.InternalError.ToInt(),
@@ -134,8 +134,7 @@ namespace Luticate2.Auth.Utils.Business.ExpressionConverter
134 134
             }
135 135
             Options.Parameters.Add(valueExpression.Parameters[0], newExpression);
136 136
             var visitor = new LuExpressionParamReplaceVisitor(Options);
137
-            var replacedExpression = visitor.Visit(valueExpression.Body);
138
-            newExpression = LuExpressionUtils.GetFromConvert(replacedExpression);
137
+            newExpression = visitor.Visit(valueExpression.Body);
139 138
             Options.Parameters.Remove(valueExpression.Parameters[0]);
140 139
 
141 140
             return LuResult<Expression>.Ok(newExpression);

+ 14
- 0
Luticate2.Auth/Utils/Business/ExpressionConverter/LuObjectConverterIdentity.cs 查看文件

@@ -0,0 +1,14 @@
1
+using Luticate2.Auth.Utils.Dbo.Fields;
2
+using Luticate2.Auth.Utils.Dbo.Result;
3
+using Luticate2.Auth.Utils.Interfaces;
4
+
5
+namespace Luticate2.Auth.Utils.Business.ExpressionConverter
6
+{
7
+    public class LuObjectConverterIdentity : ILuObjectConverterIdentity
8
+    {
9
+        public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, LuExpressionConverterOptions options)
10
+        {
11
+            return LuResult<object>.Ok(srcObj);
12
+        }
13
+    }
14
+}

+ 1
- 0
Luticate2.Auth/Utils/Business/Fields/LuFieldsExtensions.cs 查看文件

@@ -3,6 +3,7 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Linq.Expressions;
5 5
 using System.Text.RegularExpressions;
6
+using Luticate2.Auth.Utils.Business.Utils;
6 7
 using Luticate2.Auth.Utils.Dbo.Fields;
7 8
 
8 9
 namespace Luticate2.Auth.Utils.Business.Fields

+ 68
- 0
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterPoco.cs 查看文件

@@ -0,0 +1,68 @@
1
+using System;
2
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
3
+using Luticate2.Auth.Utils.Business.Utils;
4
+using Luticate2.Auth.Utils.Dbo.Fields;
5
+using Luticate2.Auth.Utils.Dbo.Result;
6
+using Luticate2.Auth.Utils.Interfaces;
7
+using Microsoft.Extensions.DependencyInjection;
8
+
9
+namespace Luticate2.Auth.Utils.Business.ObjectConverter
10
+{
11
+    public class LuObjectConverterPoco<TTypeFrom, TTypeTo> : ILuObjectConverter<TTypeFrom, TTypeTo>
12
+    {
13
+        protected IServiceProvider ServiceProvider { get; }
14
+
15
+        public LuObjectConverterPoco(IServiceProvider serviceProvider)
16
+        {
17
+            ServiceProvider = serviceProvider;
18
+        }
19
+
20
+        protected ILuObjectConverter GetObjectConverter(Type typeFrom, Type typeTo)
21
+        {
22
+            var type = typeof(ILuObjectConverter<,>);
23
+            var gtype = type.MakeGenericType(typeFrom, typeTo);
24
+            var converter = (ILuObjectConverter) ServiceProvider.GetService(gtype);
25
+            if (converter == null && typeFrom == typeTo)
26
+            {
27
+                converter = (ILuObjectConverter) ServiceProvider.GetService(typeof(ILuObjectConverterIdentity));
28
+            }
29
+            return converter;
30
+        }
31
+
32
+        public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, LuExpressionConverterOptions options)
33
+        {
34
+            // TODO Check if this is correct
35
+            if (srcObj == null)
36
+            {
37
+                return LuResult<object>.Ok(default(TTypeTo));
38
+            }
39
+            // TODO Handle descriptor == null
40
+            var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
41
+            var typeTo = typeof(TTypeTo);
42
+            var dstObj = options.Allocator(typeTo);
43
+
44
+            foreach (var memberInfo in typeTo.GetProperties())
45
+            {
46
+                // TODO check if field is included in fields
47
+                // TODO Handle valueExpression == null
48
+                var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options);
49
+                var srcPropDelegate = srcPropExpression.Compile();
50
+                var srcProp = srcPropDelegate.DynamicInvoke(srcObj);
51
+
52
+                var objectConverter = GetObjectConverter(srcProp.GetType(), memberInfo.GetUnderlyingType());
53
+                var dstProp = objectConverter.Convert(srcProp, fields, options);
54
+                if (!dstProp)
55
+                {
56
+                    return dstProp;
57
+                }
58
+                var result = LuExpressionUtils.SetValueFromMember(memberInfo, dstObj, dstProp.Data);
59
+                if (!result)
60
+                {
61
+                    return result.To<object>();
62
+                }
63
+            }
64
+
65
+            return LuResult<object>.Ok(dstObj);
66
+        }
67
+    }
68
+}

+ 9
- 6
Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptor.cs 查看文件

@@ -3,6 +3,8 @@ using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Linq.Expressions;
5 5
 using System.Reflection;
6
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
7
+using Luticate2.Auth.Utils.Business.Utils;
6 8
 using Luticate2.Auth.Utils.Interfaces;
7 9
 
8 10
 namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
@@ -41,7 +43,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
41 43
             var memberInfo = LuExpressionUtils.GetSingleMemberFromExpression(memberFrom);
42 44
             StaticMemberConverters.Add(memberInfo, new LuObjectConverterDescriptorMemberInfo
43 45
             {
44
-                ValueExpression = valueExpression,
46
+                ValueExpression = LuExpressionUtils.GetFromConvert(valueExpression),
45 47
                 PropertyType = propertyType
46 48
             });
47 49
         }
@@ -52,7 +54,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
52 54
             var methodInfo = LuExpressionUtils.GetSingleMethodFromExpression(methodFrom);
53 55
             StaticMethodConverters.Add(methodInfo, new LuObjectConverterDescriptorMethodInfo
54 56
             {
55
-                ValueExpression = valueExpression
57
+                ValueExpression = LuExpressionUtils.GetFromConvert(valueExpression)
56 58
             });
57 59
         }
58 60
 
@@ -79,7 +81,8 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
79 81
                     var methodParams = templateMethodInfo.GetParameters().Select(x => Expression.Parameter(x.ParameterType)).ToList();
80 82
                     lambdaParams.AddRange(methodParams);
81 83
                     var methodCallExpression = Expression.Call(templateMethodInfo.IsStatic ? null : lambdaParams[0], templateMethodInfo, methodParams);
82
-                    var lambda = Expression.Lambda(methodCallExpression, lambdaParams);
84
+                    var body = LuExpressionUtils.GetFromConvert(methodCallExpression);
85
+                    var lambda = Expression.Lambda(body, lambdaParams);
83 86
 
84 87
                     return new LuObjectConverterDescriptorMethodInfo
85 88
                     {
@@ -89,7 +92,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
89 92
             );
90 93
         }
91 94
 
92
-        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, IDictionary<Type, Type> types)
95
+        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, LuExpressionConverterOptions options)
93 96
         {
94 97
             if (StaticMemberConverters.ContainsKey(memberInfo))
95 98
             {
@@ -99,7 +102,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
99 102
             return null;
100 103
         }
101 104
 
102
-        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, IDictionary<Type, Type> types)
105
+        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, LuExpressionConverterOptions options)
103 106
         {
104 107
             if (StaticMethodConverters.ContainsKey(methodInfo))
105 108
             {
@@ -109,7 +112,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
109 112
             var genericMethodInfo = methodInfo.GetGenericMethodDefinition();
110 113
             if (DynamicMethodConverters.ContainsKey(genericMethodInfo))
111 114
             {
112
-                var result = DynamicMethodConverters[genericMethodInfo](methodInfo, types);
115
+                var result = DynamicMethodConverters[genericMethodInfo](methodInfo, options.Types);
113 116
                 if (result != null)
114 117
                 {
115 118
                     return result.ValueExpression;

+ 3
- 3
Luticate2.Auth/Utils/Business/ObjectConverterDescriptor/LuObjectConverterDescriptorIdentity.cs 查看文件

@@ -1,15 +1,15 @@
1
-using System;
2 1
 using System.Collections.Generic;
3 2
 using System.Linq;
4 3
 using System.Linq.Expressions;
5 4
 using System.Reflection;
5
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
6 6
 using Luticate2.Auth.Utils.Interfaces;
7 7
 
8 8
 namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
9 9
 {
10 10
     public class LuObjectConverterDescriptorIdentity : ILuObjectConverterDescriptorIdentity
11 11
     {
12
-        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, IDictionary<Type, Type> types)
12
+        public LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, LuExpressionConverterOptions options)
13 13
         {
14 14
             var isStatic = false;
15 15
             if (memberInfo is FieldInfo fieldInfo)
@@ -26,7 +26,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverterDescriptor
26 26
             return lambda;
27 27
         }
28 28
 
29
-        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, IDictionary<Type, Type> types)
29
+        public LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, LuExpressionConverterOptions options)
30 30
         {
31 31
             var lambdaParams = new List<ParameterExpression>
32 32
             {

+ 1
- 0
Luticate2.Auth/Utils/Business/PartialObjectCopier/LuPartialObjectCopier.cs 查看文件

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.Linq.Expressions;
4 4
 using Luticate2.Auth.Utils.Business.Fields;
5
+using Luticate2.Auth.Utils.Business.Utils;
5 6
 using Luticate2.Auth.Utils.Dbo;
6 7
 using Luticate2.Auth.Utils.Dbo.Fields;
7 8
 using Luticate2.Auth.Utils.Dbo.PartialObjectCopier;

+ 28
- 0
Luticate2.Auth/Utils/Business/Utils/LuCoreExtension.cs 查看文件

@@ -0,0 +1,28 @@
1
+using System;
2
+using System.Reflection;
3
+
4
+namespace Luticate2.Auth.Utils.Business.Utils
5
+{
6
+    public static class LuCoreExtension
7
+    {
8
+        public static Type GetUnderlyingType(this MemberInfo member)
9
+        {
10
+            switch (member.MemberType)
11
+            {
12
+                case MemberTypes.Event:
13
+                    return ((EventInfo)member).EventHandlerType;
14
+                case MemberTypes.Field:
15
+                    return ((FieldInfo)member).FieldType;
16
+                case MemberTypes.Method:
17
+                    return ((MethodInfo)member).ReturnType;
18
+                case MemberTypes.Property:
19
+                    return ((PropertyInfo)member).PropertyType;
20
+                default:
21
+                    throw new ArgumentException
22
+                    (
23
+                        "Input MemberInfo must be if type EventInfo, FieldInfo, MethodInfo, or PropertyInfo"
24
+                    );
25
+            }
26
+        }
27
+    }
28
+}

Luticate2.Auth/Utils/Business/LuExpressionUtils.cs → Luticate2.Auth/Utils/Business/Utils/LuExpressionUtils.cs 查看文件

@@ -6,62 +6,73 @@ using System.Reflection;
6 6
 using Luticate2.Auth.Utils.Dbo;
7 7
 using Luticate2.Auth.Utils.Dbo.Result;
8 8
 
9
-namespace Luticate2.Auth.Utils.Business
9
+namespace Luticate2.Auth.Utils.Business.Utils
10 10
 {
11 11
     public static class LuExpressionUtils
12 12
     {
13 13
         public static Expression GetFromConvert(Expression exp)
14 14
         {
15
-            Expression e = exp;
16 15
             if (exp is LambdaExpression lambdaExpression)
17 16
             {
18
-                e = lambdaExpression.Body;
17
+                return GetFromConvert(lambdaExpression);
19 18
             }
20 19
 
21
-            Expression operand;
22
-            if ((e.NodeType == ExpressionType.Convert ||
23
-                 e.NodeType == ExpressionType.ConvertChecked)
24
-                && e is UnaryExpression)
20
+            if ((exp.NodeType == ExpressionType.Convert ||
21
+                 exp.NodeType == ExpressionType.ConvertChecked)
22
+                && exp is UnaryExpression)
25 23
             {
26
-                operand = ((UnaryExpression) e).Operand;
24
+                return ((UnaryExpression) exp).Operand;
27 25
             }
28 26
             else
29 27
             {
30
-                operand = e;
28
+                return exp;
31 29
             }
32
-            return operand;
30
+        }
31
+
32
+        public static LambdaExpression GetFromConvert(LambdaExpression exp)
33
+        {
34
+            var body = GetFromConvert(exp.Body);
35
+            var lambda = Expression.Lambda(body, exp.Parameters);
36
+
37
+            return lambda;
33 38
         }
34 39
 
35 40
         public static Expression<Func<TType1, TType3>> AddConvert<TType1, TType2, TType3>(Expression<Func<TType1, TType2>> exp)
36 41
         {
37
-            var converted = Expression.Convert(exp, typeof(object));
42
+            var converted = Expression.Convert(exp.Body, typeof(TType3));
38 43
             var expLambda = Expression.Lambda<Func<TType1, TType3>>(converted, exp.Parameters);
39 44
             return expLambda;
40 45
         }
41 46
 
42 47
         public static LuResult<TProperty> SetValueFromExpression<TDbo, TProperty>(Expression<Func<TDbo, TProperty>> property,
43
-            TDbo dboTo, TProperty value)
48
+            TDbo dstObj, TProperty value)
44 49
         {
45 50
             var memberInfo = GetSingleMemberFromExpression(property);
46 51
             if (memberInfo != null)
47 52
             {
48
-                switch (memberInfo.MemberType)
49
-                {
50
-                    case MemberTypes.Field:
51
-                        ((FieldInfo) memberInfo).SetValue(dboTo, value);
52
-                        return LuResult<TProperty>.Ok(value);
53
-                    case MemberTypes.Property:
54
-                        ((PropertyInfo) memberInfo).SetValue(dboTo, value);
55
-                        return LuResult<TProperty>.Ok(value);
56
-                    default:
57
-                        return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(),
58
-                            $"Bad MemberType: {memberInfo.MemberType}");
59
-                }
53
+                return SetValueFromMember(memberInfo, dstObj, value);
60 54
             }
61 55
 
62 56
             return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(), "Bad member expression");
63 57
         }
64 58
 
59
+        public static LuResult<TProperty> SetValueFromMember<TDbo, TProperty>(MemberInfo memberInfo,
60
+            TDbo dstObj, TProperty value)
61
+        {
62
+            switch (memberInfo.MemberType)
63
+            {
64
+                case MemberTypes.Field:
65
+                    ((FieldInfo) memberInfo).SetValue(dstObj, value);
66
+                    return LuResult<TProperty>.Ok(value);
67
+                case MemberTypes.Property:
68
+                    ((PropertyInfo) memberInfo).SetValue(dstObj, value);
69
+                    return LuResult<TProperty>.Ok(value);
70
+                default:
71
+                    return LuResult<TProperty>.Error(LuStatus.InternalError.ToInt(),
72
+                        $"Bad MemberType: {memberInfo.MemberType}");
73
+            }
74
+        }
75
+
65 76
         public static LuResult<TProperty> GetValueFromExpression<TDbo, TProperty>(Expression<Func<TDbo, TProperty>> property,
66 77
             TDbo dboTo)
67 78
         {
@@ -88,7 +99,7 @@ namespace Luticate2.Auth.Utils.Business
88 99
 
89 100
         public static MemberInfo GetSingleMemberFromExpression(LambdaExpression property)
90 101
         {
91
-            if (GetFromConvert(property) is MemberExpression memberExpression &&
102
+            if (GetFromConvert(property.Body) is MemberExpression memberExpression &&
92 103
                 (memberExpression.Expression == property.Parameters.First() ||
93 104
                  (memberExpression.Expression == null && memberExpression.Member.DeclaringType == property.Parameters.First().Type)))
94 105
             {
@@ -100,7 +111,7 @@ namespace Luticate2.Auth.Utils.Business
100 111
         public static MethodInfo GetSingleMethodFromExpression(LambdaExpression method)
101 112
         {
102 113
             // TODO check if something like methodCallExpression.Method.DeclaringType == method.Parameters.First().Type is usefull/required (static/extension methods)
103
-            if (GetFromConvert(method) is MethodCallExpression methodCallExpression &&
114
+            if (GetFromConvert(method.Body) is MethodCallExpression methodCallExpression &&
104 115
                 (methodCallExpression.Object == method.Parameters.First() || methodCallExpression.Object == null))
105 116
             {
106 117
                 return methodCallExpression.Method;

+ 19
- 0
Luticate2.Auth/Utils/Interfaces/ILuObjectConverter.cs 查看文件

@@ -0,0 +1,19 @@
1
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
2
+using Luticate2.Auth.Utils.Dbo.Fields;
3
+using Luticate2.Auth.Utils.Dbo.Result;
4
+
5
+namespace Luticate2.Auth.Utils.Interfaces
6
+{
7
+    public interface ILuObjectConverter
8
+    {
9
+        LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, LuExpressionConverterOptions options);
10
+    }
11
+
12
+    public interface ILuObjectConverter<TTypeFrom, TTypeTo> : ILuObjectConverter
13
+    {
14
+    }
15
+
16
+    public interface ILuObjectConverterIdentity : ILuObjectConverter
17
+    {
18
+    }
19
+}

+ 3
- 4
Luticate2.Auth/Utils/Interfaces/ILuObjectConverterDescriptor.cs 查看文件

@@ -1,15 +1,14 @@
1
-using System;
2
-using System.Collections.Generic;
3 1
 using System.Linq.Expressions;
4 2
 using System.Reflection;
3
+using Luticate2.Auth.Utils.Business.ExpressionConverter;
5 4
 
6 5
 namespace Luticate2.Auth.Utils.Interfaces
7 6
 {
8 7
     public interface ILuObjectConverterDescriptor
9 8
     {
10
-        LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, IDictionary<Type, Type> types);
9
+        LambdaExpression GetMemberValueExpression(MemberInfo memberInfo, LuExpressionConverterOptions options);
11 10
 
12
-        LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, IDictionary<Type, Type> types);
11
+        LambdaExpression GetMethodValueExpression(MethodInfo methodInfo, LuExpressionConverterOptions options);
13 12
     }
14 13
 
15 14
     public interface ILuObjectConverterDescriptor<TTypeFrom, TTypeTo> : ILuObjectConverterDescriptor

Loading…
取消
儲存