瀏覽代碼

Added dstObject in ILuObjectConverter

develop
Robin Thoni 6 年之前
父節點
當前提交
a7afb88e31

+ 9
- 7
Luticate2.Auth.Tests/Business/ObjectConverter/ObjectConverterTests.cs 查看文件

@@ -30,7 +30,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
30 30
             return serviceProvider;
31 31
         }
32 32
 
33
-        protected ILuObjectConverterOptions GetConverterOptions()
33
+        protected ILuObjectConverterOptions GetConverterOptions(IServiceProvider serviceProvider)
34 34
         {
35 35
             var options = new LuConvertersOptions
36 36
             {
@@ -42,7 +42,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
42 42
                     {typeof(TestModel1), typeof(TestDbo1)},
43 43
                     {typeof(TestModel2), typeof(TestDbo2)}
44 44
                 }),
45
-                Allocator = new LuConvertersAllocator()
45
+                Allocator = serviceProvider.GetService<ILuConvertersAllocator>()
46 46
             };
47 47
             return options;
48 48
         }
@@ -61,7 +61,8 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
61 61
                         name = "bla",
62 62
                         parent = null,
63 63
                         test_model1 = null,
64
-                        test_model1_id = Guid.Empty
64
+                        test_model1_id = Guid.Empty,
65
+                        optional_int = 42
65 66
                     }
66 67
                 }
67 68
             };
@@ -73,7 +74,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
73 74
             var serviceProvider = GetServiceProvider();
74 75
             var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
75 76
             var model = GetBasicModel();
76
-            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
77
+            var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("*").Data, GetConverterOptions(serviceProvider));
77 78
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
78 79
             var dbo = result.Data as TestDbo1;
79 80
             Assert.NotNull(dbo);
@@ -86,6 +87,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
86 87
                 Assert.Equal(e.Model.id, e.Dbo.Id);
87 88
                 Assert.Equal(e.Model.name, e.Dbo.Name);
88 89
                 Assert.Equal(e.Model.name + " " + e.Model.name, e.Dbo.NameVirtual);
90
+                Assert.Equal(e.Model.optional_int, e.Dbo.NotOptionalInt);
89 91
                 Assert.Null(e.Dbo.Parent);
90 92
                 Assert.Null(e.Dbo.TestDbo1);
91 93
             }
@@ -97,7 +99,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
97 99
             var serviceProvider = GetServiceProvider();
98 100
             var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
99 101
             var model = GetBasicModel();
100
-            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("Id").Data, GetConverterOptions());
102
+            var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("Id").Data, GetConverterOptions(serviceProvider));
101 103
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
102 104
             var dbo = result.Data as TestDbo1;
103 105
             Assert.NotNull(dbo);
@@ -112,7 +114,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
112 114
             var serviceProvider = GetServiceProvider();
113 115
             var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
114 116
             var model = GetBasicModel();
115
-            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("id").Data, GetConverterOptions());
117
+            var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("id").Data, GetConverterOptions(serviceProvider));
116 118
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
117 119
             var dbo = result.Data as TestDbo1;
118 120
             Assert.NotNull(dbo);
@@ -127,7 +129,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
127 129
             var serviceProvider = GetServiceProvider();
128 130
             var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
129 131
             var model = GetBasicModel();
130
-            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("Id, TestDbo2s(Id, Name)").Data, GetConverterOptions());
132
+            var result = converter.Convert(model, new TestDbo1(), new LuFieldDbo(), LuPartialFieldsParser.Parse("Id, TestDbo2s(Id, Name)").Data, GetConverterOptions(serviceProvider));
131 133
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
132 134
             var dbo = result.Data as TestDbo1;
133 135
             Assert.NotNull(dbo);

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

@@ -40,7 +40,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
40 40
 
41 41
         public TestDbo1 Unused { get; set; }
42 42
 
43
-        public int? OptionalInt { get; set; }
43
+        public int NotOptionalInt { get; set; }
44 44
     }
45 45
 
46 46
     public class TestModel1
@@ -90,7 +90,7 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
90 90
             AddStaticMemberConverter(x => x.NameVirtual, y => y.name + " " + y.name);
91 91
             AddStaticMemberConverter(x => x.TestDbo1, y => y.test_model1);
92 92
             AddStaticMemberConverter(x => x.Parent, y => y.parent);
93
-            AddStaticMemberConverter(x => x.OptionalInt, y => y.optional_int);
93
+            AddStaticMemberConverter(x => x.NotOptionalInt, y => y.optional_int ?? -1);
94 94
             AddNullMemberConverter<TestDbo1>(x => x.Unused);
