-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceCollectionDBreezeExtensions.cs
49 lines (48 loc) · 1.85 KB
/
ServiceCollectionDBreezeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using DBreeze;
using DBreeze.AspNet;
using DBreeze.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionDBreezeExtensions
{
private static void SetNewtonsoftJsonSerializer()
{
CustomSerializator.ByteArraySerializator = o =>
{
using MemoryStream stream = new();
using BsonDataWriter writer = new(stream);
JsonSerializer serializer = new();
serializer.Serialize(writer, o);
return stream.ToArray();
};
CustomSerializator.ByteArrayDeSerializator = (bt, t) =>
{
using MemoryStream stream = new(bt);
using BsonDataReader reader = new(stream);
JsonSerializer serializer = new();
return serializer.Deserialize(reader, t);
};
}
// ReSharper disable once MemberCanBePrivate.Global
public static IServiceCollection AddDBreeze(this IServiceCollection services, Action<DBreezeConfiguration> configure)
{
SetNewtonsoftJsonSerializer();
services.Configure(configure);
services.AddSingleton<IDBreezeEngineProxyChildFactory, DBreezeEngineProxy>();
services.AddTransient<IDBreezeEngineProxy>(provider =>
provider.GetRequiredService<IDBreezeEngineProxyChildFactory>().CreateDisposableChild());
return services;
}
// ReSharper disable once MemberCanBePrivate.Global
public static IServiceCollection AddDBreeze(this IServiceCollection services)
{
return services.AddDBreeze(opt =>
{
opt.DBreezeDataFolderName = "./db";
});
}
}
}