Skip to content

Commit 8a2ac55

Browse files
committed
EF 6 refactoring: general simplification and Migrations support (initial commit)
1 parent d56be46 commit 8a2ac55

68 files changed

Lines changed: 775 additions & 668 deletions

File tree

Some content is hidden

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

src/Libraries/SmartStore.Core/Data/BaseDataProviderManager.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace SmartStore.Core.Data
4+
{
5+
public abstract class DataProviderFactory
6+
{
7+
protected DataProviderFactory(DataSettings settings)
8+
{
9+
Guard.ArgumentNotNull(() => settings);
10+
this.Settings = settings;
11+
}
12+
13+
protected DataSettings Settings
14+
{
15+
get;
16+
private set;
17+
}
18+
19+
public abstract IDataProvider LoadDataProvider();
20+
}
21+
}
Lines changed: 217 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,229 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using System.Web.Hosting;
8+
using SmartStore.Utilities;
9+
using SmartStore.Utilities.Threading;
310

411
namespace SmartStore.Core.Data
512
{
6-
public partial class DataSettings
13+
14+
public partial class DataSettings
715
{
8-
public DataSettings()
16+
private static readonly ReaderWriterLockSlim s_rwLock = new ReaderWriterLockSlim();
17+
private static DataSettings s_current = null;
18+
private static Func<DataSettings> s_settingsFactory = new Func<DataSettings>(() => new DataSettings());
19+
20+
protected const char SEPARATOR = ':';
21+
protected const string FILENAME = "Settings.txt";
22+
23+
private DataSettings()
924
{
10-
RawDataSettings=new Dictionary<string, string>();
25+
RawDataSettings = new Dictionary<string, string>();
1126
}
12-
public string DataProvider { get; set; }
13-
public string DataConnectionString { get; set; }
14-
public IDictionary<string, string> RawDataSettings { get; private set; }
27+
28+
#region Static members
29+
30+
public static void SetDefaultFactory(Func<DataSettings> factory)
31+
{
32+
Guard.ArgumentNotNull(() => factory);
33+
34+
lock (s_rwLock.GetWriteLock())
35+
{
36+
s_settingsFactory = factory;
37+
}
38+
}
39+
40+
public static DataSettings Current
41+
{
42+
get
43+
{
44+
using (s_rwLock.GetUpgradeableReadLock())
45+
{
46+
if (s_current == null)
47+
{
48+
using (s_rwLock.GetWriteLock())
49+
{
50+
if (s_current == null)
51+
{
52+
s_current = s_settingsFactory();
53+
s_current.Load();
54+
}
55+
}
56+
}
57+
}
58+
59+
return s_current;
60+
}
61+
}
62+
63+
public static bool DatabaseIsInstalled()
64+
{
65+
return Current.IsValid();
66+
}
67+
68+
public static void Reload()
69+
{
70+
using (s_rwLock.GetWriteLock())
71+
{
72+
s_current = null;
73+
}
74+
}
75+
76+
public static void Delete()
77+
{
78+
using (s_rwLock.GetWriteLock())
79+
{
80+
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
81+
File.Delete(filePath);
82+
s_current = null;
83+
}
84+
}
85+
86+
#endregion
87+
88+
#region Instance members
89+
90+
public string DataProvider
91+
{
92+
get;
93+
set;
94+
}
95+
96+
public string DataConnectionString
97+
{
98+
get;
99+
set;
100+
}
101+
102+
public IDictionary<string, string> RawDataSettings
103+
{
104+
get;
105+
private set;
106+
}
15107

16108
public bool IsValid()
17109
{
18-
return !String.IsNullOrEmpty(this.DataProvider) && !String.IsNullOrEmpty(this.DataConnectionString);
19-
}
20-
}
110+
return this.DataProvider.HasValue() && this.DataConnectionString.HasValue();
111+
}
112+
113+
public virtual bool Load()
114+
{
115+
using (s_rwLock.GetWriteLock())
116+
{
117+
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
118+
119+
this.Reset();
120+
121+
if (File.Exists(filePath))
122+
{
123+
string text = File.ReadAllText(filePath);
124+
var settings = ParseSettings(text);
125+
if (settings.Any())
126+
{
127+
this.RawDataSettings.AddRange(settings);
128+
if (settings.ContainsKey("DataProvider"))
129+
{
130+
this.DataProvider = settings["DataProvider"];
131+
}
132+
if (settings.ContainsKey("DataConnectionString"))
133+
{
134+
this.DataConnectionString = settings["DataConnectionString"];
135+
}
136+
137+
return this.IsValid();
138+
}
139+
}
140+
141+
return false;
142+
}
143+
}
144+
145+
public void Reset()
146+
{
147+
using (s_rwLock.GetWriteLock())
148+
{
149+
this.RawDataSettings.Clear();
150+
this.DataProvider = null;
151+
this.DataConnectionString = null;
152+
}
153+
}
154+
155+
public virtual bool Save()
156+
{
157+
if (!this.IsValid())
158+
return false;
159+
160+
using (s_rwLock.GetWriteLock())
161+
{
162+
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
163+
if (!File.Exists(filePath))
164+
{
165+
using (File.Create(filePath))
166+
{
167+
// we use 'using' to close the file after it's created
168+
}
169+
}
170+
171+
var text = SerializeSettings();
172+
File.WriteAllText(filePath, text);
173+
174+
return true;
175+
}
176+
}
177+
178+
#endregion
179+
180+
#region Instance helpers
181+
182+
protected virtual IDictionary<string, string> ParseSettings(string text)
183+
{
184+
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
185+
186+
if (text.IsEmpty())
187+
return result;
188+
189+
var settings = new List<string>();
190+
using (var reader = new StringReader(text))
191+
{
192+
string str;
193+
while ((str = reader.ReadLine()) != null)
194+
settings.Add(str);
195+
}
196+
197+
foreach (var setting in settings)
198+
{
199+
var separatorIndex = setting.IndexOf(SEPARATOR);
200+
if (separatorIndex == -1)
201+
{
202+
continue;
203+
}
204+
string key = setting.Substring(0, separatorIndex).Trim();
205+
string value = setting.Substring(separatorIndex + 1).Trim();
206+
207+
if (key.HasValue() && value.HasValue())
208+
{
209+
result.Add(key, value);
210+
}
211+
}
212+
213+
return result;
214+
}
215+
216+
protected virtual string SerializeSettings()
217+
{
218+
return string.Format("DataProvider: {0}{2}DataConnectionString: {1}{2}",
219+
this.DataProvider,
220+
this.DataConnectionString,
221+
Environment.NewLine
222+
);
223+
}
224+
225+
#endregion
226+
227+
}
228+
21229
}

src/Libraries/SmartStore.Core/Data/DataSettingsHelper.cs

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

0 commit comments

Comments
 (0)