95 95
         }
96 96
     }

+ 7
- 1
Luticate2.Auth/Utils/Business/Converters/LuConvertersAllocator.cs 查看文件

@@ -16,7 +16,7 @@ namespace Luticate2.Auth.Utils.Business.Converters
16 16
             {typeof(List<>), typeof(List<>)}
17 17
         };
18 18
 
19
-        public object GetInstance(Type type)
19
+        public virtual object GetInstance(Type type)
20 20
         {
21 21
             if (type.IsGenericType)
22 22
             {
@@ -28,6 +28,12 @@ namespace Luticate2.Auth.Utils.Business.Converters
28 28
                     return Activator.CreateInstance(glistType);
29 29
                 }
30 30
             }
31
+
32
+            if (type == typeof(string) || type.IsPrimitive)
33
+            {
34
+                return null;
35
+            }
36
+
31 37
             return Activator.CreateInstance(type);
32 38
         }
33 39
     }

+ 3
- 10
Luticate2.Auth/Utils/Business/Converters/LuConvertersExtensions.cs 查看文件

@@ -1,4 +1,3 @@
1
-using System;
2 1
 using System.Collections.Generic;
3 2
 using System.Collections.ObjectModel;
4 3
 using System.Linq;
@@ -6,6 +5,7 @@ using Luticate2.Auth.Utils.Business.ObjectConverter;
6 5
 using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
7 6
 using Luticate2.Auth.Utils.Interfaces;
8 7
 using Microsoft.Extensions.DependencyInjection;
8
+using Microsoft.Extensions.DependencyInjection.Extensions;
9 9
 
10 10
 namespace Luticate2.Auth.Utils.Business.Converters
11 11
 {
@@ -32,21 +32,14 @@ namespace Luticate2.Auth.Utils.Business.Converters
32 32
                 typeof(IList<>),
33 33
                 typeof(List<>)
34 34
             };
35
-            var listImplemType = new Dictionary<Type, Type>
36
-            {
37
-                {typeof(IEnumerable<>), typeof(LuObjectConverterList)},
38
-                {typeof(ICollection<>), typeof(LuObjectConverterCollection)},
39
-                {typeof(Collection<>), typeof(LuObjectConverterCollection)},
40
-                {typeof(IList<>), typeof(LuObjectConverterList)},
41
-                {typeof(List<>), typeof(LuObjectConverterList)}
42
-            };
43 35
 
36
+            services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
44 37
             services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
45 38
             foreach (var typeFrom in listTypes)
46 39
             {
47 40
                 foreach (var typeTo in listTypes)
48 41
                 {
49
-                    services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), listImplemType[typeTo]);
42
+                    services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists));
50 43
                 }
51 44
             }
52 45
         }

+ 15
- 4
Luticate2.Auth/Utils/Business/Crud/LuEfCrudBusiness.cs 查看文件

@@ -20,15 +20,20 @@ namespace Luticate2.Auth.Utils.Business.Crud
20 20
         where TDbo : class, new()
21 21
     {
22 22
         protected IServiceProvider ServiceProvider { get; }
23
+        protected ILuConvertersAllocator ConvertersAllocator { get; }
23 24
 
24 25
         public LuEfCrudBusiness(IServiceProvider serviceProvider)
25 26
         {
26 27
             ServiceProvider = serviceProvider;
28
+            ConvertersAllocator = ServiceProvider.GetRequiredService<ILuConvertersAllocator>();
27 29
         }
28 30
 
29 31
         protected virtual ILuObjectConverterOptions GetOptions()
30 32
         {
31
-            return new LuConvertersOptions();// TODO
33
+            return new LuConvertersOptions
34
+            {
35
+                Allocator = ConvertersAllocator
36
+            };// TODO
32 37
         }
33 38
 
34 39
         protected virtual LuResult<TModel> ConvertDboToModel(LuPartialFieldsDbo partialInput, TDbo dbo)
@@ -39,7 +44,10 @@ namespace Luticate2.Auth.Utils.Business.Crud
39 44
                 return LuResult<TModel>.Error(LuStatus.InternalError.ToInt(),
40 45
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TDbo).Name}, {typeof(TModel).Name}>");
41 46
             }
