Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit dc600a6

Browse files
committed
Support removing features from FeatureCollection.
1 parent 2352bd7 commit dc600a6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Microsoft.AspNet.FeatureModel/FeatureCollection.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public object GetInterface(Type type)
6666
void SetInterface(Type type, object feature)
6767
{
6868
if (type == null) throw new ArgumentNullException("type");
69-
if (feature == null) throw new ArgumentNullException("feature");
69+
70+
if (feature == null)
71+
{
72+
Remove(type);
73+
return;
74+
}
7075

7176
lock (_containerSync)
7277
{
@@ -167,7 +172,20 @@ public void Add(Type key, object value)
167172

168173
public bool Remove(Type key)
169174
{
170-
throw new NotImplementedException();
175+
if (key == null) throw new ArgumentNullException("key");
176+
177+
lock (_containerSync)
178+
{
179+
Type priorFeatureType;
180+
if (_featureTypeByName.TryGetValue(key.FullName, out priorFeatureType))
181+
{
182+
_featureTypeByName.Remove(key.FullName);
183+
_featureByFeatureType.Remove(priorFeatureType);
184+
Interlocked.Increment(ref _containerRevision);
185+
return true;
186+
}
187+
return false;
188+
}
171189
}
172190

173191
public bool TryGetValue(Type key, out object value)

test/Microsoft.AspNet.FeatureModel.Tests/InterfaceDictionaryTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,36 @@ public void SecondCallToAddThrowsException()
4848

4949
Assert.Throws<ArgumentException>(() => interfaces.Add(typeof(IThing), thing));
5050
}
51+
52+
[Fact]
53+
public void RemovedInterfaceIsRemoved()
54+
{
55+
var interfaces = new FeatureCollection();
56+
var thing = new Thing();
57+
58+
interfaces.Add(typeof(IThing), thing);
59+
60+
Assert.Equal(interfaces[typeof(IThing)], thing);
61+
62+
Assert.True(interfaces.Remove(typeof(IThing)));
63+
64+
object thing2;
65+
Assert.False(interfaces.TryGetValue(typeof(IThing), out thing2));
66+
}
67+
68+
[Fact]
69+
public void SetNullValueRemoves()
70+
{
71+
var interfaces = new FeatureCollection();
72+
var thing = new Thing();
73+
74+
interfaces.Add(typeof(IThing), thing);
75+
Assert.Equal(interfaces[typeof(IThing)], thing);
76+
77+
interfaces[typeof(IThing)] = null;
78+
79+
object thing2;
80+
Assert.False(interfaces.TryGetValue(typeof(IThing), out thing2));
81+
}
5182
}
5283
}

0 commit comments

Comments
 (0)