This repository was archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRavenStore.cs
More file actions
160 lines (128 loc) · 5.29 KB
/
RavenStore.cs
File metadata and controls
160 lines (128 loc) · 5.29 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Raven.Abstractions.Data;
using Raven.Abstractions.Replication;
using Raven.Client;
using Raven.Client.Document;
using Raven.Facade.Attributes;
using Raven.Imports.Newtonsoft.Json;
namespace Raven.Facade
{
public static class RavenStore
{
private static IDocumentStore _instance;
/*\ ***** ***** ***** ***** ***** Properties ***** ***** ***** ***** ***** \*/
[NotNull]
public static IDocumentStore Instance
{
get
{
if (_instance == null)
throw new InvalidOperationException("The document store has not been initialized.");
return _instance;
}
}
/*\ ***** ***** ***** ***** ***** Public Methods ***** ***** ***** ***** ***** \*/
public static IDocumentStore Initialize([NotNull] string connectionString)
{
if (connectionString == null)
throw new ArgumentNullException(nameof(connectionString));
var options = ParseConnectionString(connectionString);
var store = CreateUninitializedDocumentStore();
if (options.ResourceManagerId != Guid.Empty)
store.ResourceManagerId = options.ResourceManagerId;
if (options.Credentials != null)
store.Credentials = options.Credentials;
if (!String.IsNullOrEmpty(options.Url))
store.Url = options.Url;
if (!String.IsNullOrEmpty(options.DefaultDatabase))
store.DefaultDatabase = options.DefaultDatabase;
if (!String.IsNullOrEmpty(options.ApiKey))
store.ApiKey = options.ApiKey;
if (options.FailoverServers != null)
store.FailoverServers = options.FailoverServers;
store.EnlistInDistributedTransactions = options.EnlistInDistributedTransactions;
store.Initialize();
return _instance = store;
}
public static IDocumentStore Initialize([NotNull] string url, [NotNull] string databaseName, [CanBeNull] string apiKey)
{
if (url == null)
throw new ArgumentNullException(nameof(url));
if (databaseName == null)
throw new ArgumentNullException(nameof(databaseName));
var store = CreateUninitializedDocumentStore();
store.Url = url;
store.ApiKey = apiKey;
store.DefaultDatabase = databaseName;
store.Initialize();
return _instance = store;
}
/*\ ***** ***** ***** ***** ***** Private Methods ***** ***** ***** ***** ***** \*/
private static DocumentStore CreateUninitializedDocumentStore()
{
return new DocumentStore
{
Conventions =
{
FailoverBehavior = FailoverBehavior.FailImmediately,
FindTypeTagName = FindTypeTagName,
TransformTypeTagNameToDocumentKeyPrefix = TransformTypeTagNameToDocumentKeyPrefix,
CustomizeJsonSerializer = serializer =>
{
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
serializer.DateFormatHandling = DateFormatHandling.IsoDateFormat;
serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
serializer.NullValueHandling = NullValueHandling.Ignore;
},
},
};
}
private static RavenConnectionStringOptions ParseConnectionString([NotNull] string connectionString)
{
if (connectionString == null)
throw new ArgumentNullException(nameof(connectionString));
var parser = ConnectionStringParser<RavenConnectionStringOptions>.FromConnectionString(connectionString);
try
{
parser.Parse();
}
catch (ArgumentException e)
{
throw new ArgumentOutOfRangeException(nameof(connectionString), connectionString, e.Message);
}
return parser.ConnectionStringOptions;
}
private static string FindTypeTagName(Type type)
{
var collectionAttribute = type.GetCustomAttribute<CollectionAttribute>();
if (collectionAttribute == null)
{
return DocumentConvention.DefaultTypeTagName(type);
}
else
{
return collectionAttribute.Name;
}
}
private static string TransformTypeTagNameToDocumentKeyPrefix(string name)
{
var builder = new StringBuilder(name);
for (int i = 0; i < builder.Length; i++)
{
if (Char.IsUpper(builder[i]))
{
builder[i] = Char.ToLower(builder[i]);
if (i != 0 && Char.IsLower(builder[i - 1]))
builder.Insert(i, '-');
}
}
return builder.ToString();
}
}
}