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
-using Luticate2.Auth.Business;
1
+using System;
2
+using Luticate2.Auth.Business;
2
 using Luticate2.Auth.DataAccess;
3
 using Luticate2.Auth.DataAccess;
3
 using Luticate2.Auth.Middlewares;
4
 using Luticate2.Auth.Middlewares;
4
 using Luticate2.Utils.Controllers;
5
 using Luticate2.Utils.Controllers;
6
+using Luticate2.Utils.Dbo.Basic;
5
 using Microsoft.AspNetCore.Builder;
7
 using Microsoft.AspNetCore.Builder;
6
 using Microsoft.AspNetCore.Http;
8
 using Microsoft.AspNetCore.Http;
7
 using Microsoft.Extensions.DependencyInjection;
9
 using Microsoft.Extensions.DependencyInjection;
10
 {
12
 {
11
     public static class LuAuthExtensions
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
             services.AddLuticateUtils(optionsDelegate);
17
             services.AddLuticateUtils(optionsDelegate);
16
 
18
 

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

1
 using Luticate2.Auth.Business;
1
 using Luticate2.Auth.Business;
2
 using Luticate2.Utils.Controllers;
2
 using Luticate2.Utils.Controllers;
3
+using Luticate2.Utils.Dbo.Basic;
3
 using Microsoft.AspNetCore.Mvc;
4
 using Microsoft.AspNetCore.Mvc;
5
+using Microsoft.Extensions.Options;
4
 
6
 
5
 namespace Luticate2.Auth.Controllers
7
 namespace Luticate2.Auth.Controllers
6
 {
8
 {
9
     {
11
     {
10
         private readonly LuGroupsBusiness _luGroupsBusiness;
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
             _luGroupsBusiness = luGroupsBusiness;
16
             _luGroupsBusiness = luGroupsBusiness;
15
         }
17
         }

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

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

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

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

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

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

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

5
 using Luticate2.Utils.Dbo.PaginatedRequest;
5
 using Luticate2.Utils.Dbo.PaginatedRequest;
6
 using Luticate2.Utils.Interfaces;
6
 using Luticate2.Utils.Interfaces;
7
 using Microsoft.AspNetCore.Mvc;
7
 using Microsoft.AspNetCore.Mvc;
8
+using Microsoft.Extensions.Options;
8
 
9
 
9
 namespace Luticate2.Utils.Controllers
10
 namespace Luticate2.Utils.Controllers
10
 {
11
 {
16
     {
17
     {
17
         protected readonly TBusiness Busines;
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
             Busines = busines;
22
             Busines = busines;
22
         }
23
         }

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

1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
2
 using Luticate2.Utils.Business;
3
 using Luticate2.Utils.Business;
4
+using Luticate2.Utils.DataAccess;
3
 using Luticate2.Utils.DataAccess.Npgsql;
5
 using Luticate2.Utils.DataAccess.Npgsql;
4
 using Luticate2.Utils.Dbo.Basic;
6
 using Luticate2.Utils.Dbo.Basic;
5
 using Luticate2.Utils.Dbo.Filter;
7
 using Luticate2.Utils.Dbo.Filter;
20
 {
22
 {
21
     public static class LuUtilsExtensions
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
             var settings = new JsonSerializerSettings();
27
             var settings = new JsonSerializerSettings();
30
             settings.ContractResolver = new SignalRContractResolver();
28
             settings.ContractResolver = new SignalRContractResolver();
39
                 options.Hubs.EnableDetailedErrors = true;
37
                 options.Hubs.EnableDetailedErrors = true;
40
             });
38
             });
41
 
39
 
40
+            services.AddScoped<LuEfTransactionScope>();
42
             services.AddSingleton<LuHubConnectionTracker>();
41
             services.AddSingleton<LuHubConnectionTracker>();
43
             services.AddSingleton<LuNotificationsBusiness>();
42
             services.AddSingleton<LuNotificationsBusiness>();
44
-            Options = new LuOptionsDbo();
45
-            optionsDelegate(Options);
43
+            services.Configure(optionsDelegate);
46
             return services;
44
             return services;
47
         }
45
         }
48
 
46
 

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

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
+using System.Data;
3
 using System.Linq.Expressions;
4
 using System.Linq.Expressions;
4
 using Luticate2.Utils.Dbo.Result;
5
 using Luticate2.Utils.Dbo.Result;
5
 using Microsoft.EntityFrameworkCore;
6
 using Microsoft.EntityFrameworkCore;
7
+using Microsoft.Extensions.DependencyInjection;
6
 
8
 
7
 namespace Luticate2.Utils.DataAccess
9
 namespace Luticate2.Utils.DataAccess
8
 {
10
 {
12
     {
14
     {
13
         private readonly IServiceProvider _serviceProvider;
15
         private readonly IServiceProvider _serviceProvider;
14
 
16
 
17
+        protected LuEfTransactionScope TransactionScope => _serviceProvider.GetService<LuEfTransactionScope>();
18
+
15
         protected LuEfDataAccess(IServiceProvider serviceProvider)
19
         protected LuEfDataAccess(IServiceProvider serviceProvider)
16
         {
20
         {
17
             _serviceProvider = serviceProvider;
21
             _serviceProvider = serviceProvider;
23
         {
27
         {
24
             try
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
             catch (Exception e)
40
             catch (Exception e)
33
             {
41
             {
47
             return Expression.Lambda<Func<TModel, bool>>(totalExp, param);
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

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
 namespace Luticate2.Utils.Dbo.Basic
1
 namespace Luticate2.Utils.Dbo.Basic
2
 {
2
 {
3
-    public class LuOptionsDbo
3
+    public class LuUtilsOptionsDbo
4
     {
4
     {
5
         public string Version { get; set; }
5
         public string Version { get; set; }
6
     }
6
     }

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

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

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

48
     services.AddLuticateUtils(options => options.Version = "dev");
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
     services.AddDbContext<YourDbContext>(options => // Replace YourDbContext with your own database context
53
     services.AddDbContext<YourDbContext>(options => // Replace YourDbContext with your own database context
53
     {
54
     {
54
         options.UseNpgsql(Configuration.GetConnectionString("default"));
55
         options.UseNpgsql(Configuration.GetConnectionString("default"));
56
             .AddEntityFrameworkNpgsqlLuticate() // MUST be before AddEntityFrameworkNpgsql()
57
             .AddEntityFrameworkNpgsqlLuticate() // MUST be before AddEntityFrameworkNpgsql()
57
             .AddEntityFrameworkNpgsql()
58
             .AddEntityFrameworkNpgsql()
58
             .BuildServiceProvider());
59
             .BuildServiceProvider());
59
-    });
60
+    }, ServiceLifetime.Transient);
60
 
61
 
61
     services.AddMvc()
62
     services.AddMvc()
62
         .AddLuticateUtils();
63
         .AddLuticateUtils();

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

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

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

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

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

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

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

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

+ 31
- 10
TestUtils/Tests.cs View File

1
 using System;
1
 using System;
2
-using System.Linq;
2
+using Luticate2.Utils.DataAccess;
3
 using Microsoft.EntityFrameworkCore;
3
 using Microsoft.EntityFrameworkCore;
4
 using Microsoft.Extensions.DependencyInjection;
4
 using Microsoft.Extensions.DependencyInjection;
5
 using TestUtils.DataAccess;
5
 using TestUtils.DataAccess;
11
         public const string ConnectionString =
11
         public const string ConnectionString =
12
             "User ID=dev;Password=dev;Host=localhost;Port=5432;Database=luticate2_utils;Pooling=true;";
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
             IServiceCollection serviceCollection = new ServiceCollection();
16
             IServiceCollection serviceCollection = new ServiceCollection();
17
+            serviceCollection.AddScoped<LuEfTransactionScope>();
18
+            serviceCollection.AddTransient<LuUtilsPkBigSerialDataAccess>();
19
+            serviceCollection.AddTransient<LuUtilsPkGuidDataAccess>();
20
+            serviceCollection.AddTransient<LuUtilsFkPkGuidDataAccess>();
17
             serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
21
             serviceCollection.AddDbContext<LuUtilsDbContext>(builder => builder.UseNpgsql(ConnectionString),
18
                 ServiceLifetime.Transient);
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
             try
31
             try
21
             {
32
             {
22
                 func(serviceProvider);
33
                 func(serviceProvider);
23
             }
34
             }
24
             finally
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
 using Luticate2.Utils.Controllers;
1
 using Luticate2.Utils.Controllers;
2
+using Luticate2.Utils.Dbo.Basic;
3
+using Microsoft.Extensions.Options;
2
 using TestUtils.Dbo.PkGuid;
4
 using TestUtils.Dbo.PkGuid;
3
 using WebApiUtils.Business;
5
 using WebApiUtils.Business;
4
 
6
 
6
 {
8
 {
7
     public class PkGuidController : LuCrudController<PkGuidBusiness, PkGuidAddDbo, PkGuidDbo, PkGuidAddDbo, string>
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
 
39
 
40
             services.AddLuticateUtils(options => options.Version = "dev");
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
             services.AddDbContext<LuUtilsDbContext>(options =>
44
             services.AddDbContext<LuUtilsDbContext>(options =>
45
             {
45
             {
46
                 options.UseNpgsql(Configuration.GetConnectionString("default"));
46
                 options.UseNpgsql(Configuration.GetConnectionString("default"));

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

9
         }
9
         }
10
     },
10
     },
11
     "ConnectionStrings": {
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
         }
10
         }
11
     },
11
     },
12
     "ConnectionStrings": {
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