3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using CommunityToolkit . Datasync . Client . Http ;
6
- using CommunityToolkit . Datasync . Client . Offline . Internal ;
6
+ using CommunityToolkit . Datasync . Client . Offline . Models ;
7
+ using CommunityToolkit . Datasync . Client . Query . Linq ;
7
8
8
9
namespace CommunityToolkit . Datasync . Client . Offline ;
9
10
@@ -83,8 +84,21 @@ public DatasyncOfflineOptionsBuilder UseHttpClientOptions(HttpClientOptions clie
83
84
/// <typeparam name="TEntity">The type of the entity.</typeparam>
84
85
/// <param name="configure">A configuration function for the entity.</param>
85
86
/// <returns>The current builder for chaining.</returns>
86
- public DatasyncOfflineOptionsBuilder Entity < TEntity > ( Action < EntityOfflineOptions > configure )
87
- => Entity ( typeof ( TEntity ) , configure ) ;
87
+ public DatasyncOfflineOptionsBuilder Entity < TEntity > ( Action < EntityOfflineOptions < TEntity > > configure ) where TEntity : class
88
+ {
89
+ ArgumentNullException . ThrowIfNull ( configure , nameof ( configure ) ) ;
90
+ if ( ! this . _entities . TryGetValue ( typeof ( TEntity ) . FullName ! , out EntityOfflineOptions ? options ) )
91
+ {
92
+ throw new DatasyncException ( $ "Entity is not synchronizable.") ;
93
+ }
94
+
95
+ EntityOfflineOptions < TEntity > entity = new ( ) ;
96
+ configure ( entity ) ;
97
+ options . ClientName = entity . ClientName ;
98
+ options . Endpoint = entity . Endpoint ;
99
+ options . QueryDescription = new QueryTranslator < TEntity > ( entity . Query ) . Translate ( ) ;
100
+ return this ;
101
+ }
88
102
89
103
/// <summary>
90
104
/// Configures the specified entity type for offline operations.
@@ -120,9 +134,10 @@ internal OfflineOptions Build()
120
134
{
121
135
HttpClientFactory = this . _httpClientFactory
122
136
} ;
137
+
123
138
foreach ( EntityOfflineOptions entity in this . _entities . Values )
124
139
{
125
- result . AddEntity ( entity . EntityType , entity . ClientName , entity . Endpoint ) ;
140
+ result . AddEntity ( entity . EntityType , entity . ClientName , entity . Endpoint , entity . QueryDescription ) ;
126
141
}
127
142
128
143
return result ;
@@ -139,14 +154,46 @@ public class EntityOfflineOptions(Type entityType)
139
154
/// </summary>
140
155
public Type EntityType { get => entityType ; }
141
156
157
+ /// <summary>
158
+ /// The name of the client to use when requesting a <see cref="HttpClient"/>.
159
+ /// </summary>
160
+ public string ClientName { get ; set ; } = string . Empty ;
161
+
142
162
/// <summary>
143
163
/// The endpoint for the entity type.
144
164
/// </summary>
145
165
public Uri Endpoint { get ; set ; } = new Uri ( $ "/tables/{ entityType . Name . ToLowerInvariant ( ) } ", UriKind . Relative ) ;
146
166
167
+ /// <summary>
168
+ /// The query description for the entity type - may be null (to mean "pull everything").
169
+ /// </summary>
170
+ internal QueryDescription ? QueryDescription { get ; set ; }
171
+ }
172
+
173
+ /// <summary>
174
+ /// A typed version of the <see cref="EntityOfflineOptions"/> for setting up queries.
175
+ /// </summary>
176
+ /// <typeparam name="TEntity">The type of entity being stored.</typeparam>
177
+ public class EntityOfflineOptions < TEntity > ( ) where TEntity : class
178
+ {
179
+ /// <summary>
180
+ /// The entity type being configured.
181
+ /// </summary>
182
+ public Type EntityType { get => typeof ( TEntity ) ; }
183
+
147
184
/// <summary>
148
185
/// The name of the client to use when requesting a <see cref="HttpClient"/>.
149
186
/// </summary>
150
187
public string ClientName { get ; set ; } = string . Empty ;
188
+
189
+ /// <summary>
190
+ /// The endpoint for the entity type.
191
+ /// </summary>
192
+ public Uri Endpoint { get ; set ; } = new Uri ( $ "/tables/{ typeof ( TEntity ) . Name . ToLowerInvariant ( ) } ", UriKind . Relative ) ;
193
+
194
+ /// <summary>
195
+ /// The query used to pull the data from the service.
196
+ /// </summary>
197
+ public IDatasyncPullQuery < TEntity > Query = new DatasyncPullQuery < TEntity > ( ) ;
151
198
}
152
199
}
0 commit comments