1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
2
using Camelot . DataAccess . UnitOfWork ;
5
3
using Camelot . Services . Abstractions ;
6
4
using Camelot . Services . Abstractions . Models ;
7
- using Camelot . Services . Abstractions . Models . Enums ;
8
5
9
6
namespace Camelot . Services ;
10
7
11
8
public class AppearanceSettingsService : IAppearanceSettingsService
12
9
{
13
10
private const string SettingsId = "AppearanceSettings" ;
11
+
14
12
private readonly AppearanceSettingsModel _default ;
15
13
private readonly IUnitOfWorkFactory _unitOfWorkFactory ;
14
+
16
15
private AppearanceSettingsModel _cachedSettingsValue ;
16
+
17
17
public AppearanceSettingsService ( IUnitOfWorkFactory unitOfWorkFactory )
18
18
{
19
19
_unitOfWorkFactory = unitOfWorkFactory ;
@@ -28,27 +28,23 @@ public AppearanceSettingsModel GetAppearanceSettings()
28
28
using var uow = _unitOfWorkFactory . Create ( ) ;
29
29
var repository = uow . GetRepository < AppearanceSettingsModel > ( ) ;
30
30
var dbModel = repository . GetById ( SettingsId ) ;
31
- if ( dbModel != null )
32
- _cachedSettingsValue = dbModel ;
33
- else
34
- _cachedSettingsValue = _default ;
35
- }
36
- else
37
- {
38
- // we set value of _cachedValue in 'save',
39
- // so no need to read from the repository every time.
31
+ _cachedSettingsValue = dbModel ?? _default ;
40
32
}
33
+
41
34
return _cachedSettingsValue ;
42
35
}
43
36
44
37
45
38
public void SaveAppearanceSettings ( AppearanceSettingsModel appearanceSettingsModel )
46
39
{
47
40
if ( appearanceSettingsModel == null )
41
+ {
48
42
throw new ArgumentNullException ( nameof ( appearanceSettingsModel ) ) ;
43
+ }
49
44
50
45
using var uow = _unitOfWorkFactory . Create ( ) ;
51
46
var repository = uow . GetRepository < AppearanceSettingsModel > ( ) ;
47
+
52
48
repository . Upsert ( SettingsId , appearanceSettingsModel ) ;
53
49
_cachedSettingsValue = appearanceSettingsModel ;
54
50
}
0 commit comments