|
5 | 5 | namespace JsonApiDotNetCore.Models
|
6 | 6 | {
|
7 | 7 | public class Identifiable : Identifiable<int>
|
8 |
| - {} |
9 |
| - |
| 8 | + { } |
| 9 | + |
10 | 10 | public class Identifiable<T> : IIdentifiable<T>
|
11 | 11 | {
|
| 12 | + /// <summary> |
| 13 | + /// The resource identifier |
| 14 | + /// </summary> |
12 | 15 | public virtual T Id { get; set; }
|
13 | 16 |
|
| 17 | + /// <summary> |
| 18 | + /// The string representation of the `Id`. |
| 19 | + /// |
| 20 | + /// This is used in serialization and deserialization. |
| 21 | + /// The getters should handle the conversion |
| 22 | + /// from `typeof(T)` to a string and the setter vice versa. |
| 23 | + /// |
| 24 | + /// To override this behavior, you can either implement the |
| 25 | + /// <see cref="IIdentifiable{T}"> interface directly or override |
| 26 | + /// `GetStringId` and `GetTypedId` methods. |
| 27 | + /// </summary> |
14 | 28 | [NotMapped]
|
15 | 29 | public string StringId
|
16 | 30 | {
|
17 | 31 | get => GetStringId(Id);
|
18 | 32 | set => Id = GetTypedId(value);
|
19 | 33 | }
|
20 | 34 |
|
| 35 | + /// <summary> |
| 36 | + /// Convert the provided resource identifier to a string. |
| 37 | + /// </summary> |
21 | 38 | protected virtual string GetStringId(object value)
|
22 | 39 | {
|
23 | 40 | var type = typeof(T);
|
24 | 41 | var stringValue = value.ToString();
|
25 | 42 |
|
26 |
| - if(type == typeof(Guid)) |
| 43 | + if (type == typeof(Guid)) |
27 | 44 | {
|
28 | 45 | var guid = Guid.Parse(stringValue);
|
29 | 46 | return guid == Guid.Empty ? string.Empty : stringValue;
|
30 | 47 | }
|
31 | 48 |
|
32 |
| - return stringValue == "0" |
33 |
| - ? string.Empty |
| 49 | + return stringValue == "0" |
| 50 | + ? string.Empty |
34 | 51 | : stringValue;
|
35 | 52 | }
|
36 | 53 |
|
| 54 | + /// <summary> |
| 55 | + /// Convert a string to a typed resource identifier. |
| 56 | + /// </summary> |
37 | 57 | protected virtual T GetTypedId(string value)
|
38 | 58 | {
|
39 | 59 | var convertedValue = TypeHelper.ConvertType(value, typeof(T));
|
|
0 commit comments