Fauna v10 .NET/C# Driver 1.0.1
 
Loading...
Searching...
No Matches
MappingContext.cs
Go to the documentation of this file.
1using System.Diagnostics.CodeAnalysis;
2using Fauna.Linq;
3
4namespace Fauna.Mapping;
5
9public sealed class MappingContext
10{
11 // FIXME(matt) possibly replace with more efficient cache impl
12 private readonly Dictionary<Type, MappingInfo> _cache = new();
13 private readonly Dictionary<string, MappingInfo> _collections = new();
14 private readonly Dictionary<Type, MappingInfo> _baseTypes = new();
15
19 public MappingContext() { }
20
25 public MappingContext(IEnumerable<DataContext.ICollection> collections)
26 {
27 foreach (var col in collections)
28 {
29 var info = GetInfo(col.DocType, col.Name);
30 _collections[col.Name] = info;
31 _baseTypes[col.DocType] = info;
32 }
33 }
34
39 public MappingContext(Dictionary<string, Type> collections)
40 {
41 foreach ((string name, Type ty) in collections)
42 {
43 _collections[name] = GetInfo(ty, name);
44 }
45 }
46
53 public bool TryGetCollection(string col, [NotNullWhen(true)] out MappingInfo? ret)
54 {
55 return _collections.TryGetValue(col, out ret);
56 }
57
64 public bool TryGetBaseType(Type ty, [NotNullWhen(true)] out MappingInfo? ret)
65 {
66 return _baseTypes.TryGetValue(ty, out ret);
67 }
68
75 public MappingInfo GetInfo(Type ty, string? colName = null)
76 {
77 lock (this)
78 {
79 if (_cache.TryGetValue(ty, out var ret))
80 {
81 return ret;
82 }
83 }
84
85 // MappingInfo caches itself during construction in order to make
86 // itself available early for recursive lookup.
87 return new MappingInfo(this, ty, colName);
88 }
89
90 internal void Add(Type ty, MappingInfo info)
91 {
92 lock (this)
93 {
94 _cache[ty] = info;
95 }
96 }
97}
A class representing the mapping context to be used during serialization and deserialization.
MappingContext()
Initializes an empty MappingContext.
MappingContext(IEnumerable< DataContext.ICollection > collections)
Initializes a MappingContext with associated collections.
MappingContext(Dictionary< string, Type > collections)
Initializes a MappingContext with associated 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,...