Browse Source

using Configure method for LuUtilsOptions; added transactions to LuEfDataAccess; updated readme; tests

tags/v0.3.0
Robin Thoni 8 years ago
parent
commit
641703d253

+ 4
- 2
Luticate2.Auth/Controllers/LuAuthExtensions.cs View File

@@ -1,7 +1,9 @@
1
-using Luticate2.Auth.Business;
1
+using System;
2
+using Luticate2.Auth.Business;
2 3
 using Luticate2.Auth.DataAccess;
3 4
 using Luticate2.Auth.Middlewares;
4 5
 using Luticate2.Utils.Controllers;
6
+using Luticate2.Utils.Dbo.Basic;
5 7
 using Microsoft.AspNetCore.Builder;
6 8
 using Microsoft.AspNetCore.Http;
7 9
 using Microsoft.Extensions.DependencyInjection;
@@ -10,7 +12,7 @@ namespace Luticate2.Auth.Controllers
10 12
 {
11 13
     public static class LuAuthExtensions
12 14
     {
13
-        public static IServiceCollection AddLuticateAuth(this IServiceCollection services, LuUtilsExtensions.LuOptionsDelegate optionsDelegate)
15
+        public static IServiceCollection AddLuticateAuth(this IServiceCollection services, Action<LuUtilsOptionsDbo> optionsDelegate)
14 16
         {
15 17
             services.AddLuticateUtils(optionsDelegate);
16 18
 

+ 3
- 1
Luticate2.Auth/Controllers/LuGroupsController.cs View File

@@ -1,6 +1,8 @@
1 1
 using Luticate2.Auth.Business;
2 2
 using Luticate2.Utils.Controllers;
3
+using Luticate2.Utils.Dbo.Basic;
3 4
 using Microsoft.AspNetCore.Mvc;
5
+using Microsoft.Extensions.Options;
4 6
 
5 7
 namespace Luticate2.Auth.Controllers
6 8
 {
@@ -9,7 +11,7 @@ namespace Luticate2.Auth.Controllers
9 11
     {
10 12
         private readonly LuGroupsBusiness _luGroupsBusiness;
11 13
 
12
-        public LuGroupsController(LuGroupsBusiness luGroupsBusiness)
14
+        public LuGroupsController(LuGroupsBusiness luGroupsBusiness, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
13 15
         {
14 16
             _luGroupsBusiness = luGroupsBusiness;
15 17
         }

+ 6
- 0
Luticate2.Auth/Controllers/LuUsersController.cs View File

@@ -1,11 +1,17 @@
1 1
 using Luticate2.Utils.Controllers;
2
+using Luticate2.Utils.Dbo.Basic;
2 3
 using Microsoft.AspNetCore.Mvc;
4
+using Microsoft.Extensions.Options;
3 5
 
4 6
 namespace Luticate2.Auth.Controllers
5 7
 {
6 8
     [Route("luticate/users")]
7 9
     public class LuUsersController : LuController
8 10
     {
11
+        public LuUsersController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
12
+        {
13
+        }
14
+
9 15
         public string Get()
10 16
         {
11 17
             return "users";

+ 1
- 1
Luticate2.Auth/project.json View File

@@ -4,7 +4,7 @@
4 4
         "debugType": "portable"
5 5
     },
6 6
     "dependencies": {
7
-        "Luticate2.Utils": "0.1.*"
7
+        "Luticate2.Utils": "0.2.*"
8 8
     },
9 9
     "frameworks": {
10 10
         "netcoreapp1.0": {

+ 8
- 2
Luticate2.Utils/Controllers/LuController.cs View File

@@ -2,14 +2,20 @@
2 2
 using Luticate2.Utils.Dbo.Basic;
3 3
 using Luticate2.Utils.Dbo.Result;
4 4
 using Microsoft.AspNetCore.Mvc;
5
+using Microsoft.Extensions.Options;
5 6
 
6 7
 namespace Luticate2.Utils.Controllers
7 8
 {
8 9
     public abstract class LuController : Controller
9 10
     {
10
-
11
+        protected readonly LuUtilsOptionsDbo LuUtilsOptionsDbo;
11 12
         protected IDictionary<object, object> LuItems => HttpContext.GetLuItems();
12 13
 
14
+        protected LuController(IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo)
15
+        {
16
+            LuUtilsOptionsDbo = luUtilsOptionsDbo.Value;
17
+        }
18
+
13 19
         protected virtual LuApiWrapperDbo<T> Handle<T>(LuResult<T> result)
14 20
         {
15 21
             if (result)
@@ -19,7 +25,7 @@ namespace Luticate2.Utils.Controllers
19 25
                     code = 200,
20 26
                     data = result.Data,
21 27
                     message = null,
22
-                    version = LuUtilsExtensions.Options.Version
28
+                    version = LuUtilsOptionsDbo.Version
23 29
                 };
24 30
             }
25 31
             throw new LuResultException(result.To<object>());

+ 2
- 1
Luticate2.Utils/Controllers/LuCrudController.cs View File

@@ -5,6 +5,7 @@ using Luticate2.Utils.Dbo.OrderBy;
5 5
 using Luticate2.Utils.Dbo.PaginatedRequest;
6 6
 using Luticate2.Utils.Interfaces;
7 7
 using Microsoft.AspNetCore.Mvc;
8
+using Microsoft.Extensions.Options;
8 9
 
9 10
 namespace Luticate2.Utils.Controllers
10 11
 {
@@ -16,7 +17,7 @@ namespace Luticate2.Utils.Controllers
16 17
     {
17 18
         protected readonly TBusiness Busines;
18 19
 
19
-        protected LuCrudController(TBusiness busines)
20
+        protected LuCrudController(TBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(luUtilsOptionsDbo)
20 21
         {
21 22
             Busines = busines;
22 23
         }

+ 6
- 8
Luticate2.Utils/Controllers/LuUtilsExtensions.cs View File

@@ -1,5 +1,7 @@
1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
2 3
 using Luticate2.Utils.Business;
4
+using Luticate2.Utils.DataAccess;
3 5
 using Luticate2.Utils.DataAccess.Npgsql;
4 6
 using Luticate2.Utils.Dbo.Basic;
5 7
 using Luticate2.Utils.Dbo.Filter;
@@ -20,11 +22,7 @@ namespace Luticate2.Utils.Controllers
20 22
 {
21 23
     public static class LuUtilsExtensions
22 24
     {
23
-        public delegate void LuOptionsDelegate(LuOptionsDbo options);
24
-
25
-        public static LuOptionsDbo Options { get; private set; }
26
-
27
-        public static IServiceCollection AddLuticateUtils(this IServiceCollection services, LuOptionsDelegate optionsDelegate)
25
+        public static IServiceCollection AddLuticateUtils(this IServiceCollection services, Action<LuUtilsOptionsDbo> optionsDelegate)
28 26
         {
29 27
             var settings = new JsonSerializerSettings();
30 28
             settings.ContractResolver = new SignalRContractResolver();
@@ -39,10 +37,10 @@ namespace Luticate2.Utils.Controllers
39 37
                 options.Hubs.EnableDetailedErrors = true;
40 38
             });
41 39
 
40
+            services.AddScoped<LuEfTransactionScope>();
42 41
             services.AddSingleton<LuHubConnectionTracker>();
43 42
             services.AddSingleton<LuNotificationsBusiness>();
44
-            Options = new LuOptionsDbo();
45
-            optionsDelegate(Options);
43
+            services.Configure(optionsDelegate);
46 44
             return services;
47 45
         }
48 46
 

+ 32
- 3
Luticate2.Utils/DataAccess/LuEfDataAccess.cs View File

@@ -1,8 +1,10 @@
1 1
 using System;
2 2
 using System.Collections.Generic;
3
+using System.Data;
3 4
 using System.Linq.Expressions;
4 5
 using Luticate2.Utils.Dbo.Result;
5 6
 using Microsoft.EntityFrameworkCore;
7
+using Microsoft.Extensions.DependencyInjection;
6 8
 
7 9
 namespace Luticate2.Utils.DataAccess
8 10
 {
@@ -12,6 +14,8 @@ namespace Luticate2.Utils.DataAccess
12 14
     {
13 15
         private readonly IServiceProvider _serviceProvider;
14 16
 
17
+        protected LuEfTransactionScope TransactionScope => _serviceProvider.GetService<LuEfTransactionScope>();
18
+
15 19
         protected LuEfDataAccess(IServiceProvider serviceProvider)
16 20
         {
17 21
             _serviceProvider = serviceProvider;
@@ -23,11 +27,15 @@ namespace Luticate2.Utils.DataAccess
23 27
         {
24 28
             try
25 29
             {
26
-                using (var db = (TDbContext)_serviceProvider.GetService(typeof(TDbContext)))
30
+                var transactedDb = TransactionScope.GetTransactedDbContext<TDbContext>();
31
+                if (transactedDb == null)
27 32
                 {
28
-                    var table = GetTable(db);
29
-                    return func(db, table);
33
+                    using (var db = _serviceProvider.GetService<TDbContext>())
34
+                    {
35
+                        return func(db, GetTable(db));
36
+                    }
30 37
                 }
38
+                return func(transactedDb, GetTable(transactedDb));
31 39
             }
32 40
             catch (Exception e)
33 41
             {
@@ -47,5 +55,26 @@ namespace Luticate2.Utils.DataAccess
47 55
             return Expression.Lambda<Func<TModel, bool>>(totalExp, param);
48 56
         }
49 57
 
58
+        public bool BeginTransaction(TDbContext context, IsolationLevel? level = null)
59
+        {
60
+            return TransactionScope.BeginTransaction(context, level);
61
+        }
62
+
63
+        public void CommitTransaction(bool transact)
64
+        {
65
+            if (transact)
66
+            {
67
+                TransactionScope.CommitTransaction<TDbContext>();
68
+            }
69
+        }
70
+
71
+        public void RollbackTransaction(bool transact)
72
+        {
73
+            if (transact)
74
+            {
75
+                TransactionScope.RollbackTransaction<TDbContext>();
76
+            }
77
+        }
78
+
50 79
     }
51 80
 }

+ 89
- 0
Luticate2.Utils/DataAccess/LuEfTransactionScope.cs View File

@@ -0,0 +1,89 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Data;
4
+using Microsoft.EntityFrameworkCore;
5
+using Microsoft.Extensions.DependencyInjection;
6
+
7
+namespace Luticate2.Utils.DataAccess
8
+{
9
+    public class LuEfTransactionScope
10
+    {
11
+        protected IDictionary<Type, DbContext> Contexts;
12
+
13
+        private readonly IServiceProvider _serviceProvider;
14
+
15
+        public LuEfTransactionScope(IServiceProvider serviceProvider)
16
+        {
17
+            _serviceProvider = serviceProvider;
18
+            Contexts = new Dictionary<Type, DbContext>();
19
+        }
20
+
21
+        public TDbContext GetTransactedDbContext<TDbContext>()
22
+            where TDbContext : DbContext
23
+        {
24
+            if (Contexts.ContainsKey(typeof(TDbContext)))
25
+            {
26
+                return (TDbContext) Contexts[typeof(TDbContext)];
27
+            }
28
+            return null;
29
+        }
30
+
31
+        public bool BeginTransaction<TDbContext>(TDbContext dbContext, IsolationLevel? level = null)
32
+            where TDbContext : DbContext
33
+        {
34
+            if (dbContext == null)
35
+            {
36
+                dbContext = GetTransactedDbContext<TDbContext>();
37
+                if (dbContext == null)
38
+                {
39
+                    dbContext = _serviceProvider.GetService<TDbContext>();
40
+                }
41
+            }
42
+            if (!Contexts.Contains(new KeyValuePair<Type, DbContext>(typeof(TDbContext), dbContext)))
43
+            {
44
+                Contexts.Add(typeof(TDbContext), dbContext);
45
+            }
46
+
47
+            if (dbContext.Database.CurrentTransaction != null)
48
+            {
49
+                return false;
50
+            }
51
+
52
+            if (level == null)
53
+            {
54
+                dbContext.Database.BeginTransaction();
55
+            }
56
+            else
57
+            {
58
+                dbContext.Database.BeginTransaction(level.Value);
59
+            }
60
+            return true;
61
+        }
62
+
63
+        public void CommitTransaction<TDbContext>()
64
+            where TDbContext : DbContext
65
+        {
66
+            try
67
+            {
68
+                GetTransactedDbContext<TDbContext>().Database.CommitTransaction();
69
+            }
70
+            finally
71
+            {
72
+                Contexts.Remove(typeof(TDbContext));
73
+            }
74
+        }
75
+
76
+        public void RollbackTransaction<TDbContext>()
77
+            where TDbContext : DbContext
78
+        {
79
+            try
80
+            {
81
+                GetTransactedDbContext<TDbContext>().Database.RollbackTransaction();
82
+            }
83
+            finally
84
+            {
85
+                Contexts.Remove(typeof(TDbContext));
86
+            }
87
+        }
88
+    }
89
+}

Luticate2.Utils/Dbo/Basic/LuOptionsDbo.cs → Luticate2.Utils/Dbo/Basic/LuUtilsOptionsDbo.cs View File

@@ -1,6 +1,6 @@
1 1
 namespace Luticate2.Utils.Dbo.Basic
2 2
 {
3
-    public class LuOptionsDbo
3
+    public class LuUtilsOptionsDbo
4 4
     {
5 5
         public string Version { get; set; }
6 6
     }

+ 6
- 3
Luticate2.Utils/Middlewares/LuExceptionMiddleware.cs View File

@@ -4,6 +4,7 @@ using Luticate2.Utils.Controllers;
4 4
 using Luticate2.Utils.Dbo.Basic;
5 5
 using Microsoft.AspNetCore.Http;
6 6
 using Microsoft.Extensions.Logging;
7
+using Microsoft.Extensions.Options;
7 8
 using Newtonsoft.Json;
8 9
 
9 10
 namespace Luticate2.Utils.Middlewares
@@ -12,11 +13,13 @@ namespace Luticate2.Utils.Middlewares
12 13
     {
13 14
         private readonly RequestDelegate _next;
14 15
         private readonly ILogger<LuExceptionMiddleware> _logger;
16
+        private readonly LuUtilsOptionsDbo _luUtilsOptionsDbo;
15 17
 
16
-        public LuExceptionMiddleware(RequestDelegate next, ILogger<LuExceptionMiddleware> logger)
18
+        public LuExceptionMiddleware(RequestDelegate next, ILogger<LuExceptionMiddleware> logger, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo)
17 19
         {
18 20
             _next = next;
19 21
             _logger = logger;
22
+            _luUtilsOptionsDbo = luUtilsOptionsDbo.Value;
20 23
         }
21 24
 
22 25
         public async Task Invoke(HttpContext context)
@@ -36,7 +39,7 @@ namespace Luticate2.Utils.Middlewares
36 39
                     code = e.Result.GetHttpCode(),
37 40
                     data = null,
38 41
                     message = e.Result.PublicDetails ?? e.Result.GetHttpString(),
39
-                    version = LuUtilsExtensions.Options.Version
42
+                    version = _luUtilsOptionsDbo.Version
40 43
                 })).ConfigureAwait(false);
41 44
             }
42 45
             catch (Exception e)
@@ -50,7 +53,7 @@ namespace Luticate2.Utils.Middlewares
50 53
                     code = 500,
51 54
                     data = null,
52 55
                     message = "Internal Error",
53
-                    version = LuUtilsExtensions.Options.Version
56
+                    version = _luUtilsOptionsDbo.Version
54 57
                 })).ConfigureAwait(false);
55 58
             }
56 59
         }

+ 3
- 2
Luticate2.Utils/README.md View File

@@ -48,7 +48,8 @@ public void ConfigureServices(IServiceCollection services)
48 48
     services.AddLuticateUtils(options => options.Version = "dev");
49 49
 
50 50
     // ...
51
-
51
+    
52
+    //The data access classes using YourDbContext need to be added as scoped
52 53
     services.AddDbContext<YourDbContext>(options => // Replace YourDbContext with your own database context
53 54
     {
54 55
         options.UseNpgsql(Configuration.GetConnectionString("default"));
@@ -56,7 +57,7 @@ public void ConfigureServices(IServiceCollection services)
56 57
             .AddEntityFrameworkNpgsqlLuticate() // MUST be before AddEntityFrameworkNpgsql()
57 58
             .AddEntityFrameworkNpgsql()
58 59
             .BuildServiceProvider());
59
-    });
60
+    }, ServiceLifetime.Transient);
60 61
 
61 62
     services.AddMvc()
62 63
         .AddLuticateUtils();

+ 40
- 0
TestUtils/DataAccess/LuUtilsPkGuidDataAccess.cs View File

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using System.Collections.Generic;
2 3
 using System.Linq;
3 4
 using System.Linq.Expressions;
4 5
 using Luticate2.Utils.DataAccess;
@@ -58,5 +59,44 @@ namespace TestUtils.DataAccess
58 59
         {
59 60
             return Execute((db, table) => LuResult<bool>.Ok(table.Any(guid => guid.some_text == someText)));
60 61
         }
62
+
63
+        public override LuResult<T> Add<T>(IEnumerable<PkGuidAddDbo> objs, Func<IEnumerable<PkGuidDbo>, T> returnFunc)
64
+        {
65
+            IList<pk_guid> models = new List<pk_guid>();
66
+            var addRes = Execute((db, table) =>
67
+            {
68
+                var transact = BeginTransaction(db);
69
+                foreach (var dbo in objs)
70
+                {
71
+                    var quoteRequest = GetModelFromTCreate(dbo);
72
+                    models.Add(quoteRequest);
73
+                    table.Add(quoteRequest);
74
+                }
75
+                db.SaveChanges();
76
+                foreach (var model in models)
77
+                {
78
+                    db.Entry(model).State = EntityState.Detached;
79
+                }
80
+                foreach (var pkGuid in models)
81
+                {
82
+                    if (pkGuid.some_int == 2424)
83
+                    {
84
+                        throw new Exception("Test unexpected db error");
85
+                    }
86
+                    if (pkGuid.some_int == 4242)
87
+                    {
88
+                        RollbackTransaction(transact);
89
+                        return LuResult<T>.Error(LuStatus.DbError, "Some expected error", "");
90
+                    }
91
+                }
92
+                CommitTransaction(transact);
93
+                return LuResult<T>.Ok(default(T));
94
+            });
95
+            if (!addRes)
96
+            {
97
+                return addRes;
98
+            }
99
+            return GetMultiple(models, returnFunc);
100
+        }
61 101
     }
62 102
 }

+ 14
- 29
TestUtils/EfCrudDataAccess/LuEfCreateDataAccessTest.cs View File

@@ -15,7 +15,7 @@ namespace TestUtils.EfCrudDataAccess
15 15
         [Fact]
16 16
         public void TestAddMultiple1()
17 17
         {
18
-            Tests.TestRealDb(context =>
18
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
19 19
             {
20 20
                 var dbos = new List<PkBigSerialAddDbo>
21 21
                 {
@@ -30,7 +30,6 @@ namespace TestUtils.EfCrudDataAccess
30 30
                         SomeText = "24"
31 31
                     }
32 32
                 };
33
-                var service = new LuUtilsPkBigSerialDataAccess(context);
34 33
                 var res = service.Add(dbos, enumerable => enumerable);
35 34
                 Assert.Equal(LuStatus.Success, res.Status);
36 35
                 foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
@@ -54,7 +53,7 @@ namespace TestUtils.EfCrudDataAccess
54 53
         [Fact]
55 54
         public void TestAddMultiple2()
56 55
         {
57
-            Tests.TestRealDb(context =>
56
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
58 57
             {
59 58
                 var dbos = new List<PkBigSerialAddDbo>
60 59
                 {
@@ -69,7 +68,6 @@ namespace TestUtils.EfCrudDataAccess
69 68
                         SomeText = "24"
70 69
                     }
71 70
                 };
72
-                var service = new LuUtilsPkBigSerialDataAccess(context);
73 71
                 var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.Id));
74 72
                 Assert.Equal(LuStatus.Success, res.Status);
75 73
                 foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
@@ -91,7 +89,7 @@ namespace TestUtils.EfCrudDataAccess
91 89
         [Fact]
92 90
         public void TestAddMultiple3()
93 91
         {
94
-            Tests.TestRealDb(context =>
92
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
95 93
             {
96 94
                 var dbos = new List<PkBigSerialAddDbo>
97 95
                 {
@@ -106,7 +104,6 @@ namespace TestUtils.EfCrudDataAccess
106 104
                         SomeText = "24"
107 105
                     }
108 106
                 };
109
-                var service = new LuUtilsPkBigSerialDataAccess(context);
110 107
                 var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
111 108
                 Assert.Equal(LuStatus.Success, res.Status);
112 109
                 foreach (var dbo in res.Data)
@@ -119,7 +116,7 @@ namespace TestUtils.EfCrudDataAccess
119 116
         [Fact]
120 117
         public void TestAddMultiple4()
121 118
         {
122
-            Tests.TestRealDb(context =>
119
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
123 120
             {
124 121
                 var dbos = new List<PkBigSerialAddDbo>
125 122
                 {
@@ -134,7 +131,6 @@ namespace TestUtils.EfCrudDataAccess
134 131
                         SomeText = "42"
135 132
                     }
136 133
                 };
137
-                var service = new LuUtilsPkBigSerialDataAccess(context);
138 134
                 var res = service.Add(dbos, enumerable => enumerable.Select(dbo => dbo.CreatedAt));
139 135
                 Assert.Equal(LuStatus.DbError, res.Status);
140 136
             });
@@ -144,9 +140,8 @@ namespace TestUtils.EfCrudDataAccess
144 140
         [Fact]
145 141
         public void TestAddSingle1()
146 142
         {
147
-            Tests.TestRealDb(context =>
143
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
148 144
             {
149
-                var service = new LuUtilsPkBigSerialDataAccess(context);
150 145
                 var res = service.Add(new PkBigSerialAddDbo
151 146
                 {
152 147
                     SomeInt = 42,
@@ -168,9 +163,8 @@ namespace TestUtils.EfCrudDataAccess
168 163
         [Fact]
169 164
         public void TestAddSingle2()
170 165
         {
171
-            Tests.TestRealDb(context =>
166
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
172 167
             {
173
-                var service = new LuUtilsPkBigSerialDataAccess(context);
174 168
                 var res = service.Add(new PkBigSerialAddDbo
175 169
                 {
176 170
                     SomeInt = 42,
@@ -190,9 +184,8 @@ namespace TestUtils.EfCrudDataAccess
190 184
         [Fact]
191 185
         public void TestAddSingle3()
192 186
         {
193
-            Tests.TestRealDb(context =>
187
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
194 188
             {
195
-                var service = new LuUtilsPkBigSerialDataAccess(context);
196 189
                 var res = service.Add(new PkBigSerialAddDbo
197 190
                 {
198 191
                     SomeInt = 42,
@@ -206,7 +199,7 @@ namespace TestUtils.EfCrudDataAccess
206 199
         [Fact]
207 200
         public void TestAddGuidMultiple1()
208 201
         {
209
-            Tests.TestRealDb(context =>
202
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
210 203
             {
211 204
                 var dbos = new List<PkGuidAddDbo>
212 205
                 {
@@ -221,7 +214,6 @@ namespace TestUtils.EfCrudDataAccess
221 214
                         SomeText = "24"
222 215
                     }
223 216
                 };
224
-                var service = new LuUtilsPkGuidDataAccess(context);
225 217
                 var res = service.AddId(dbos);
226 218
                 Assert.Equal(LuStatus.Success, res.Status);
227 219
                 foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkGuidAddDbo, string>(dbo, serialDbo)))
@@ -244,9 +236,8 @@ namespace TestUtils.EfCrudDataAccess
244 236
         [Fact]
245 237
         public void TestAddGuidSingle1()
246 238
         {
247
-            Tests.TestRealDb(context =>
239
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
248 240
             {
249
-                var service = new LuUtilsPkGuidDataAccess(context);
250 241
                 var res = service.AddId(new PkGuidAddDbo
251 242
                 {
252 243
                     SomeInt = 42,
@@ -266,7 +257,7 @@ namespace TestUtils.EfCrudDataAccess
266 257
         [Fact]
267 258
         public void TestAddIdMultiple1()
268 259
         {
269
-            Tests.TestRealDb(context =>
260
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
270 261
             {
271 262
                 var dbos = new List<PkBigSerialAddDbo>
272 263
                 {
@@ -281,7 +272,6 @@ namespace TestUtils.EfCrudDataAccess
281 272
                         SomeText = "24"
282 273
                     }
283 274
                 };
284
-                var service = new LuUtilsPkBigSerialDataAccess(context);
285 275
                 var res = service.AddId(dbos);
286 276
                 Assert.Equal(LuStatus.Success, res.Status);
287 277
                 foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, long>(dbo, serialDbo)))
@@ -304,9 +294,8 @@ namespace TestUtils.EfCrudDataAccess
304 294
         [Fact]
305 295
         public void TestAddIdSingle1()
306 296
         {
307
-            Tests.TestRealDb(context =>
297
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
308 298
             {
309
-                var service = new LuUtilsPkBigSerialDataAccess(context);
310 299
                 var res = service.AddId(new PkBigSerialAddDbo
311 300
                 {
312 301
                     SomeInt = 42,
@@ -326,7 +315,7 @@ namespace TestUtils.EfCrudDataAccess
326 315
         [Fact]
327 316
         public void TestAddDboMultiple1()
328 317
         {
329
-            Tests.TestRealDb(context =>
318
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
330 319
             {
331 320
                 var dbos = new List<PkBigSerialAddDbo>
332 321
                 {
@@ -341,7 +330,6 @@ namespace TestUtils.EfCrudDataAccess
341 330
                         SomeText = "24"
342 331
                     }
343 332
                 };
344
-                var service = new LuUtilsPkBigSerialDataAccess(context);
345 333
                 var res = service.AddDbo(dbos);
346 334
                 Assert.Equal(LuStatus.Success, res.Status);
347 335
                 foreach (var dbo in dbos.Zip(res.Data, (dbo, serialDbo) => new KeyValuePair<PkBigSerialAddDbo, PkBigSerialDbo>(dbo, serialDbo)))
@@ -364,9 +352,8 @@ namespace TestUtils.EfCrudDataAccess
364 352
         [Fact]
365 353
         public void TestAddDboSingle1()
366 354
         {
367
-            Tests.TestRealDb(context =>
355
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
368 356
             {
369
-                var service = new LuUtilsPkBigSerialDataAccess(context);
370 357
                 var res = service.AddDbo(new PkBigSerialAddDbo
371 358
                 {
372 359
                     SomeInt = 42,
@@ -387,10 +374,8 @@ namespace TestUtils.EfCrudDataAccess
387 374
         [Fact]
388 375
         public void TestAddDboSingleFk1()
389 376
         {
390
-            Tests.TestRealDb(context =>
377
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess, LuUtilsFkPkGuidDataAccess>((pkGuidDataAccess, fkPkGuidDataAccess) =>
391 378
             {
392
-                var pkGuidDataAccess = new LuUtilsPkGuidDataAccess(context);
393
-                var fkPkGuidDataAccess = new LuUtilsFkPkGuidDataAccess(context);
394 379
                 var pkGuidDbo = pkGuidDataAccess.AddDbo(new PkGuidAddDbo
395 380
                 {
396 381
                     SomeInt = 42,

+ 11
- 22
TestUtils/EfCrudDataAccess/LuEfDeleteDataAccessTest.cs View File

@@ -13,7 +13,7 @@ namespace TestUtils.EfCrudDataAccess
13 13
         [Fact]
14 14
         public void TestDeleteMultiple1()
15 15
         {
16
-            Tests.TestRealDb(context =>
16
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
17 17
             {
18 18
                 var dbos = new List<PkGuidAddDbo>
19 19
                 {
@@ -33,7 +33,6 @@ namespace TestUtils.EfCrudDataAccess
33 33
                         SomeText = "24"
34 34
                     }
35 35
                 };
36
-                var service = new LuUtilsPkGuidDataAccess(context);
37 36
                 var res = service.AddId(dbos);
38 37
                 Assert.Equal(LuStatus.Success, res.Status);
39 38
 
@@ -64,7 +63,7 @@ namespace TestUtils.EfCrudDataAccess
64 63
         [Fact]
65 64
         public void TestDeleteMultiple2()
66 65
         {
67
-            Tests.TestRealDb(context =>
66
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
68 67
             {
69 68
                 var dbos = new List<PkGuidAddDbo>
70 69
                 {
@@ -84,7 +83,6 @@ namespace TestUtils.EfCrudDataAccess
84 83
                         SomeText = "24"
85 84
                     }
86 85
                 };
87
-                var service = new LuUtilsPkGuidDataAccess(context);
88 86
                 var res = service.AddId(dbos);
89 87
                 Assert.Equal(LuStatus.Success, res.Status);
90 88
 
@@ -107,7 +105,7 @@ namespace TestUtils.EfCrudDataAccess
107 105
         [Fact]
108 106
         public void TestDeleteMultiple3()
109 107
         {
110
-            Tests.TestRealDb(context =>
108
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
111 109
             {
112 110
                 var dbos = new List<PkGuidAddDbo>
113 111
                 {
@@ -127,7 +125,6 @@ namespace TestUtils.EfCrudDataAccess
127 125
                         SomeText = "24"
128 126
                     }
129 127
                 };
130
-                var service = new LuUtilsPkGuidDataAccess(context);
131 128
                 var res = service.AddId(dbos);
132 129
                 Assert.Equal(LuStatus.Success, res.Status);
133 130
 
@@ -150,7 +147,7 @@ namespace TestUtils.EfCrudDataAccess
150 147
         [Fact]
151 148
         public void TestDeleteMultiple4()
152 149
         {
153
-            Tests.TestRealDb(context =>
150
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
154 151
             {
155 152
                 var dbos = new List<PkBigSerialAddDbo>
156 153
                 {
@@ -170,7 +167,6 @@ namespace TestUtils.EfCrudDataAccess
170 167
                         SomeText = "24"
171 168
                     }
172 169
                 };
173
-                var service = new LuUtilsPkBigSerialDataAccess(context);
174 170
                 var res = service.AddId(dbos);
175 171
                 Assert.Equal(LuStatus.Success, res.Status);
176 172
 
@@ -193,7 +189,7 @@ namespace TestUtils.EfCrudDataAccess
193 189
         [Fact]
194 190
         public void TestDeleteMultiple5()
195 191
         {
196
-            Tests.TestRealDb(context =>
192
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
197 193
             {
198 194
                 var dbos = new List<PkGuidAddDbo>
199 195
                 {
@@ -213,7 +209,6 @@ namespace TestUtils.EfCrudDataAccess
213 209
                         SomeText = "24"
214 210
                     }
215 211
                 };
216
-                var service = new LuUtilsPkGuidDataAccess(context);
217 212
                 var res = service.AddId(dbos);
218 213
                 Assert.Equal(LuStatus.Success, res.Status);
219 214
 
@@ -244,7 +239,7 @@ namespace TestUtils.EfCrudDataAccess
244 239
         [Fact]
245 240
         public void TestDeleteSingle1()
246 241
         {
247
-            Tests.TestRealDb(context =>
242
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
248 243
             {
249 244
                 var dbos = new List<PkGuidAddDbo>
250 245
                 {
@@ -264,7 +259,6 @@ namespace TestUtils.EfCrudDataAccess
264 259
                         SomeText = "24"
265 260
                     }
266 261
                 };
267
-                var service = new LuUtilsPkGuidDataAccess(context);
268 262
                 var res = service.AddId(dbos);
269 263
                 Assert.Equal(LuStatus.Success, res.Status);
270 264
 
@@ -291,7 +285,7 @@ namespace TestUtils.EfCrudDataAccess
291 285
         [Fact]
292 286
         public void TestDeleteSingle2()
293 287
         {
294
-            Tests.TestRealDb(context =>
288
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
295 289
             {
296 290
                 var dbos = new List<PkGuidAddDbo>
297 291
                 {
@@ -311,7 +305,6 @@ namespace TestUtils.EfCrudDataAccess
311 305
                         SomeText = "24"
312 306
                     }
313 307
                 };
314
-                var service = new LuUtilsPkGuidDataAccess(context);
315 308
                 var res = service.AddId(dbos);
316 309
                 Assert.Equal(LuStatus.Success, res.Status);
317 310
 
@@ -336,7 +329,7 @@ namespace TestUtils.EfCrudDataAccess
336 329
         [Fact]
337 330
         public void TestDeleteSingle3()
338 331
         {
339
-            Tests.TestRealDb(context =>
332
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
340 333
             {
341 334
                 var dbos = new List<PkGuidAddDbo>
342 335
                 {
@@ -356,7 +349,6 @@ namespace TestUtils.EfCrudDataAccess
356 349
                         SomeText = "24"
357 350
                     }
358 351
                 };
359
-                var service = new LuUtilsPkGuidDataAccess(context);
360 352
                 var res = service.AddId(dbos);
361 353
                 Assert.Equal(LuStatus.Success, res.Status);
362 354
 
@@ -381,7 +373,7 @@ namespace TestUtils.EfCrudDataAccess
381 373
         [Fact]
382 374
         public void TestDeleteSingle4()
383 375
         {
384
-            Tests.TestRealDb(context =>
376
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
385 377
             {
386 378
                 var dbos = new List<PkBigSerialAddDbo>
387 379
                 {
@@ -401,7 +393,6 @@ namespace TestUtils.EfCrudDataAccess
401 393
                         SomeText = "24"
402 394
                     }
403 395
                 };
404
-                var service = new LuUtilsPkBigSerialDataAccess(context);
405 396
                 var res = service.AddId(dbos);
406 397
                 Assert.Equal(LuStatus.Success, res.Status);
407 398
 
@@ -428,7 +419,7 @@ namespace TestUtils.EfCrudDataAccess
428 419
         [Fact]
429 420
         public void TestDeleteSingle5()
430 421
         {
431
-            Tests.TestRealDb(context =>
422
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
432 423
             {
433 424
                 var dbos = new List<PkBigSerialAddDbo>
434 425
                 {
@@ -448,7 +439,6 @@ namespace TestUtils.EfCrudDataAccess
448 439
                         SomeText = "24"
449 440
                     }
450 441
                 };
451
-                var service = new LuUtilsPkBigSerialDataAccess(context);
452 442
                 var res = service.AddId(dbos);
453 443
                 Assert.Equal(LuStatus.Success, res.Status);
454 444
 
@@ -473,7 +463,7 @@ namespace TestUtils.EfCrudDataAccess
473 463
         [Fact]
474 464
         public void TestDeleteSingle6()
475 465
         {
476
-            Tests.TestRealDb(context =>
466
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
477 467
             {
478 468
                 var dbos = new List<PkBigSerialAddDbo>
479 469
                 {
@@ -493,7 +483,6 @@ namespace TestUtils.EfCrudDataAccess
493 483
                         SomeText = "24"
494 484
                     }
495 485
                 };
496
-                var service = new LuUtilsPkBigSerialDataAccess(context);
497 486
                 var res = service.AddId(dbos);
498 487
                 Assert.Equal(LuStatus.Success, res.Status);
499 488
 

+ 14
- 28
TestUtils/EfCrudDataAccess/LuEfReadDataAccessTest.cs View File

@@ -16,9 +16,8 @@ namespace TestUtils.EfCrudDataAccess
16 16
         [Fact]
17 17
         public void TestGetSingle1()
18 18
         {
19
-            Tests.TestRealDb(context =>
19
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
20 20
             {
21
-                var service = new LuUtilsPkBigSerialDataAccess(context);
22 21
                 var res = service.AddDbo(new PkBigSerialAddDbo
23 22
                 {
24 23
                     SomeInt = 42,
@@ -38,9 +37,8 @@ namespace TestUtils.EfCrudDataAccess
38 37
         [Fact]
39 38
         public void TestGetSingle2()
40 39
         {
41
-            Tests.TestRealDb(context =>
40
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
42 41
             {
43
-                var service = new LuUtilsPkBigSerialDataAccess(context);
44 42
                 var res = service.AddDbo(new PkBigSerialAddDbo
45 43
                 {
46 44
                     SomeInt = 42,
@@ -58,9 +56,8 @@ namespace TestUtils.EfCrudDataAccess
58 56
         [Fact]
59 57
         public void TestGetSingleByKeys1()
60 58
         {
61
-            Tests.TestRealDb(context =>
59
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
62 60
             {
63
-                var service = new LuUtilsPkBigSerialDataAccess(context);
64 61
                 var res = service.AddDbo(new PkBigSerialAddDbo
65 62
                 {
66 63
                     SomeInt = 42,
@@ -80,9 +77,8 @@ namespace TestUtils.EfCrudDataAccess
80 77
         [Fact]
81 78
         public void TestGetSingleByKeys2()
82 79
         {
83
-            Tests.TestRealDb(context =>
80
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
84 81
             {
85
-                var service = new LuUtilsPkBigSerialDataAccess(context);
86 82
                 var res = service.AddDbo(new PkBigSerialAddDbo
87 83
                 {
88 84
                     SomeInt = 42,
@@ -99,9 +95,8 @@ namespace TestUtils.EfCrudDataAccess
99 95
         [Fact]
100 96
         public void TestGetSingleById1()
101 97
         {
102
-            Tests.TestRealDb(context =>
98
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
103 99
             {
104
-                var service = new LuUtilsPkBigSerialDataAccess(context);
105 100
                 var res = service.AddId(new PkBigSerialAddDbo
106 101
                 {
107 102
                     SomeInt = 42,
@@ -121,9 +116,8 @@ namespace TestUtils.EfCrudDataAccess
121 116
         [Fact]
122 117
         public void TestGetSingleById2()
123 118
         {
124
-            Tests.TestRealDb(context =>
119
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
125 120
             {
126
-                var service = new LuUtilsPkBigSerialDataAccess(context);
127 121
                 var res = service.AddId(new PkBigSerialAddDbo
128 122
                 {
129 123
                     SomeInt = 42,
@@ -140,9 +134,8 @@ namespace TestUtils.EfCrudDataAccess
140 134
         [Fact]
141 135
         public void TestGetSingleById3()
142 136
         {
143
-            Tests.TestRealDb(context =>
137
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
144 138
             {
145
-                var service = new LuUtilsPkGuidDataAccess(context);
146 139
                 var res = service.AddId(new PkGuidAddDbo
147 140
                 {
148 141
                     SomeInt = 42,
@@ -162,9 +155,8 @@ namespace TestUtils.EfCrudDataAccess
162 155
         [Fact]
163 156
         public void TestGetSingleById4()
164 157
         {
165
-            Tests.TestRealDb(context =>
158
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
166 159
             {
167
-                var service = new LuUtilsPkGuidDataAccess(context);
168 160
                 var res = service.AddId(new PkGuidAddDbo
169 161
                 {
170 162
                     SomeInt = 42,
@@ -181,7 +173,7 @@ namespace TestUtils.EfCrudDataAccess
181 173
         [Fact]
182 174
         public void TestGetMultiple1()
183 175
         {
184
-            Tests.TestRealDb(context =>
176
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
185 177
             {
186 178
                 var dbos = new List<PkGuidAddDbo>
187 179
                 {
@@ -201,7 +193,6 @@ namespace TestUtils.EfCrudDataAccess
201 193
                         SomeText = "24"
202 194
                     }
203 195
                 };
204
-                var service = new LuUtilsPkGuidDataAccess(context);
205 196
                 var res = service.AddId(dbos);
206 197
                 Assert.Equal(LuStatus.Success, res.Status);
207 198
 
@@ -224,7 +215,7 @@ namespace TestUtils.EfCrudDataAccess
224 215
         [Fact]
225 216
         public void TestGetMultiple2()
226 217
         {
227
-            Tests.TestRealDb(context =>
218
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
228 219
             {
229 220
                 var dbos = new List<PkGuidAddDbo>
230 221
                 {
@@ -244,7 +235,6 @@ namespace TestUtils.EfCrudDataAccess
244 235
                         SomeText = "24"
245 236
                     }
246 237
                 };
247
-                var service = new LuUtilsPkGuidDataAccess(context);
248 238
                 var res = service.AddId(dbos);
249 239
                 Assert.Equal(LuStatus.Success, res.Status);
250 240
 
@@ -267,7 +257,7 @@ namespace TestUtils.EfCrudDataAccess
267 257
         [Fact]
268 258
         public void TestGetMultiple3()
269 259
         {
270
-            Tests.TestRealDb(context =>
260
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
271 261
             {
272 262
                 var dbos = new List<PkGuidAddDbo>
273 263
                 {
@@ -287,7 +277,6 @@ namespace TestUtils.EfCrudDataAccess
287 277
                         SomeText = "24"
288 278
                     }
289 279
                 };
290
-                var service = new LuUtilsPkGuidDataAccess(context);
291 280
                 var res = service.AddId(dbos);
292 281
                 Assert.Equal(LuStatus.Success, res.Status);
293 282
 
@@ -314,7 +303,7 @@ namespace TestUtils.EfCrudDataAccess
314 303
         [Fact]
315 304
         public void TestGetMultiple4()
316 305
         {
317
-            Tests.TestRealDb(context =>
306
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
318 307
             {
319 308
                 var dbos = new List<PkGuidAddDbo>
320 309
                 {
@@ -334,7 +323,6 @@ namespace TestUtils.EfCrudDataAccess
334 323
                         SomeText = "24"
335 324
                     }
336 325
                 };
337
-                var service = new LuUtilsPkGuidDataAccess(context);
338 326
                 var res = service.AddId(dbos);
339 327
                 Assert.Equal(LuStatus.Success, res.Status);
340 328
 
@@ -360,7 +348,7 @@ namespace TestUtils.EfCrudDataAccess
360 348
         [Fact]
361 349
         public void TestGetMultiple5()
362 350
         {
363
-            Tests.TestRealDb(context =>
351
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
364 352
             {
365 353
                 var dbos = new List<PkGuidAddDbo>
366 354
                 {
@@ -380,7 +368,6 @@ namespace TestUtils.EfCrudDataAccess
380 368
                         SomeText = "24"
381 369
                     }
382 370
                 };
383
-                var service = new LuUtilsPkGuidDataAccess(context);
384 371
                 var res = service.AddId(dbos);
385 372
                 Assert.Equal(LuStatus.Success, res.Status);
386 373
 
@@ -409,7 +396,7 @@ namespace TestUtils.EfCrudDataAccess
409 396
         [Fact]
410 397
         public void TestGetMultiple6()
411 398
         {
412
-            Tests.TestRealDb(context =>
399
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
413 400
             {
414 401
                 var dbos = new List<PkGuidAddDbo>
415 402
                 {
@@ -429,7 +416,6 @@ namespace TestUtils.EfCrudDataAccess
429 416
                         SomeText = "24"
430 417
                     }
431 418
                 };
432
-                var service = new LuUtilsPkGuidDataAccess(context);
433 419
                 var res = service.AddId(dbos);
434 420
                 Assert.Equal(LuStatus.Success, res.Status);
435 421
 

+ 35
- 0
TestUtils/EfCrudDataAccess/LuEfTransactionScopeTest.cs View File

@@ -0,0 +1,35 @@
1
+using System;
2
+using Luticate2.Utils.Dbo.Result;
3
+using Microsoft.Extensions.DependencyInjection;
4
+using TestUtils.DataAccess;
5
+using TestUtils.Dbo.PkGuid;
6
+using Xunit;
7
+
8
+namespace TestUtils.EfCrudDataAccess
9
+{
10
+    public class LuEfTransactionScopeTest
11
+    {
12
+        [Fact]
13
+        public void TestAddDboSingle1()
14
+        {
15
+            var serviceProvider = Tests.BuildServiceProvider();
16
+            var service = serviceProvider.GetService<LuUtilsPkGuidDataAccess>();
17
+            var transact = service.BeginTransaction(null);
18
+            var res = service.AddDbo(new PkGuidAddDbo
19
+            {
20
+                SomeInt = 42,
21
+                SomeText = "Test."
22
+            });
23
+            Assert.Equal(LuStatus.Success, res.Status);
24
+            Assert.NotEqual(new Guid().ToString(), res.Data.Id);
25
+
26
+            var get = service.GetSingleById(res.Data.Id);
27
+            Assert.Equal(LuStatus.Success, get.Status);
28
+
29
+            service.RollbackTransaction(transact);
30
+
31
+            get = service.GetSingleById(res.Data.Id);
32
+            Assert.Equal(LuStatus.NotFound, get.Status);
33
+        }
34
+    }
35
+}

+ 17
- 34
TestUtils/EfCrudDataAccess/LuEfUpdateDataAccessTest.cs View File

@@ -14,7 +14,7 @@ namespace TestUtils.EfCrudDataAccess
14 14
         [Fact]
15 15
         public void TestEditMultiple1()
16 16
         {
17
-            Tests.TestRealDb(context =>
17
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
18 18
             {
19 19
                 var dbos = new List<PkGuidAddDbo>
20 20
                 {
@@ -34,7 +34,6 @@ namespace TestUtils.EfCrudDataAccess
34 34
                         SomeText = "24"
35 35
                     }
36 36
                 };
37
-                var service = new LuUtilsPkGuidDataAccess(context);
38 37
                 var res = service.AddId(dbos);
39 38
                 Assert.Equal(LuStatus.Success, res.Status);
40 39
 
@@ -57,7 +56,7 @@ namespace TestUtils.EfCrudDataAccess
57 56
         [Fact]
58 57
         public void TestEditMultiple2()
59 58
         {
60
-            Tests.TestRealDb(context =>
59
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
61 60
             {
62 61
                 var dbos = new List<PkGuidAddDbo>
63 62
                 {
@@ -77,7 +76,6 @@ namespace TestUtils.EfCrudDataAccess
77 76
                         SomeText = "24"
78 77
                     }
79 78
                 };
80
-                var service = new LuUtilsPkGuidDataAccess(context);
81 79
                 var res = service.AddId(dbos);
82 80
                 Assert.Equal(LuStatus.Success, res.Status);
83 81
 
@@ -98,7 +96,7 @@ namespace TestUtils.EfCrudDataAccess
98 96
         [Fact]
99 97
         public void TestEditMultiple3()
100 98
         {
101
-            Tests.TestRealDb(context =>
99
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
102 100
             {
103 101
                 var dbos = new List<PkGuidAddDbo>
104 102
                 {
@@ -118,7 +116,6 @@ namespace TestUtils.EfCrudDataAccess
118 116
                         SomeText = "24"
119 117
                     }
120 118
                 };
121
-                var service = new LuUtilsPkGuidDataAccess(context);
122 119
                 var res = service.AddId(dbos);
123 120
                 Assert.Equal(LuStatus.Success, res.Status);
124 121
 
@@ -139,7 +136,7 @@ namespace TestUtils.EfCrudDataAccess
139 136
         [Fact]
140 137
         public void TestEditMultiple4()
141 138
         {
142
-            Tests.TestRealDb(context =>
139
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
143 140
             {
144 141
                 var dbos = new List<PkBigSerialAddDbo>
145 142
                 {
@@ -159,7 +156,6 @@ namespace TestUtils.EfCrudDataAccess
159 156
                         SomeText = "24"
160 157
                     }
161 158
                 };
162
-                var service = new LuUtilsPkBigSerialDataAccess(context);
163 159
                 var res = service.AddId(dbos);
164 160
                 Assert.Equal(LuStatus.Success, res.Status);
165 161
 
@@ -180,7 +176,7 @@ namespace TestUtils.EfCrudDataAccess
180 176
         [Fact]
181 177
         public void TestEditMultiple5()
182 178
         {
183
-            Tests.TestRealDb(context =>
179
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
184 180
             {
185 181
                 var dbos = new List<PkGuidAddDbo>
186 182
                 {
@@ -200,7 +196,6 @@ namespace TestUtils.EfCrudDataAccess
200 196
                         SomeText = "24"
201 197
                     }
202 198
                 };
203
-                var service = new LuUtilsPkGuidDataAccess(context);
204 199
                 var res = service.AddId(dbos);
205 200
                 Assert.Equal(LuStatus.Success, res.Status);
206 201
 
@@ -223,7 +218,7 @@ namespace TestUtils.EfCrudDataAccess
223 218
         [Fact]
224 219
         public void TestEditSingle1()
225 220
         {
226
-            Tests.TestRealDb(context =>
221
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
227 222
             {
228 223
                 var dbos = new List<PkBigSerialAddDbo>
229 224
                 {
@@ -243,7 +238,6 @@ namespace TestUtils.EfCrudDataAccess
243 238
                         SomeText = "24"
244 239
                     }
245 240
                 };
246
-                var service = new LuUtilsPkBigSerialDataAccess(context);
247 241
                 var res = service.AddId(dbos);
248 242
                 Assert.Equal(LuStatus.Success, res.Status);
249 243
                 var ids = res.Data.ToList();
@@ -276,7 +270,7 @@ namespace TestUtils.EfCrudDataAccess
276 270
         [Fact]
277 271
         public void TestEditSingle2()
278 272
         {
279
-            Tests.TestRealDb(context =>
273
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
280 274
             {
281 275
                 var dbos = new List<PkBigSerialAddDbo>
282 276
                 {
@@ -296,7 +290,6 @@ namespace TestUtils.EfCrudDataAccess
296 290
                         SomeText = "24"
297 291
                     }
298 292
                 };
299
-                var service = new LuUtilsPkBigSerialDataAccess(context);
300 293
                 var res = service.AddId(dbos);
301 294
                 Assert.Equal(LuStatus.Success, res.Status);
302 295
                 var ids = res.Data.ToList();
@@ -326,7 +319,7 @@ namespace TestUtils.EfCrudDataAccess
326 319
         [Fact]
327 320
         public void TestEditSingle3()
328 321
         {
329
-            Tests.TestRealDb(context =>
322
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
330 323
             {
331 324
                 var dbos = new List<PkBigSerialAddDbo>
332 325
                 {
@@ -346,7 +339,6 @@ namespace TestUtils.EfCrudDataAccess
346 339
                         SomeText = "24"
347 340
                     }
348 341
                 };
349
-                var service = new LuUtilsPkBigSerialDataAccess(context);
350 342
                 var res = service.AddId(dbos);
351 343
                 Assert.Equal(LuStatus.Success, res.Status);
352 344
                 var ids = res.Data.ToList();
@@ -378,7 +370,7 @@ namespace TestUtils.EfCrudDataAccess
378 370
         [Fact]
379 371
         public void TestEditSingle4()
380 372
         {
381
-            Tests.TestRealDb(context =>
373
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
382 374
             {
383 375
                 var dbos = new List<PkGuidAddDbo>
384 376
                 {
@@ -398,7 +390,6 @@ namespace TestUtils.EfCrudDataAccess
398 390
                         SomeText = "24"
399 391
                     }
400 392
                 };
401
-                var service = new LuUtilsPkGuidDataAccess(context);
402 393
                 var res = service.AddId(dbos);
403 394
                 Assert.Equal(LuStatus.Success, res.Status);
404 395
                 var ids = res.Data.ToList();
@@ -430,7 +421,7 @@ namespace TestUtils.EfCrudDataAccess
430 421
         [Fact]
431 422
         public void TestEditSingle5()
432 423
         {
433
-            Tests.TestRealDb(context =>
424
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
434 425
             {
435 426
                 var dbos = new List<PkGuidAddDbo>
436 427
                 {
@@ -450,7 +441,6 @@ namespace TestUtils.EfCrudDataAccess
450 441
                         SomeText = "24"
451 442
                     }
452 443
                 };
453
-                var service = new LuUtilsPkGuidDataAccess(context);
454 444
                 var res = service.AddId(dbos);
455 445
                 Assert.Equal(LuStatus.Success, res.Status);
456 446
                 var ids = res.Data.ToList();
@@ -480,7 +470,7 @@ namespace TestUtils.EfCrudDataAccess
480 470
         [Fact]
481 471
         public void TestEditSingle6()
482 472
         {
483
-            Tests.TestRealDb(context =>
473
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
484 474
             {
485 475
                 var dbos = new List<PkGuidAddDbo>
486 476
                 {
@@ -500,7 +490,6 @@ namespace TestUtils.EfCrudDataAccess
500 490
                         SomeText = "24"
501 491
                     }
502 492
                 };
503
-                var service = new LuUtilsPkGuidDataAccess(context);
504 493
                 var res = service.AddId(dbos);
505 494
                 Assert.Equal(LuStatus.Success, res.Status);
506 495
                 var ids = res.Data.ToList();
@@ -532,7 +521,7 @@ namespace TestUtils.EfCrudDataAccess
532 521
         [Fact]
533 522
         public void TestEditSingle7()
534 523
         {
535
-            Tests.TestRealDb(context =>
524
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
536 525
             {
537 526
                 var dbos = new List<PkBigSerialAddDbo>
538 527
                 {
@@ -552,7 +541,6 @@ namespace TestUtils.EfCrudDataAccess
552 541
                         SomeText = "24"
553 542
                     }
554 543
                 };
555
-                var service = new LuUtilsPkBigSerialDataAccess(context);
556 544
                 var res = service.AddId(dbos);
557 545
                 Assert.Equal(LuStatus.Success, res.Status);
558 546
                 var ids = res.Data.ToList();
@@ -586,7 +574,7 @@ namespace TestUtils.EfCrudDataAccess
586 574
         [Fact]
587 575
         public void TestEditSingle8()
588 576
         {
589
-            Tests.TestRealDb(context =>
577
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
590 578
             {
591 579
                 var dbos = new List<PkBigSerialAddDbo>
592 580
                 {
@@ -606,7 +594,6 @@ namespace TestUtils.EfCrudDataAccess
606 594
                         SomeText = "24"
607 595
                     }
608 596
                 };
609
-                var service = new LuUtilsPkBigSerialDataAccess(context);
610 597
                 var res = service.AddId(dbos);
611 598
                 Assert.Equal(LuStatus.Success, res.Status);
612 599
                 var ids = res.Data.ToList();
@@ -638,7 +625,7 @@ namespace TestUtils.EfCrudDataAccess
638 625
         [Fact]
639 626
         public void TestEditSingle9()
640 627
         {
641
-            Tests.TestRealDb(context =>
628
+            Tests.TestRealDb<LuUtilsPkBigSerialDataAccess>(service =>
642 629
             {
643 630
                 var dbos = new List<PkBigSerialAddDbo>
644 631
                 {
@@ -658,7 +645,6 @@ namespace TestUtils.EfCrudDataAccess
658 645
                         SomeText = "24"
659 646
                     }
660 647
                 };
661
-                var service = new LuUtilsPkBigSerialDataAccess(context);
662 648
                 var res = service.AddId(dbos);
663 649
                 Assert.Equal(LuStatus.Success, res.Status);
664 650
                 var ids = res.Data.ToList();
@@ -692,7 +678,7 @@ namespace TestUtils.EfCrudDataAccess
692 678
         [Fact]
693 679
         public void TestEditSingle10()
694 680
         {
695
-            Tests.TestRealDb(context =>
681
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
696 682
             {
697 683
                 var dbos = new List<PkGuidAddDbo>
698 684
                 {
@@ -712,7 +698,6 @@ namespace TestUtils.EfCrudDataAccess
712 698
                         SomeText = "24"
713 699
                     }
714 700
                 };
715
-                var service = new LuUtilsPkGuidDataAccess(context);
716 701
                 var res = service.AddId(dbos);
717 702
                 Assert.Equal(LuStatus.Success, res.Status);
718 703
                 var ids = res.Data.ToList();
@@ -746,7 +731,7 @@ namespace TestUtils.EfCrudDataAccess
746 731
         [Fact]
747 732
         public void TestEditSingle11()
748 733
         {
749
-            Tests.TestRealDb(context =>
734
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
750 735
             {
751 736
                 var dbos = new List<PkGuidAddDbo>
752 737
                 {
@@ -766,7 +751,6 @@ namespace TestUtils.EfCrudDataAccess
766 751
                         SomeText = "24"
767 752
                     }
768 753
                 };
769
-                var service = new LuUtilsPkGuidDataAccess(context);
770 754
                 var res = service.AddId(dbos);
771 755
                 Assert.Equal(LuStatus.Success, res.Status);
772 756
                 var ids = res.Data.ToList();
@@ -798,7 +782,7 @@ namespace TestUtils.EfCrudDataAccess
798 782
         [Fact]
799 783
         public void TestEditSingle12()
800 784
         {
801
-            Tests.TestRealDb(context =>
785
+            Tests.TestRealDb<LuUtilsPkGuidDataAccess>(service =>
802 786
             {
803 787
                 var dbos = new List<PkGuidAddDbo>
804 788
                 {
@@ -818,7 +802,6 @@ namespace TestUtils.EfCrudDataAccess
818 802
                         SomeText = "24"
819 803
                     }
820 804
                 };
821
-                var service = new LuUtilsPkGuidDataAccess(context);
822 805
                 var res = service.AddId(dbos);
823 806
                 Assert.Equal(LuStatus.Success, res.Status);
824 807
                 var ids = res.Data.ToList();

+ 31
- 10
TestUtils/Tests.cs View File

@@ -1,5 +1,5 @@
1 1
 using System;
2
-using System.Linq;
2
+using Luticate2.Utils.DataAccess;
3 3
 using Microsoft.EntityFrameworkCore;
4 4
 using Microsoft.Extensions.DependencyInjection;
5 5
 using TestUtils.DataAccess;
@@ -11,26 +11,47 @@ namespace TestUtils
11 11
         public const string ConnectionString =
12 12
             "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
13 13
 
14
-        public static void TestRealDb(Action<IServiceProvider> func)
14
+        public static IServiceProvider BuildServiceProvider()
15 15
         {
16 16
             IServiceCollection serviceCollection = new ServiceCollection();
17
+            serviceCollection.AddScoped<LuEfTransactionScope>();
18
+            serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
19
+            serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
20
+            serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
17 21
             serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
18 22
                 ServiceLifetime.Transient);
19
-            var serviceProvider = serviceCollection.BuildServiceProvider();
23
+            return serviceCollection.BuildServiceProvider();
24
+        }
25
+
26
+        protected static void _TestRealDb(Action<IServiceProvider> func)
27
+        {
28
+            var serviceProvider = BuildServiceProvider();
29
+            var transactionScope = serviceProvider.GetService<LuEfTransactionScope>();
30
+            transactionScope.BeginTransaction<LuUtilsDbContext>(null);
20 31
             try
21 32
             {
22 33
                 func(serviceProvider);
23 34
             }
24 35
             finally
25 36
             {
26
-                var dbContext = (LuUtilsDbContext)serviceProvider.GetService(typeof(LuUtilsDbContext));
27
-                dbContext.pk_bigserial.RemoveRange(dbContext.pk_bigserial);
28
-                dbContext.pk_guid.RemoveRange(dbContext.pk_guid);
29
-                dbContext.fk_pk_guids.RemoveRange(dbContext.fk_pk_guids);
30
-
31
-                dbContext.SaveChanges();
32
-                dbContext.Dispose();
37
+                transactionScope.RollbackTransaction<LuUtilsDbContext>();
33 38
             }
34 39
         }
40
+
41
+        public static void TestRealDb<TDataAccess>(Action<TDataAccess> func)
42
+        {
43
+            _TestRealDb(provider =>
44
+            {
45
+                func(provider.GetService<TDataAccess>());
46
+            });
47
+        }
48
+
49
+        public static void TestRealDb<TDataAccess, TDataAccess2>(Action<TDataAccess, TDataAccess2> func)
50
+        {
51
+            _TestRealDb(provider =>
52
+            {
53
+                func(provider.GetService<TDataAccess>(), provider.GetService<TDataAccess2>());
54
+            });
55
+        }
35 56
     }
36 57
 }

+ 3
- 1
WebApiUtils/Controllers/PkGuidController.cs View File

@@ -1,4 +1,6 @@
1 1
 using Luticate2.Utils.Controllers;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Microsoft.Extensions.Options;
2 4
 using TestUtils.Dbo.PkGuid;
3 5
 using WebApiUtils.Business;
4 6
 
@@ -6,7 +8,7 @@ namespace WebApiUtils.Controllers
6 8
 {
7 9
     public class PkGuidController : LuCrudController<PkGuidBusiness, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
8 10
     {
9
-        public PkGuidController(PkGuidBusiness busines) : base(busines)
11
+        public PkGuidController(PkGuidBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo) : base(busines, luUtilsOptionsDbo)
10 12
         {
11 13
         }
12 14
     }

+ 2
- 2
WebApiUtils/Startup.cs View File

@@ -39,8 +39,8 @@ namespace WebApiUtils
39 39
 
40 40
             services.AddLuticateUtils(options => options.Version = "dev");
41 41
 
42
-            services.AddSingleton<PkGuidBusiness>();
43
-            services.AddSingleton<LuUtilsPkGuidDataAccess>();
42
+            services.AddScoped<PkGuidBusiness>();
43
+            services.AddScoped<LuUtilsPkGuidDataAccess>();
44 44
             services.AddDbContext<LuUtilsDbContext>(options =>
45 45
             {
46 46
                 options.UseNpgsql(Configuration.GetConnectionString("default"));

+ 1
- 1
WebApiUtils/appsettings.Development.json View File

@@ -9,6 +9,6 @@
9 9
         }
10 10
     },
11 11
     "ConnectionStrings": {
12
-        "default":  "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;"
12
+        "default":  "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;ApplicationName=WebApiUtilsDev"
13 13
     }
14 14
 }

+ 1
- 1
WebApiUtils/appsettings.json View File

@@ -10,6 +10,6 @@
10 10
         }
11 11
     },
12 12
     "ConnectionStrings": {
13
-        "default":  "User ID=POSTGRES_USER;Password=POSTGRES_PASSWORD;Host=POSTGRES_HOST;Port=5432;Database=POSTGRES_DB;Pooling=true;"
13
+        "default":  "User ID=POSTGRES_USER;Password=POSTGRES_PASSWORD;Host=POSTGRES_HOST;Port=5432;Database=POSTGRES_DB;Pooling=true;ApplicationName=WebApiUtils"
14 14
     }
15 15
 }

Loading…
Cancel
Save