1
+ namespace RoyalCode . WorkContext . Abstractions ;
2
+
3
+ /// <summary>
4
+ /// Extensions methods for <see cref="IWorkContext"/>.
5
+ /// </summary>
6
+ public static class WorkContextExtensions
7
+ {
8
+ /// <summary>
9
+ /// <para>
10
+ /// Adds a new entity to the repository to be persisted.
11
+ /// </para>
12
+ /// </summary>
13
+ /// <param name="context">The work context to get the repository.</param>
14
+ /// <param name="entity">The new entity instance.</param>
15
+ public static void Add < TEntity > ( this IWorkContext context , TEntity entity )
16
+ where TEntity : class
17
+ => context . Repository < TEntity > ( ) . Add ( entity ) ;
18
+
19
+ /// <summary>
20
+ /// <para>
21
+ /// Adds a collection of new entities to the repository to be persisted.
22
+ /// </para>
23
+ /// </summary>
24
+ /// <param name="context">The work context to get the repository.</param>
25
+ /// <param name="entities">A collection of new entities.</param>
26
+ public static void AddRange < TEntity > ( this IWorkContext context , IEnumerable < TEntity > entities )
27
+ where TEntity : class
28
+ => context . Repository < TEntity > ( ) . AddRange ( entities ) ;
29
+
30
+ /// <summary>
31
+ /// <para>
32
+ /// Finds an existing entity through its unique identity (id).
33
+ /// </para>
34
+ /// </summary>
35
+ /// <param name="context">The work context to get the repository.</param>
36
+ /// <param name="id">The entity identity.</param>
37
+ /// <returns>
38
+ /// <para>
39
+ /// Existing instance, or null/default if it does not exist.
40
+ /// </para>
41
+ /// </returns>
42
+ public static TEntity ? Find < TEntity > ( this IWorkContext context , object id )
43
+ where TEntity : class
44
+ => context . Repository < TEntity > ( ) . Find ( id ) ;
45
+
46
+ /// <summary>
47
+ /// <para>
48
+ /// Finds an existing entity through its unique identity (id).
49
+ /// </para>
50
+ /// </summary>
51
+ /// <param name="context">The work context to get the repository.</param>
52
+ /// <param name="id">The entity identity.</param>
53
+ /// <param name="token">Token for cancelling tasks.</param>
54
+ /// <returns>
55
+ /// <para>
56
+ /// Existing instance, or null/default if it does not exist.
57
+ /// </para>
58
+ /// </returns>
59
+ public static ValueTask < TEntity ? > FindAsync < TEntity > ( this IWorkContext context , object id , CancellationToken token = default )
60
+ where TEntity : class
61
+ => context . Repository < TEntity > ( ) . FindAsync ( id , token ) ;
62
+
63
+ /// <summary>
64
+ /// <para>
65
+ /// Remove an entity from the database.
66
+ /// </para>
67
+ /// </summary>
68
+ /// <param name="context">The work context to get the repository.</param>
69
+ /// <param name="entity">The entity.</param>
70
+ public static void Remove < TEntity > ( this IWorkContext context , TEntity entity )
71
+ where TEntity : class
72
+ => context . Repository < TEntity > ( ) . Remove ( entity ) ;
73
+
74
+ /// <summary>
75
+ /// <para>
76
+ /// Remove a range of entities from the database.
77
+ /// </para>
78
+ /// </summary>
79
+ /// <param name="context">The work context to get the repository.</param>
80
+ /// <param name="entities">The entities.</param>
81
+ /// <exception cref="ArgumentNullException">
82
+ /// If <paramref name="entities"/> is null.
83
+ /// </exception>
84
+ public static void RemoveRange < TEntity > ( this IWorkContext context , IEnumerable < TEntity > entities )
85
+ where TEntity : class
86
+ => context . Repository < TEntity > ( ) . RemoveRange ( entities ) ;
87
+
88
+ /// <summary>
89
+ /// <para>
90
+ /// Delete the entity by its Id.
91
+ /// </para>
92
+ /// </summary>
93
+ /// <param name="context">The work context to get the repository.</param>
94
+ /// <param name="id">The entity id.</param>
95
+ /// <returns>
96
+ /// <para>
97
+ /// The entity excluded, or null if the entity is not found.
98
+ /// </para>
99
+ /// </returns>
100
+ public static TEntity ? Delete < TEntity > ( this IWorkContext context , object id )
101
+ where TEntity : class
102
+ => context . Repository < TEntity > ( ) . Delete ( id ) ;
103
+
104
+ /// <summary>
105
+ /// <para>
106
+ /// Delete a range of entities by their Ids.
107
+ /// </para>
108
+ /// </summary>
109
+ /// <param name="context">The work context to get the repository.</param>
110
+ /// <param name="ids">The entities Ids.</param>
111
+ /// <returns>
112
+ /// <para>
113
+ /// The entities excluded, or null if the entity is not found.
114
+ /// </para>
115
+ /// </returns>
116
+ public static IEnumerable < TEntity ? > DeleteRange < TEntity > ( this IWorkContext context , IEnumerable < object > ids )
117
+ where TEntity : class
118
+ => context . Repository < TEntity > ( ) . DeleteRange ( ids ) ;
119
+
120
+ /// <summary>
121
+ /// <para>
122
+ /// Delete the entity by their Id.
123
+ /// </para>
124
+ /// </summary>
125
+ /// <param name="context">The work context to get the repository.</param>
126
+ /// <param name="id">The entity Id.</param>
127
+ /// <param name="token">Token for cancelling tasks.</param>
128
+ /// <returns>
129
+ /// <para>
130
+ /// The entity excluded, or null if the entity is not found.
131
+ /// </para>
132
+ /// </returns>
133
+ public static Task < TEntity ? > DeleteAsync < TEntity > ( this IWorkContext context , object id , CancellationToken token = default )
134
+ where TEntity : class
135
+ => context . Repository < TEntity > ( ) . DeleteAsync ( id , token ) ;
136
+
137
+ /// <summary>
138
+ /// <para>
139
+ /// Delete a range of entities by their Ids.
140
+ /// </para>
141
+ /// </summary>
142
+ /// <param name="context">The work context to get the repository.</param>
143
+ /// <param name="ids">The entities Ids.</param>
144
+ /// <param name="token">Token for cancelling tasks.</param>
145
+ /// <returns>
146
+ /// <para>
147
+ /// The entities excluded, or null if the entity is not found.
148
+ /// </para>
149
+ /// </returns>
150
+ public static Task < IEnumerable < TEntity ? > > DeleteRangeAsync < TEntity > (
151
+ this IWorkContext context , IEnumerable < object > ids , CancellationToken token = default )
152
+ where TEntity : class
153
+ => context . Repository < TEntity > ( ) . DeleteRangeAsync ( ids , token ) ;
154
+ }
0 commit comments