-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathXmlRepositoryBase.cs
More file actions
147 lines (122 loc) · 4.4 KB
/
Copy pathXmlRepositoryBase.cs
File metadata and controls
147 lines (122 loc) · 4.4 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using SharpRepository.Repository;
using SharpRepository.Repository.Caching;
using SharpRepository.Repository.FetchStrategies;
namespace SharpRepository.XmlRepository
{
public abstract class XmlRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
{
private List<T> _items = new List<T>();
private string _storagePath;
/// <summary>
///
/// </summary>
/// <param name="storagePath">Path to the directory. The XML filename is determined by the TypeName</param>
/// <param name="cachingStrategy"></param>
internal XmlRepositoryBase(string storagePath, ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
{
Initialize(storagePath);
}
private void Initialize(string storagePath)
{
_items = new List<T>();
_storagePath = Path.Combine(storagePath, TypeName + ".xml");
// load up the items
LoadItems();
}
private void LoadItems()
{
if (!File.Exists(_storagePath)) return;
using (var stream = new FileStream(_storagePath, FileMode.Open))
using (var reader = new StreamReader(stream))
{
var serializer = new XmlSerializer(typeof(List<T>));
_items = (List<T>)serializer.Deserialize(reader);
}
}
protected List<T> Items
{
get
{
return _items;
}
}
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
{
return Items.AsQueryable();
}
protected override T GetQuery(TKey key, IFetchStrategy<T> fetchStrategy)
{
return BaseQuery(fetchStrategy).FirstOrDefault(x => MatchOnPrimaryKey(x, key));
}
protected override void AddItem(T entity)
{
if (GenerateKeyOnAdd && GetPrimaryKey(entity, out TKey id) && Equals(id, default(TKey)))
{
id = GeneratePrimaryKey();
SetPrimaryKey(entity, id);
}
Items.Add(entity);
}
protected override void DeleteItem(T entity)
{
GetPrimaryKey(entity, out TKey pkValue);
var index = Items.FindIndex(x => MatchOnPrimaryKey(x, pkValue));
if (index >= 0)
{
Items.RemoveAt(index);
}
}
protected override void UpdateItem(T entity)
{
GetPrimaryKey(entity, out TKey pkValue);
var index = Items.FindIndex(x => MatchOnPrimaryKey(x, pkValue));
if (index >= 0)
{
Items[index] = entity;
}
}
// need to match on primary key instead of using Equals() since the objects are not the same
private bool MatchOnPrimaryKey(T item, TKey keyValue)
{
return GetPrimaryKey(item, out TKey value) && keyValue.Equals(value);
}
protected override void SaveChanges()
{
var writer = new StreamWriter(_storagePath, false);
var serializer = new XmlSerializer(typeof(List<T>));
serializer.Serialize(writer, Items);
writer.Close();
}
public override void Dispose()
{
}
protected virtual TKey GeneratePrimaryKey()
{
if (typeof(TKey) == typeof(Guid))
{
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
}
if (typeof(TKey) == typeof(string))
{
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
}
var last = Items.LastOrDefault() ?? new T();
if (typeof(TKey) == typeof(Int32))
{
GetPrimaryKey(last, out TKey pkValue);
var nextInt = Convert.ToInt32(pkValue) + 1;
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
}
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and String.");
}
public override string ToString()
{
return "SharpRepository.XmlRepository";
}
}
}