Skip to content

Commit 541420c

Browse files
Merge pull request #43 from magiccodingman/magiccodingman/CallJsSerializationRefactor
Attributes Respected By Serializer
2 parents 61e18bd + 428f85d commit 541420c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1545
-655
lines changed

E2eTestWebApp/TestPages/SingleRecordBasicTestPage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public async Task<string> Add()
5959
{
6060
Name = "SingleRecordBasic.Add",
6161
Version = 1,
62-
StoreSchemas = [SchemaHelper.GetStoreSchema<Record>(null, false)]
62+
StoreSchemas = [SchemaHelper.GetStoreSchema(typeof(Record))]
6363
});
6464
var id = await database.AddAsync<Record, int>(NewSample);
6565
return id.ToString();
@@ -71,7 +71,7 @@ public async Task<string> Delete()
7171
{
7272
Name = "SingleRecordBasic.Delete",
7373
Version = 1,
74-
StoreSchemas = [SchemaHelper.GetStoreSchema<Record>(null, false)]
74+
StoreSchemas = [SchemaHelper.GetStoreSchema(typeof(Record))]
7575
});
7676
_ = await database.AddAsync<Record, int>(NewSample);
7777
await database.DeleteAsync(NewSample);
@@ -84,7 +84,7 @@ public async Task<string> Update()
8484
{
8585
Name = "SingleRecordBasic.Update",
8686
Version = 1,
87-
StoreSchemas = [SchemaHelper.GetStoreSchema<Record>(null, false)]
87+
StoreSchemas = [SchemaHelper.GetStoreSchema(typeof(Record))]
8888
});
8989
_ = await database.AddAsync<Record, int>(NewSample);
9090

@@ -100,7 +100,7 @@ public async Task<string> GetById()
100100
{
101101
Name = "SingleRecordBasic.GetById",
102102
Version = 1,
103-
StoreSchemas = [SchemaHelper.GetStoreSchema<Record>(null, false)]
103+
StoreSchemas = [SchemaHelper.GetStoreSchema(typeof(Record))]
104104
});
105105
var id = await database.AddAsync<Record, int>(NewSample);
106106
var result = await database.GetByIdAsync<Record>(id);
@@ -113,7 +113,7 @@ public async Task<string> GetAll()
113113
{
114114
Name = "SingleRecordBasic.GetAll",
115115
Version = 1,
116-
StoreSchemas = [SchemaHelper.GetStoreSchema<Record>(null, false)]
116+
StoreSchemas = [SchemaHelper.GetStoreSchema(typeof(Record))]
117117
});
118118
_ = await database.AddAsync<Record, int>(NewSample);
119119
var result = await database.GetAllAsync<Record>();

