Skip to content

Commit 9bdcebd

Browse files
author
Jeff Treuting
committed
Cleaned up XmlRepository
For some reason, previously we needed to clone the internal list before using it for the queries, but that didn't seem right anymore. So I removed it and made sure all tests still pass.
1 parent bff82c2 commit 9bdcebd

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

SharpRepository.XmlRepository/XmlRepositoryBase.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SharpRepository.XmlRepository
1111
{
1212
public abstract class XmlRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
1313
{
14-
private IList<T> _items = new List<T>();
14+
private List<T> _items = new List<T>();
1515
private string _storagePath;
1616

1717
/// <summary>
@@ -50,7 +50,7 @@ private void LoadItems()
5050
reader.Close();
5151
}
5252

53-
protected IList<T> Items
53+
protected List<T> Items
5454
{
5555
get
5656
{
@@ -60,37 +60,38 @@ protected IList<T> Items
6060

6161
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
6262
{
63-
return CloneList(Items).AsQueryable();
63+
return Items.AsQueryable();
64+
//return CloneList(Items).AsQueryable();
6465
}
6566

6667
protected override T GetQuery(TKey key)
6768
{
6869
return BaseQuery().FirstOrDefault(x => MatchOnPrimaryKey(x, key));
6970
}
7071

71-
private static IEnumerable<T> CloneList(IList<T> list)
72-
{
73-
// when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
74-
// since we can't really put those constraints on T I'm going to do it via reflection
75-
76-
var type = typeof(T);
77-
var properties = type.GetProperties();
78-
79-
var clonedList = new List<T>(list.Count);
80-
81-
foreach (T item in list)
82-
{
83-
var newItem = new T();
84-
foreach (var propInfo in properties)
85-
{
86-
propInfo.SetValue(newItem, propInfo.GetValue(item, null), null);
87-
}
88-
89-
clonedList.Add(newItem);
90-
}
91-
92-
return clonedList;
93-
}
72+
// private static IEnumerable<T> CloneList(IList<T> list)
73+
// {
74+
// // when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
75+
// // since we can't really put those constraints on T I'm going to do it via reflection
76+
//
77+
// var type = typeof(T);
78+
// var properties = type.GetProperties();
79+
//
80+
// var clonedList = new List<T>(list.Count);
81+
//
82+
// foreach (T item in list)
83+
// {
84+
// var newItem = new T();
85+
// foreach (var propInfo in properties)
86+
// {
87+
// propInfo.SetValue(newItem, propInfo.GetValue(item, null), null);
88+
// }
89+
//
90+
// clonedList.Add(newItem);
91+
// }
92+
//
93+
// return clonedList;
94+
// }
9495

9596
protected override void AddItem(T entity)
9697
{
@@ -110,7 +111,7 @@ protected override void DeleteItem(T entity)
110111
TKey pkValue;
111112
GetPrimaryKey(entity, out pkValue);
112113

113-
var index = Items.ToList().FindIndex(x => MatchOnPrimaryKey(x, pkValue));
114+
var index = Items.FindIndex(x => MatchOnPrimaryKey(x, pkValue));
114115
if (index >= 0)
115116
{
116117
Items.RemoveAt(index);
@@ -122,14 +123,14 @@ protected override void UpdateItem(T entity)
122123
TKey pkValue;
123124
GetPrimaryKey(entity, out pkValue);
124125

125-
var index = _items.ToList().FindIndex(x => MatchOnPrimaryKey(x, pkValue));
126+
var index = Items.FindIndex(x => MatchOnPrimaryKey(x, pkValue));
126127
if (index >= 0)
127128
{
128-
_items[index] = entity;
129+
Items[index] = entity;
129130
}
130131
}
131132

132-
// need to match on primary key instead of using Equals() since the objects are not the same and are a cloned copy
133+
// need to match on primary key instead of using Equals() since the objects are not the same
133134
private bool MatchOnPrimaryKey(T item, TKey keyValue)
134135
{
135136
TKey value;
@@ -140,7 +141,7 @@ protected override void SaveChanges()
140141
{
141142
var writer = new StreamWriter(_storagePath, false);
142143
var serializer = new XmlSerializer(typeof(List<T>));
143-
serializer.Serialize(writer, _items);
144+
serializer.Serialize(writer, Items);
144145
writer.Close();
145146
}
146147

@@ -158,10 +159,10 @@ private TKey GeneratePrimaryKey()
158159

159160
if (typeof(TKey) == typeof(string))
160161
{
161-
return (TKey)Convert.ChangeType("ABC"+ Guid.NewGuid().ToString("N"), typeof(TKey));
162+
return (TKey)Convert.ChangeType(Guid.NewGuid().ToString("N"), typeof(TKey));
162163
}
163164

164-
var last = _items.LastOrDefault() ?? new T();
165+
var last = Items.LastOrDefault() ?? new T();
165166

166167
if (typeof(TKey) == typeof(Int32))
167168
{

0 commit comments

Comments
 (0)