Skip to content

Commit b83a538

Browse files
committed
ForceInitializeTest green
1 parent 3834c49 commit b83a538

File tree

6 files changed

+121
-308
lines changed

6 files changed

+121
-308
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
using System;
21
using NHibernate.Envers.Entities.Mapper.Relation.Lazy.Initializor;
32
using NHibernate.Envers.Entities.Mapper.Relation.Lazy.Proxy;
43

54
namespace NHibernate.Envers.Tests.NetSpecific.Integration.CustomMapping.UserCollection
65
{
76
public class SpecialProxyCollectionType : ListProxy<Number>, ISpecialCollection
87
{
9-
private Lazy<ISpecialCollection> _delegate;
10-
118
public SpecialProxyCollectionType(IInitializor initializor) : base(initializor)
129
{
13-
_delegate = new Lazy<ISpecialCollection>(() => (ISpecialCollection) initializor.Initialize());
1410
}
1511

1612
public int ItemsOverLimit()
1713
{
18-
return _delegate.Value.ItemsOverLimit();
14+
return GetCollection<ISpecialCollection>().ItemsOverLimit();
1915
}
2016

21-
public int Limit => _delegate.Value.Limit;
17+
public int Limit => GetCollection<ISpecialCollection>().Limit;
2218
}
2319
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using NHibernate.Collection;
7+
using NHibernate.Envers.Entities.Mapper.Relation.Lazy.Initializor;
8+
9+
namespace NHibernate.Envers.Entities.Mapper.Relation.Lazy.Proxy
10+
{
11+
[Serializable]
12+
public class CollectionProxy<T> : ICollection<T>, ILazyInitializedCollection
13+
{
14+
[NonSerialized]
15+
private readonly IInitializor _initializor;
16+
private ICollection<T> _collection;
17+
18+
protected CollectionProxy(IInitializor initializor)
19+
{
20+
_initializor = initializor;
21+
}
22+
23+
protected TConcrete GetCollection<TConcrete>()
24+
{
25+
if (_collection == null)
26+
{
27+
_collection = (ICollection<T>) _initializor.Initialize();
28+
}
29+
30+
return (TConcrete) _collection;
31+
}
32+
33+
34+
public IEnumerator<T> GetEnumerator()
35+
{
36+
return GetCollection<ICollection<T>>().GetEnumerator();
37+
}
38+
39+
IEnumerator IEnumerable.GetEnumerator()
40+
{
41+
return GetEnumerator();
42+
}
43+
44+
public void Add(T item)
45+
{
46+
GetCollection<ICollection<T>>().Add(item);
47+
}
48+
49+
public void Clear()
50+
{
51+
GetCollection<ICollection<T>>().Clear();
52+
}
53+
54+
public bool Contains(T item)
55+
{
56+
return GetCollection<ICollection<T>>().Contains(item);
57+
}
58+
59+
public void CopyTo(T[] array, int arrayIndex)
60+
{
61+
GetCollection<ICollection<T>>().CopyTo(array, arrayIndex);
62+
}
63+
64+
public bool Remove(T item)
65+
{
66+
return GetCollection<ICollection<T>>().Remove(item);
67+
}
68+
69+
public int Count => GetCollection<ICollection<T>>().Count;
70+
71+
public bool IsReadOnly => GetCollection<ICollection<T>>().IsReadOnly;
72+
73+
74+
public Task ForceInitializationAsync(CancellationToken cancellationToken)
75+
{
76+
ForceInitialization();
77+
return Task.CompletedTask;
78+
}
79+
80+
public void ForceInitialization()
81+
{
82+
GetCollection<ICollection<T>>();
83+
}
84+
85+
public bool WasInitialized => _collection != null;
86+
}
87+
}

Src/NHibernate.Envers/Entities/Mapper/Relation/Lazy/Proxy/DictionaryProxy.cs

Lines changed: 10 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,139 +6,40 @@
66
namespace NHibernate.Envers.Entities.Mapper.Relation.Lazy.Proxy
77
{
88
[Serializable]
9-
public class DictionaryProxy<K, V> : IDictionary<K, V>
9+
public class DictionaryProxy<K, V> : CollectionProxy<KeyValuePair<K, V>>, IDictionary<K, V>
1010
{
11-
[NonSerialized]
12-
private readonly IInitializor _initializor;
13-
private IDictionary<K, V> _delegate;
14-
15-
public DictionaryProxy(IInitializor initializor)
11+
public DictionaryProxy(IInitializor initializor) : base(initializor)
1612
{
17-
_initializor = initializor;
18-
}
19-
20-
private void checkInit()
21-
{
22-
if (_delegate == null)
23-
{
24-
_delegate = (IDictionary<K, V>) _initializor.Initialize();
25-
}
26-
}
27-
28-
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
29-
{
30-
checkInit();
31-
return _delegate.GetEnumerator();
32-
}
33-
34-
IEnumerator IEnumerable.GetEnumerator()
35-
{
36-
checkInit();
37-
return GetEnumerator();
38-
}
39-
40-
public void Add(KeyValuePair<K, V> item)
41-
{
42-
checkInit();
43-
_delegate.Add(item);
44-
}
45-
46-
public void Clear()
47-
{
48-
checkInit();
49-
_delegate.Clear();
50-
}
51-
52-
public bool Contains(KeyValuePair<K, V> item)
53-
{
54-
checkInit();
55-
return _delegate.Contains(item);
56-
}
57-
58-
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
59-
{
60-
checkInit();
61-
_delegate.CopyTo(array, arrayIndex);
62-
}
63-
64-
public bool Remove(KeyValuePair<K, V> item)
65-
{
66-
checkInit();
67-
return _delegate.Remove(item);
68-
}
69-
70-
public int Count
71-
{
72-
get
73-
{
74-
checkInit();
75-
return _delegate.Count;
76-
}
77-
}
78-
79-
public bool IsReadOnly
80-
{
81-
get
82-
{
83-
checkInit();
84-
return _delegate.IsReadOnly;
85-
}
8613
}
8714

8815
public void Add(K key, V value)
8916
{
90-
checkInit();
91-
_delegate.Add(key, value);
17+
GetCollection<IDictionary<K, V>>().Add(key, value);
9218
}
9319

9420
public bool ContainsKey(K key)
9521
{
96-
checkInit();
97-
return _delegate.ContainsKey(key);
22+
return GetCollection<IDictionary<K, V>>().ContainsKey(key);
9823
}
9924

10025
public bool Remove(K key)
10126
{
102-
checkInit();
103-
return _delegate.Remove(key);
27+
return GetCollection<IDictionary<K, V>>().Remove(key);
10428
}
10529

10630
public bool TryGetValue(K key, out V value)
10731
{
108-
checkInit();
109-
return _delegate.TryGetValue(key, out value);
32+
return GetCollection<IDictionary<K, V>>().TryGetValue(key, out value);
11033
}
11134

11235
public V this[K key]
11336
{
114-
get
115-
{
116-
checkInit();
117-
return _delegate[key];
118-
}
119-
set
120-
{
121-
checkInit();
122-
_delegate[key] = value;
123-
}
37+
get => GetCollection<IDictionary<K, V>>()[key];
38+
set => GetCollection<IDictionary<K, V>>()[key] = value;
12439
}
12540

126-
public ICollection<K> Keys
127-
{
128-
get
129-
{
130-
checkInit();
131-
return _delegate.Keys;
132-
}
133-
}
41+
public ICollection<K> Keys => GetCollection<IDictionary<K, V>>().Keys;
13442

135-
public ICollection<V> Values
136-
{
137-
get
138-
{
139-
checkInit();
140-
return _delegate.Values;
141-
}
142-
}
43+
public ICollection<V> Values => GetCollection<IDictionary<K, V>>().Values;
14344
}
14445
}
Lines changed: 7 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,35 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using NHibernate.Envers.Entities.Mapper.Relation.Lazy.Initializor;
54

65
namespace NHibernate.Envers.Entities.Mapper.Relation.Lazy.Proxy
76
{
87
[Serializable]
9-
public class ListProxy<T> : IList<T>
8+
public class ListProxy<T> : CollectionProxy<T>, IList<T>
109
{
11-
[NonSerialized]
12-
private readonly IInitializor _initializor;
13-
private IList<T> _delegate;
14-
15-
public ListProxy(IInitializor initializor)
10+
public ListProxy(IInitializor initializor) : base(initializor)
1611
{
17-
_initializor = initializor;
18-
}
19-
20-
private void checkInit()
21-
{
22-
if (_delegate == null)
23-
{
24-
_delegate = (IList<T>) _initializor.Initialize();
25-
}
26-
}
27-
28-
public IEnumerator<T> GetEnumerator()
29-
{
30-
checkInit();
31-
return _delegate.GetEnumerator();
32-
}
33-
34-
IEnumerator IEnumerable.GetEnumerator()
35-
{
36-
checkInit();
37-
return GetEnumerator();
38-
}
39-
40-
public void Add(T item)
41-
{
42-
checkInit();
43-
_delegate.Add(item);
44-
}
45-
46-
public void Clear()
47-
{
48-
checkInit();
49-
_delegate.Clear();
50-
}
51-
52-
public bool Contains(T item)
53-
{
54-
checkInit();
55-
return _delegate.Contains(item);
56-
}
57-
58-
public void CopyTo(T[] array, int arrayIndex)
59-
{
60-
checkInit();
61-
_delegate.CopyTo(array, arrayIndex);
62-
}
63-
64-
public bool Remove(T item)
65-
{
66-
checkInit();
67-
return _delegate.Remove(item);
68-
}
69-
70-
public int Count
71-
{
72-
get
73-
{
74-
checkInit();
75-
return _delegate.Count;
76-
}
77-
}
78-
79-
public bool IsReadOnly
80-
{
81-
get
82-
{
83-
checkInit();
84-
return _delegate.IsReadOnly;
85-
}
8612
}
8713

8814
public int IndexOf(T item)
8915
{
90-
checkInit();
91-
return _delegate.IndexOf(item);
16+
return GetCollection<IList<T>>().IndexOf(item);
9217
}
9318

9419
public void Insert(int index, T item)
9520
{
96-
checkInit();
97-
_delegate.Insert(index, item);
21+
GetCollection<IList<T>>().Insert(index, item);
9822
}
9923

10024
public void RemoveAt(int index)
10125
{
102-
checkInit();
103-
_delegate.RemoveAt(index);
26+
GetCollection<IList<T>>().RemoveAt(index);
10427
}
10528

10629
public T this[int index]
10730
{
108-
get
109-
{
110-
checkInit();
111-
return _delegate[index];
112-
}
113-
set
114-
{
115-
checkInit();
116-
_delegate[index] = value;
117-
}
31+
get => GetCollection<IList<T>>()[index];
32+
set => GetCollection<IList<T>>()[index] = value;
11833
}
11934
}
12035
}

0 commit comments

Comments
 (0)