Skip to content

Commit 562858a

Browse files
committed
Minor refactoring of BalzorUtils
1 parent f1dea6f commit 562858a

File tree

5 files changed

+91
-63
lines changed

5 files changed

+91
-63
lines changed

src/Client/Client.Services/CRUDService.cs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
using DevInstance.BlazorUtils.Services;
44
using DevInstance.DevCoreApp.Shared.Services;
55
using DevInstance.BlazorUtils.Services.Wasm;
6+
using DevInstance.LogScope;
67

78
namespace DevInstance.DevCoreApp.Client.Services;
89

9-
public class CRUDService<T> : ServiceBase, ICRUDService<T> where T : ModelItem
10+
public class CRUDService<T> : BaseService, ICRUDService<T> where T : ModelItem
1011
{
1112
IApiContext<T> Api { get; set; }
1213

13-
public CRUDService(IApiContext<T> api)
14+
public CRUDService(IApiContext<T> api)
1415
{
1516
Api = api;
1617
}
@@ -19,63 +20,75 @@ public CRUDService(IApiContext<T> api)
1920

2021
public async Task<ServiceActionResult<T?>> GetAsync(string id)
2122
{
22-
return await HandleWebApiCallAsync(
23-
async () =>
24-
{
25-
var api = Api.Get(id);
23+
using (var log = Log.TraceScope())
24+
{
25+
return await ServiceUtils.HandleWebApiCallAsync(log,
26+
async (l) =>
27+
{
28+
var api = Api.Get(id);
2629

27-
return await api.ExecuteAsync();
28-
}
29-
);
30+
return await api.ExecuteAsync();
31+
}
32+
);
33+
}
3034
}
3135

3236
public void SetDataUpdate(T item)
3337
{
3438
OnDataUpdate?.Invoke(item);
3539
}
36-
40+
3741
public async Task<ServiceActionResult<T?>> AddNewAsync(T item)
3842
{
39-
return await HandleWebApiCallAsync(
40-
async () =>
41-
{
42-
var response = await Api.Post(item).ExecuteAsync();
43-
if (response != null)
43+
using (var log = Log.TraceScope())
44+
{
45+
return await ServiceUtils.HandleWebApiCallAsync(log,
46+
async (l) =>
4447
{
45-
OnDataUpdate?.Invoke(response);
48+
var response = await Api.Post(item).ExecuteAsync();
49+
if (response != null)
50+
{
51+
OnDataUpdate?.Invoke(response);
52+
}
53+
return response;
4654
}
47-
return response;
48-
}
49-
);
55+
);
56+
}
5057
}
5158

5259
public async Task<ServiceActionResult<T?>> UpdateAsync(T item)
5360
{
54-
return await HandleWebApiCallAsync(
55-
async () =>
56-
{
57-
var response = await Api.Put(item.Id, item).ExecuteAsync();
58-
if (response != null)
61+
using (var log = Log.TraceScope())
62+
{
63+
return await ServiceUtils.HandleWebApiCallAsync(log,
64+
async (l) =>
5965
{
60-
OnDataUpdate?.Invoke(response);
66+
var response = await Api.Put(item.Id, item).ExecuteAsync();
67+
if (response != null)
68+
{
69+
OnDataUpdate?.Invoke(response);
70+
}
71+
return response;
6172
}
62-
return response;
63-
}
64-
);
73+
);
74+
}
6575
}
6676

6777
public async Task<ServiceActionResult<bool>> RemoveAsync(T item)
6878
{
69-
return await HandleWebApiCallAsync(
70-
async () =>
71-
{
72-
var response = await Api.Delete(item.Id).ExecuteAsync();
73-
if (response != null)
79+
using (var log = Log.TraceScope())
80+
{
81+
return await ServiceUtils.HandleWebApiCallAsync(log,
82+
async (l) =>
7483
{
75-
OnDataUpdate?.Invoke(default(T));
84+
var response = await Api.Delete(item.Id).ExecuteAsync();
85+
if (response != null)
86+
{
87+
OnDataUpdate?.Invoke(default(T));
88+
}
89+
return true;
7690
}
77-
return true;
78-
}
79-
);
91+
);
92+
}
8093
}
8194
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using DevInstance.LogScope;
2+
using System.Net;
3+
4+
namespace DevInstance.DevCoreApp.Client.Services;
5+
6+
public delegate void ResultHandler<T>(T result);
7+
8+
public class BaseService
9+
{
10+
public IScopeLog? Log { get; protected set; } = null;
11+
12+
}

src/Client/Client.Services/SettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace DevInstance.DevCoreApp.Client.Services;
77

8-
public class SettingsService : ServiceBase, ISettingsService
8+
public class SettingsService : BaseService, ISettingsService
99
{
1010
SettingsLanguageItem[] supportedLanguages;
1111

src/Client/Client.Services/WeatherForecastService.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DevInstance.LogScope;
44
using DevInstance.DevCoreApp.Client.Services.Net.Api;
55
using DevInstance.DevCoreApp.Shared.Services;
6+
using DevInstance.BlazorUtils.Services.Wasm;
67

78
namespace DevInstance.DevCoreApp.Client.Services;
89

@@ -19,25 +20,28 @@ public WeatherForecastService(INetApiRepository api, IScopeManager lp)
1920

2021
public async Task<ServiceActionResult<ModelList<WeatherForecastItem>?>> GetItemsAsync(int? top, int? page, WeatherForecastFields? sortBy, bool? isAsc, string? search)
2122
{
22-
return await HandleWebApiCallAsync(
23-
async () =>
24-
{
25-
var api = Api.GetWeatherForecastApi().Get();
26-
if(top.HasValue)
23+
using (var log = Log.TraceScope())
24+
{
25+
return await ServiceUtils.HandleWebApiCallAsync(log,
26+
async (l) =>
2727
{
28-
api = api.Top(top.Value);
29-
}
30-
if (page.HasValue)
31-
{
32-
api = api.Page(page.Value);
33-
}
34-
if (sortBy.HasValue)
35-
{
36-
api = api.Sort(sortBy.Value.ToString(), isAsc ?? true);
37-
}
28+
var api = Api.GetWeatherForecastApi().Get();
29+
if (top.HasValue)
30+
{
31+
api = api.Top(top.Value);
32+
}
33+
if (page.HasValue)
34+
{
35+
api = api.Page(page.Value);
36+
}
37+
if (sortBy.HasValue)
38+
{
39+
api = api.Sort(sortBy.Value.ToString(), isAsc ?? true);
40+
}
3841

39-
return await api.ListAsync();
40-
}
41-
);
42+
return await api.ListAsync();
43+
}
44+
);
45+
}
4246
}
4347
}

