Fauna .NET Driver 0.1.0-beta
 
Loading...
Searching...
No Matches
MappingContext.cs
Go to the documentation of this file.
1using System.Diagnostics.CodeAnalysis;
2
3namespace Fauna.Mapping;
4
5public sealed class MappingContext
6{
7 // FIXME(matt) possibly replace with more efficient cache impl
8 private readonly Dictionary<Type, MappingInfo> _cache = new();
9 private readonly Dictionary<string, MappingInfo> _collections = new();
10 private readonly Dictionary<Type, MappingInfo> _baseTypes = new();
11
12 public MappingContext() { }
13
14 public MappingContext(IEnumerable<DataContext.Collection> collections)
15 {
16 foreach (var col in collections)
17 {
18 var info = GetInfo(col.DocType);
19 _collections[col.Name] = info;
20 _baseTypes[col.DocType] = info;
21 }
22 }
23
24 public MappingContext(Dictionary<string, Type> collections)
25 {
26 foreach (var (name, ty) in collections)
27 {
28 _collections[name] = GetInfo(ty);
29 }
30 }
31
32 public bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
33 {
34 return _collections.TryGetValue(col, out ret);
35 }
36
37 public bool TryGetBaseType(Type ty, [NotNullWhen(true)] out MappingInfo? ret)
38 {
39 return _baseTypes.TryGetValue(ty, out ret);
40 }
41
42 public MappingInfo GetInfo(Type ty)
43 {
44 lock (this)
45 {
46 if (_cache.TryGetValue(ty, out var ret))
47 {
48 return ret;
49 }
50 }
51
52 // MappingInfo caches itself during construction in order to make
53 // itself available early for recursive lookup.
54 return new MappingInfo(this, ty);
55 }
56
57 internal void Add(Type ty, MappingInfo info)
58 {
59 lock (this)
60 {
61 _cache[ty] = info;
62 }
63 }
64}
MappingContext(Dictionary< string, Type > collections)
bool TryGetBaseType(Type ty, [NotNullWhen(true)] out MappingInfo? ret)
MappingContext(IEnumerable< DataContext.Collection > collections)
bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
MappingInfo GetInfo(Type ty)