Magic.IndexedDb.sln

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 17.5.33502.453
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magic.IndexedDb", "Magic.IndexedDb\Magic.IndexedDb.csproj", "{A92429BE-E180-4150-BFC7-6C09558978D0}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IndexDb.Example", "IndexDb.Example\IndexDb.Example.csproj", "{4607CA8C-3AE3-4288-A52A-B3887B254657}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestWasm", "TestWasm\TestWasm.csproj", "{711DC6B6-9F00-4359-A046-9C5865B527C2}"
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "E2eTestWebApp", "E2eTestWebApp\E2eTestWebApp.csproj", "{D2025B3E-E14F-48E8-8B0E-3BA21762FB92}"
1111
EndProject
@@ -33,6 +33,10 @@ Global
3333
{BCC4F889-502A-43FF-B5C6-21C345E3EB2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3434
{BCC4F889-502A-43FF-B5C6-21C345E3EB2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3535
{BCC4F889-502A-43FF-B5C6-21C345E3EB2C}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{711DC6B6-9F00-4359-A046-9C5865B527C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{711DC6B6-9F00-4359-A046-9C5865B527C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{711DC6B6-9F00-4359-A046-9C5865B527C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{711DC6B6-9F00-4359-A046-9C5865B527C2}.Release|Any CPU.Build.0 = Release|Any CPU
3640
EndGlobalSection
3741
GlobalSection(SolutionProperties) = preSolution
3842
HideSolutionNode = FALSE
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Magic.IndexedDb.Interfaces;
2+
using Magic.IndexedDb.Models;
3+
using Microsoft.Extensions.DependencyInjection;
24
using Microsoft.JSInterop;
35
using System;
46
using System.Collections.Generic;
@@ -9,24 +11,31 @@
911

1012
namespace Magic.IndexedDb.Factories
1113
{
14+
[Obsolete("The old encryption system no longer works. It's being depreciated.")]
1215
public sealed class EncryptionFactory(IndexedDbManager indexDbManager) : IEncryptionFactory
1316
{
17+
[Obsolete("The old encryption system no longer works. It's being depreciated.")]
1418
public Task<string> EncryptAsync(
1519
string data, string key,
1620
CancellationToken cancellationToken = default)
1721
{
22+
23+
1824
return indexDbManager.CallJsAsync<string>(
1925
"encryptString", cancellationToken,
20-
[data, key]);
26+
new ITypedArgument[] { new TypedArgument<string>(data), new TypedArgument<string>(key) }
27+
);
2128
}
2229

30+
[Obsolete("The old encryption system no longer works. It's being depreciated.")]
2331
public Task<string> DecryptAsync(
2432
string encryptedData, string key,
2533
CancellationToken cancellationToken = default)
2634
{
2735
return indexDbManager.CallJsAsync<string>(
2836
"decryptString", cancellationToken,
29-
[encryptedData, key]);
37+
new ITypedArgument[] { new TypedArgument<string>(encryptedData), new TypedArgument<string>(key) }
38+
);
3039
}
3140
}
3241
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Magic.IndexedDb.Helpers
10+
{
11+
public static class AttributeHelpers
12+
{
13+
private static readonly ConcurrentDictionary<Type, PropertyInfo?> _primaryKeyCache = new();
14+
15+
public static object? GetPrimaryKeyValue<T>(T item)
16+
{
17+
if (item == null)
18+
throw new ArgumentNullException(nameof(item));
19+
20+
var primaryKeyProperty = GetPrimaryKeyProperty(typeof(T));
21+
return primaryKeyProperty?.GetValue(item);
22+
}
23+
24+
public static Type GetPrimaryKeyType<T>() where T : class
25+
{
26+
var primaryKeyProperty = GetPrimaryKeyProperty(typeof(T));
27+
if (primaryKeyProperty == null)
28+
throw new InvalidOperationException($"Type '{typeof(T).Name}' does not have a primary key with [MagicPrimaryKeyAttribute].");
29+
30+
return primaryKeyProperty.PropertyType;
31+
}
32+
33+
public static void ValidatePrimaryKey<T>(object key) where T : class
34+
{
35+
var expectedType = GetPrimaryKeyType<T>();
36+
37+
if (key == null || !expectedType.IsInstanceOfType(key))
38+
{
39+
throw new ArgumentException($"Invalid key type. Expected: {expectedType}, received: {key?.GetType()}.");
40+
}
41+
}
42+
43+
private static PropertyInfo? GetPrimaryKeyProperty(Type type)
44+
{
45+
return _primaryKeyCache.GetOrAdd(type, t =>
46+
t.GetProperties().FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(MagicPrimaryKeyAttribute))));
47+
}
48+
}
49+
}

Magic.IndexedDb/Helpers/MagicSerializationHelper.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Magic.IndexedDb.Models;
1+
using Magic.IndexedDb.Interfaces;
2+
using Magic.IndexedDb.Models;
3+
using System.Linq;
24
using System.Text.Json;
35

46
namespace Magic.IndexedDb.Helpers
@@ -9,6 +11,28 @@ namespace Magic.IndexedDb.Helpers
911
/// </summary>
1012
public static class MagicSerializationHelper
1113
{
14+
15+
public static object[] SerializeObjects(ITypedArgument[] objs, MagicJsonSerializationSettings? settings = null)
16+
{
17+
return objs.Select(arg => arg.SerializeToJsonElement(settings)).Cast<object>().ToArray();
18+
}
19+
20+
public static JsonElement SerializeObjectToJsonElement<T>(T value, MagicJsonSerializationSettings? settings = null)
21+
{
22+
if (settings == null)
23+
settings = new MagicJsonSerializationSettings();
24+
25+
if (value == null)
26+
throw new ArgumentNullException(nameof(value), "Object cannot be null");
27+
28+
var options = settings.GetOptionsWithResolver<T>(); // Ensure the correct resolver is applied
29+
string jsonString = JsonSerializer.Serialize(value, options); // Serialize using your settings
30+
31+
// Convert the string to a JsonElement so that Blazor treats it as a structured object
32+
using JsonDocument doc = JsonDocument.Parse(jsonString);
33+
return doc.RootElement.Clone(); // Clone to prevent disposal issues
34+
}
35+
1236
public static string SerializeObject<T>(T value, MagicJsonSerializationSettings? settings = null)
1337
{
1438
if (settings == null)

Magic.IndexedDb/Helpers/ManagerHelper.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)