src/Client/DevInstance.BlazorUtils/Services/Wasm/ServiceBase.cs renamed to src/Client/DevInstance.BlazorUtils/Services/Wasm/ServiceUtils.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ namespace DevInstance.BlazorUtils.Services.Wasm;
55

66
public delegate void ResultHandler<T>(T result);
77

8-
public class ServiceBase
8+
//TODO: make it utils as for server. move the base service to the client project
9+
public static class ServiceUtils
910
{
10-
public IScopeLog? Log { get; protected set; } = null;
11+
public delegate Task<T> WebApiHandlerAsync<T>(IScopeLog log);
1112

12-
public delegate Task<T> WebApiHandlerAsync<T>();
13-
14-
protected async Task<ServiceActionResult<T>> HandleWebApiCallAsync<T>(WebApiHandlerAsync<T> handler)
13+
public static async Task<ServiceActionResult<T>> HandleWebApiCallAsync<T>(IScopeLog log, WebApiHandlerAsync<T> handler)
1514
{
16-
using (var l = Log.TraceScope("ServiceBase").TraceScope())
15+
using (var l = log.TraceScope("ServiceUtils").TraceScope())
1716
{
1817
try
1918
{
2019
return new ServiceActionResult<T>
2120
{
22-
Result = await handler(),
21+
Result = await handler(l),
2322
Success = true,
2423
IsAuthorized = true,
2524
};

0 commit comments

Comments
 (0)