42
-            var result = copier.Convert(dbo, new LuFieldDbo(), partialInput, GetOptions());
47
+
48
+            var options = GetOptions();
49
+            var dstObj = options.Allocator.GetInstance(typeof(TModel));
50
+            var result = copier.Convert(dbo, dstObj, new LuFieldDbo(), partialInput, GetOptions());
43 51
             return result.Select(o => o as TModel);
44 52
         }
45 53
 
@@ -51,7 +59,10 @@ namespace Luticate2.Auth.Utils.Business.Crud
51 59
                 return LuResult<TDbo>.Error(LuStatus.InternalError.ToInt(),
52 60
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TModel).Name}, {typeof(TDbo).Name}>");
53 61
             }
54
-            var result = copier.Convert(model, new LuFieldDbo(), partialResponse, GetOptions());
62
+
63
+            var options = GetOptions();
64
+            var dstObj = options.Allocator.GetInstance(typeof(TDbo));
65
+            var result = copier.Convert(model, dstObj, new LuFieldDbo(), partialResponse, GetOptions());
55 66
             return result.Select(o => o as TDbo);
56 67
         }
57 68
 
@@ -89,7 +100,7 @@ namespace Luticate2.Auth.Utils.Business.Crud
89 100
             {
90 101
                 Parameters = new Dictionary<ParameterExpression, Expression>(), // TODO
91 102
                 TypeConverter = null, // TODO
92
-                Allocator = null // TODO
103
+                Allocator = ConvertersAllocator
93 104
             };
94 105
             var converter = new LuExpressionConverterVisitor(options, ServiceProvider);
95 106
             var lamdbaModel = converter.Visit(lamdbaDbo) as Expression<Func<TModel, bool>>;// TODO Handle errors

+ 2
- 2
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterIdentity.cs 查看文件

@@ -6,9 +6,9 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
6 6
 {
7 7
     public class LuObjectConverterIdentity : ILuObjectConverterIdentity
8 8
     {
9
-        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
9
+        public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
10 10
         {
11
-            // TODO Handle path
11
+            // TODO Handle path and dstObj
12 12
             return LuResult<object>.Ok(srcObj);
13 13
         }
14 14
     }

+ 10
- 40
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterLists.cs 查看文件

@@ -1,7 +1,6 @@
1 1
 using System;
2 2
 using System.Collections;
3 3
 using System.Collections.Generic;
4
-using System.Collections.ObjectModel;
5 4
 using Luticate2.Auth.Utils.Dbo;
6 5
 using Luticate2.Auth.Utils.Dbo.Fields;
7 6
 using Luticate2.Auth.Utils.Dbo.Result;
@@ -9,7 +8,7 @@ using Luticate2.Auth.Utils.Interfaces;
9 8
 
10 9
 namespace Luticate2.Auth.Utils.Business.ObjectConverter
11 10
 {
12
-    public abstract class LuObjectConverterLists : ILuObjectConverter
11
+    public class LuObjectConverterLists : ILuObjectConverter
13 12
     {
14 13
         protected IServiceProvider ServiceProvider { get; }
15 14
 
@@ -18,26 +17,21 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
18 17
             ServiceProvider = serviceProvider;
19 18
         }
20 19
 
21
-        protected abstract Type GetListType();
22
-
23
-        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
20
+        public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
24 21
         {
25 22
             if (srcObj is IEnumerable enumerable)
26 23
             {
27
-                var listType = GetListType();
28
-                var listTypeTo = options.TypeConverter.ConvertType(srcObj.GetType().GetGenericArguments()[0]);
29
-                var glistType = listType.MakeGenericType(listTypeTo);
30
-                var collection = options.Allocator.GetInstance(glistType);
31
-                foreach (var obj in enumerable)
24
+                foreach (var subSrcObj in enumerable)
32 25
                 {
33 26
                     object convertedObj = null;
34
-                    if (obj != null)
27
+                    if (subSrcObj != null)
35 28
                     {
36
-                        var typeFrom = obj.GetType();
29
+                        var typeFrom = subSrcObj.GetType();
37 30
                         var typeTo = options.TypeConverter.ConvertType(typeFrom);
38 31
                         // TODO Handle converter == null
39 32
                         var converter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
40
-                        var convertResult = converter.Convert(obj, path, fields, options);
33
+                        var subDstObj = options.Allocator.GetInstance(typeTo);
34
+                        var convertResult = converter.Convert(subSrcObj, subDstObj, path, fields, options);
41 35
                         if (!convertResult)
42 36
                         {
43 37
                             return convertResult;
@@ -46,11 +40,11 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
46 40
                         convertedObj = convertResult.Data;
47 41
                     }
48 42
 
49
-                    var methodInfo = collection.GetType().GetMethod(nameof(ICollection<object>.Add));
50
-                    methodInfo.Invoke(collection, new []{convertedObj});
43
+                    var methodInfo = dstObj.GetType().GetMethod(nameof(ICollection<object>.Add));
44
+                    methodInfo.Invoke(dstObj, new []{convertedObj});
51 45
                 }
52 46
 
53
-                return LuResult<object>.Ok(collection);
47
+                return LuResult<object>.Ok(dstObj);
54 48
             }
55 49
             else
56 50
             {
@@ -58,28 +52,4 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
58 52
             }
59 53
         }
60 54
     }
61
-
62
-    public class LuObjectConverterList : LuObjectConverterLists
63
-    {
64
-        public LuObjectConverterList(IServiceProvider serviceProvider) : base(serviceProvider)
65
-        {
66
-        }
67
-
68
-        protected override Type GetListType()
69
-        {
70
-            return typeof(List<>);
71
-        }
72
-    }
73
-
74
-    public class LuObjectConverterCollection : LuObjectConverterLists
75
-    {
76
-        public LuObjectConverterCollection(IServiceProvider serviceProvider) : base(serviceProvider)
77
-        {
78
-        }
79
-
80
-        protected override Type GetListType()
81
-        {
82
-            return typeof(Collection<>);
83
-        }
84
-    }
85 55
 }

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

@@ -17,7 +17,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
17 17
             ServiceProvider = serviceProvider;
18 18
         }
