-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractRepository.cs
More file actions
124 lines (93 loc) · 2.7 KB
/
AbstractRepository.cs
File metadata and controls
124 lines (93 loc) · 2.7 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
using System;
using System.Linq.Expressions;
namespace Birko.Data.Repositories
{
/// <summary>
/// Provides a base implementation for repositories that work directly with data models.
/// Wraps an <see cref="Stores.IStore{T}"/> for storage operations.
/// </summary>
/// <typeparam name="T">The type of data model.</typeparam>
public abstract class AbstractRepository<T>
: IRepository<T>
where T : Models.AbstractModel
{
#region Properties
protected Stores.IStore<T>? Store { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance with a store.
/// </summary>
/// <param name="store">The store to use for data operations.</param>
public AbstractRepository(Stores.IStore<T>? store)
{
Store = store;
}
#endregion
#region Read Operations
/// <inheritdoc />
public virtual T? Read(Guid guid)
{
return Store?.Read(guid);
}
/// <inheritdoc />
public virtual T? Read(Expression<Func<T, bool>>? filter = null)
{
return Store?.Read(filter);
}
#endregion
#region Create Operations
/// <inheritdoc />
public virtual Guid Create(T data)
{
if (Store == null) return Guid.Empty;
return Store.Create(data);
}
#endregion
#region Update Operations
/// <inheritdoc />
public virtual void Update(T data)
{
Store?.Update(data);
}
#endregion
#region Delete Operations
/// <inheritdoc />
public virtual void Delete(T data)
{
Store?.Delete(data);
}
#endregion
#region Count Operations
/// <inheritdoc />
public virtual long Count(Expression<Func<T, bool>>? filter = null)
{
if (Store == null) return 0;
return Store.Count(filter);
}
#endregion
#region Save Operations
/// <inheritdoc />
public virtual Guid Save(T data)
{
if (Store == null) return Guid.Empty;
return Store.Save(data);
}
#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 void Destroy()
{
Store?.Destroy();
}
#endregion
}
}