-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAsyncRepository.cs
More file actions
131 lines (100 loc) · 3.49 KB
/
AbstractAsyncRepository.cs
File metadata and controls
131 lines (100 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace Birko.Data.Repositories
{
/// <summary>
/// Provides a base implementation for async repositories that work directly with data models.
/// Wraps an <see cref="Stores.IAsyncStore{T}"/> for storage operations.
/// </summary>
/// <typeparam name="T">The type of data model.</typeparam>
public abstract class AbstractAsyncRepository<T>
: IAsyncRepository<T>
where T : Models.AbstractModel
{
#region Properties
protected Stores.IAsyncStore<T>? Store { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance with an async store.
/// </summary>
/// <param name="store">The async store to use for data operations.</param>
public AbstractAsyncRepository(Stores.IAsyncStore<T>? store)
{
Store = store;
}
#endregion
#region Read Operations
/// <inheritdoc />
public virtual async Task<T?> ReadAsync(Guid guid, CancellationToken ct = default)
{
if (Store == null) return default;
return await Store.ReadAsync(guid, ct);
}
/// <inheritdoc />
public virtual async Task<T?> ReadAsync(Expression<Func<T, bool>>? filter = null, CancellationToken ct = default)
{
if (Store == null) return default;
return await Store.ReadAsync(filter, ct);
}
#endregion
#region Create Operations
/// <inheritdoc />
public virtual async Task<Guid> CreateAsync(T data, CancellationToken ct = default)
{
if (Store == null) return Guid.Empty;
return await Store.CreateAsync(data, ct: ct);
}
#endregion
#region Update Operations
/// <inheritdoc />
public virtual async Task UpdateAsync(T data, CancellationToken ct = default)
{
if (Store == null) return;
await Store.UpdateAsync(data, ct: ct);
}
#endregion
#region Delete Operations
/// <inheritdoc />
public virtual async Task DeleteAsync(T data, CancellationToken ct = default)
{
if (Store == null) return;
await Store.DeleteAsync(data, ct);
}
#endregion
#region Count Operations
/// <inheritdoc />
public virtual async Task<long> CountAsync(Expression<Func<T, bool>>? filter = null, CancellationToken ct = default)
{
if (Store == null) return 0;
return await Store.CountAsync(filter, ct);
}
#endregion
#region Save Operations
/// <inheritdoc />
public virtual async Task<Guid> SaveAsync(T data, CancellationToken ct = default)
{
if (Store == null) return Guid.Empty;
return await Store.SaveAsync(data, ct: ct);
}
#endregion
#region Factory Methods
/// <inheritdoc />
public virtual T CreateInstance()
{
if (Store == null) return Activator.CreateInstance<T>();
return Store.CreateInstance();
}
#endregion
#region Lifecycle Methods
/// <inheritdoc />
public virtual async Task DestroyAsync(CancellationToken ct = default)
{
if (Store == null) return;
await Store.DestroyAsync(ct);
}
#endregion
}
}