Fauna v10 .NET/C# Driver 0.2.0-beta
 
Loading...
Searching...
No Matches
MappingContext.cs
Go to the documentation of this file.
1using System.Diagnostics.CodeAnalysis;
2
3namespace Fauna.Mapping;
4
8public sealed class MappingContext
9{
10 // FIXME(matt) possibly replace with more efficient cache impl
11 private readonly Dictionary<Type, MappingInfo> _cache = new();
12 private readonly Dictionary<string, MappingInfo> _collections = new();
13 private readonly Dictionary<Type, MappingInfo> _baseTypes = new();
14
15 public MappingContext() { }
16
17 public MappingContext(IEnumerable<DataContext.ICollection> collections)
18 {
19 foreach (var col in collections)
20 {
21 var info = GetInfo(col.DocType, col.Name);
22 _collections[col.Name] = info;
23 _baseTypes[col.DocType] = info;
24 }
25 }
26
27 public MappingContext(Dictionary<string, Type> collections)
28 {
29 foreach ((string name, Type ty) in collections)
30 {
31 _collections[name] = GetInfo(ty, name);
32 }
33 }
34
41 public bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
42 {
43 return _collections.TryGetValue(col, out ret);
44 }
45
52 public bool TryGetBaseType(Type ty, [NotNullWhen(true)] out MappingInfo? ret)
53 {
54 return _baseTypes.TryGetValue(ty, out ret);
55 }
56
63 public MappingInfo GetInfo(Type ty, string? colName = null)
64 {
65 lock (this)
66 {
67 if (_cache.TryGetValue(ty, out var ret))
68 {
69 return ret;
70 }
71 }
72
73 // MappingInfo caches itself during construction in order to make
74 // itself available early for recursive lookup.
75 return new MappingInfo(this, ty, colName);
76 }
77
78 internal void Add(Type ty, MappingInfo info)
79 {
80 lock (this)
81 {
82 _cache[ty] = info;
83 }
84 }
85}
A class representing the mapping context to be used during serialization and deserialization.
MappingContext(IEnumerable< DataContext.ICollection > collections)
MappingContext(Dictionary< string, Type > collections)
bool TryGetBaseType(Type ty, [NotNullWhen(true)] out MappingInfo? ret)
Gets the MappingInfo for a given Type.
MappingInfo GetInfo(Type ty, string? colName=null)
Gets the MappingInfo for a given Type.
bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
Gets the MappingInfo for a given collection name.
A class that encapsulates the class mapping, serialization, and deserialization of a Fauna object,...