19 19
 
20
-        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
20
+        public LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
21 21
         {
22 22
             if (srcObj == null)
23 23
             {
@@ -26,26 +26,26 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
26 26
             // TODO Handle descriptor == null
27 27
             var descriptor = ServiceProvider.GetService<ILuObjectConverterDescriptor<TTypeTo, TTypeFrom>>();
28 28
             var typeTo = typeof(TTypeTo);
29
-            var dstObj = options.Allocator.GetInstance(typeTo);
29
+//            var dstObj = options.Allocator.GetInstance(typeTo);
30 30
 
31 31
             foreach (var memberInfo in typeTo.GetProperties())
32 32
             {
33
-                // TODO check if field is included in fields
34 33
                 var memberPath = LuFieldDbo.Make(path).Add(memberInfo.Name);
35 34
                 if (!fields.Fields.IsIncluded(memberPath))
36 35
                 {
37 36
                     continue;
38 37
                 }
39 38
                 // TODO Handle srcPropExpression == null
40
-                var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
41
-                var srcPropDelegate = srcPropExpression.Compile();
42
-                var srcProp = srcPropDelegate.DynamicInvoke(srcObj);
43
-                var srcType = srcProp != null ? srcProp.GetType() : srcPropExpression.ReturnType;
44
-                var dstType = memberInfo.GetUnderlyingType();
39
+                var subSrcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
40
+                var subSrcPropDelegate = subSrcPropExpression.Compile();
41
+                var subSrcProp = subSrcPropDelegate.DynamicInvoke(srcObj);
42
+                var subSrcType = subSrcProp != null ? subSrcProp.GetType() : subSrcPropExpression.ReturnType;
43
+                var subDstType = memberInfo.GetUnderlyingType();
45 44
 
46 45
                 // TODO Handle objectConverter == null
47
-                var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, srcType, dstType);
48
-                var dstPropResult = objectConverter.Convert(srcProp, memberPath, fields, options);
46
+                var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, subSrcType, subDstType);
47
+                var subDstObj = options.Allocator.GetInstance(subDstType);
48
+                var dstPropResult = objectConverter.Convert(subSrcProp, subDstObj, memberPath, fields, options);
49 49
                 if (!dstPropResult)
50 50
                 {
51 51
                     return dstPropResult;

+ 1
- 2
Luticate2.Auth/Utils/Interfaces/ILuObjectConverter.cs 查看文件

@@ -14,8 +14,7 @@ namespace Luticate2.Auth.Utils.Interfaces
14 14
 
15 15
     public interface ILuObjectConverter
16 16
     {
17
-        // TODO add dstObject
18
-        LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options);
17
+        LuResult<object> Convert(object srcObj, object dstObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options);
19 18
     }
20 19
 
21 20
     public interface ILuObjectConverter<TTypeFrom, TTypeTo> : ILuObjectConverter

Loading…
取消
儲存