Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
DataContext.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Diagnostics.CodeAnalysis;
3using System.Reflection;
4using System.Runtime.CompilerServices;
5using Fauna.Core;
6using Fauna.Mapping;
7using Fauna.Types;
8
9namespace Fauna.Linq;
10
16public abstract class DataContext : BaseClient
17{
18 private bool _initialized = false;
19 [AllowNull]
20 private IReadOnlyDictionary<Type, ICollection> _collections = null!;
21 [AllowNull]
22 private Client _client = null!;
23 [AllowNull]
24 private MappingContext _ctx = null!;
25
26 internal override MappingContext MappingCtx { get => _ctx; }
27 internal Linq.LookupTable LookupTable { get => new Linq.LookupTable(_ctx); }
28
29 internal void Init(Client client, Dictionary<Type, ICollection> collections, MappingContext ctx)
30 {
31 _client = client;
32 _collections = collections.ToImmutableDictionary();
33 _ctx = ctx;
34
35 foreach (var col in collections.Values)
36 {
37 ((Linq.QuerySource)col).SetContext(this);
38 }
39
40 _initialized = true;
41 }
42
43 // IClient impl
44
45 internal override Task<QuerySuccess<T>> QueryAsyncInternal<T>(
46 Query query,
47 Serialization.ISerializer<T> serializer,
49 QueryOptions? queryOptions,
50 CancellationToken cancel)
51 {
52 CheckInitialization();
53 return _client.QueryAsyncInternal(query, serializer, ctx, queryOptions, cancel);
54 }
55
56 internal override IAsyncEnumerator<Event<T>> SubscribeStreamInternal<T>(
57 EventSource eventSource,
59 CancellationToken cancel = default)
60 {
61 CheckInitialization();
62 return _client.SubscribeStreamInternal<T>(eventSource, ctx, cancel);
63 }
64
65 internal override IAsyncEnumerator<FeedPage<T>> SubscribeFeedInternal<T>(
66 EventSource eventSource,
68 CancellationToken cancel = default)
69 {
70 CheckInitialization();
71 return _client.SubscribeFeedInternal<T>(eventSource, ctx, cancel);
72 }
73
74
75 // Schema DSL
76
80 [AttributeUsage(AttributeTargets.Class)]
81 public class NameAttribute : Attribute
82 {
83 internal readonly string Name;
84
89 public NameAttribute(string name)
90 {
91 Name = name;
92 }
93 }
94
98 public interface ICollection : Linq.IQuerySource
99 {
103 public string Name { get; }
107 public Type DocType { get; }
108 }
109
114 public abstract class Collection<Doc> : Linq.QuerySource<Doc>, ICollection
115 {
117 public string Name { get; }
118
120 public Type DocType { get => typeof(Doc); }
121
126 public Collection()
127 {
128 var nameAttr = this.GetType().GetCustomAttribute<NameAttribute>();
129 Name = nameAttr?.Name ?? typeof(Doc).Name;
130 SetQuery<Doc>(Linq.IntermediateQueryHelpers.CollectionAll(this));
131 }
132
133 // index call DSL
134
144 protected IndexCall Index(string? name = null, [CallerMemberName] string? auto = null)
145 {
146 if (name is null && auto is not null)
147 {
148 name = FieldName.Canonical(auto);
149 }
150
151 if (string.IsNullOrEmpty(name))
152 throw new ArgumentException($"{nameof(name)} cannot be null or empty.");
153
154 return new IndexCall(this, name, Ctx);
155 }
156
160 protected class IndexCall
161 {
162 private readonly ICollection _coll;
163 private readonly string _name;
164 private readonly DataContext _ctx;
165
172 public IndexCall(ICollection coll, string name, DataContext ctx)
173 {
174 _coll = coll;
175 _name = name;
176 _ctx = ctx;
177 }
178
183 public Index<Doc> Call() => Call(new object[] { });
184
190 public Index<Doc> Call(object a1) => Call(new object[] { a1 });
191
198 public Index<Doc> Call(object a1, object a2) => Call(new object[] { a1, a2 });
199
207 public Index<Doc> Call(object a1, object a2, object a3) => Call(new object[] { a1, a2, a3 });
208
214 public Index<Doc> Call(object[] args) => new Index<Doc>(_coll, _name, args, _ctx);
215
216 }
217 }
218
222 public interface IIndex : Linq.IQuerySource
223 {
227 public ICollection Collection { get; }
231 public string Name { get; }
235 public Type DocType { get; }
239 public object[] Args { get; }
240 }
241
246 public class Index<Doc> : Linq.QuerySource<Doc>, IIndex
247 {
249 public ICollection Collection { get; }
250
252 public string Name { get; }
253
255 public Type DocType { get => typeof(Doc); }
256
258 public object[] Args { get; }
259
260 internal Index(ICollection coll, string name, object[] args, DataContext ctx)
261 {
262 Collection = coll;
263 Name = name;
264 Args = args;
265 Ctx = ctx;
266 SetQuery<Doc>(Linq.IntermediateQueryHelpers.CollectionIndex(this));
267 }
268 }
269
270 // UDF / Function DSL
271
275 public interface IFunction : Linq.IQuerySource
276 {
280 public string Name { get; }
284 public object[] Args { get; }
285 }
286
291 protected class FunctionCall<T> where T : notnull
292 {
293
297 public string Name { get; }
298 private readonly DataContext _ctx;
299
305 public FunctionCall(string name, DataContext ctx)
306 {
307 Name = name;
308 _ctx = ctx;
309 }
310
315 public T Call() => Call(Array.Empty<object>());
316
322 public T Call(object a1) => Call(new[] { a1 });
323
330 public T Call(object a1, object a2) => Call(new[] { a1, a2 });
331
339 public T Call(object a1, object a2, object a3) => Call(new[] { a1, a2, a3 });
340
346 public T Call(object[] args) => CallAsync(args).Result;
347
352 public async Task<T> CallAsync() => await CallAsync(Array.Empty<object>());
353
359 public async Task<T> CallAsync(object a1) => await CallAsync(new[] { a1 });
360
367 public async Task<T> CallAsync(object a1, object a2) => await CallAsync(new[] { a1, a2 });
368
376 public async Task<T> CallAsync(object a1, object a2, object a3) => await CallAsync(new[] { a1, a2, a3 });
377
383 public async Task<T> CallAsync(object[] args)
384 {
385 var q = Linq.IntermediateQueryHelpers.Function(Name, args);
386 return (await _ctx.QueryAsync<T>(q)).Data;
387 }
388
389 }
390
398 protected FunctionCall<T> Fn<T>(string name = "", [CallerMemberName] string callerName = "") where T : notnull
399 {
400 var fnName = name == "" ? callerName : name;
401 return new FunctionCall<T>(fnName, this);
402 }
403
409 protected Col GetCollection<Col>() where Col : ICollection
410 {
411 CheckInitialization();
412 return (Col)_collections[typeof(Col)];
413 }
414
415 private void CheckInitialization()
416 {
417 if (!_initialized)
418 {
419 throw new InvalidOperationException(
420 "Uninitialized context. DataContext sub-classes must be instantiated using a client's .DataContext() method.");
421 }
422
423 }
424}
System.ArgumentException ArgumentException
The base class for Client and DataContext.
Definition IClient.cs:370
Represents a client for interacting with a Fauna.
Definition Client.cs:16
Represents the options for customizing Fauna queries.
A class representing an index call.
Index< Doc > Call(object a1, object a2, object a3)
Invokes an index.
IndexCall(ICollection coll, string name, DataContext ctx)
Initializes an index call.
Index< Doc > Call()
Invokes an index.
Index< Doc > Call(object a1, object a2)
Invokes an index.
Index< Doc > Call(object a1)
Invokes an index.
Index< Doc > Call(object[] args)
Invokes an index.
An abstract collection. This should be implemented for each collection in the database.
Collection()
Initializes a new collection with a name set to the NameAttribute, or the name of ....
IndexCall Index(string? name=null, [CallerMemberName] string? auto=null)
Initializes an index associated with the collection. The name of the index can be assigned,...
Type DocType
The .NET type associated with documents in the collection.
string Name
The collection name.
A class representing a function call.
T Call(object a1, object a2, object a3)
Calls the function.
T Call()
Calls the function.
string Name
The name of the function.
async Task< T > CallAsync(object a1)
Calls the function asynchronously.
async Task< T > CallAsync(object[] args)
Calls the function asynchronously.
T Call(object a1)
Calls the function.
async Task< T > CallAsync()
Calls the function asynchronously.
FunctionCall(string name, DataContext ctx)
Initializes a function call.
async Task< T > CallAsync(object a1, object a2, object a3)
Calls the function asynchronously.
async Task< T > CallAsync(object a1, object a2)
Calls the function asynchronously.
T Call(object a1, object a2)
Calls the function.
T Call(object[] args)
Calls the function.
A class representing an index query source.
Type DocType
The return type of the index.
object[] Args
An index argument array.
string Name
The name of the index.
An attribute representing a collection name.
NameAttribute(string name)
Initializes a NameAttribute.
An abstract class representing a DataContext. This is a special type of Fauna client that can be used...
Col GetCollection< Col >()
Gets a collection of type Col .
FunctionCall< T > Fn< T >(string name="", [CallerMemberName] string callerName="")
A helper method to declare new function calls.
An abstract class representing a QuerySource for LINQ-style queries.
A class representing the mapping context to be used during serialization and deserialization.
Represents the abstract base class for constructing FQL queries.
Definition Query.cs:11
Represents a Fauna EventSource for initializing Streams and Feeds.
Definition EventSource.cs:9
An interface for a Fauna collection within a DataContext.
Type DocType
The .NET type associated with documents in the collection.
string Name
The collection name.
An interface representing a function.
object[] Args
An array of arguments for the function.
string Name
The name of the function.
An interface representing an index query source.
string Name
The name of the index.
Type DocType
The return type of the index.
object[] Args
An index argument array.
An interface for common static IQuerySource methods that are non-generic.