Support removing features from FeatureCollection.
This commit is contained in:
parent
2352bd7ca3
commit
dc600a636a
|
|
@ -66,7 +66,12 @@ namespace Microsoft.AspNet.FeatureModel
|
||||||
void SetInterface(Type type, object feature)
|
void SetInterface(Type type, object feature)
|
||||||
{
|
{
|
||||||
if (type == null) throw new ArgumentNullException("type");
|
if (type == null) throw new ArgumentNullException("type");
|
||||||
if (feature == null) throw new ArgumentNullException("feature");
|
|
||||||
|
if (feature == null)
|
||||||
|
{
|
||||||
|
Remove(type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
lock (_containerSync)
|
lock (_containerSync)
|
||||||
{
|
{
|
||||||
|
|
@ -167,7 +172,20 @@ namespace Microsoft.AspNet.FeatureModel
|
||||||
|
|
||||||
public bool Remove(Type key)
|
public bool Remove(Type key)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
if (key == null) throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
lock (_containerSync)
|
||||||
|
{
|
||||||
|
Type priorFeatureType;
|
||||||
|
if (_featureTypeByName.TryGetValue(key.FullName, out priorFeatureType))
|
||||||
|
{
|
||||||
|
_featureTypeByName.Remove(key.FullName);
|
||||||
|
_featureByFeatureType.Remove(priorFeatureType);
|
||||||
|
Interlocked.Increment(ref _containerRevision);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetValue(Type key, out object value)
|
public bool TryGetValue(Type key, out object value)
|
||||||
|
|
|
||||||
|
|
@ -48,5 +48,36 @@ namespace Microsoft.AspNet.FeatureModel.Tests
|
||||||
|
|
||||||
Assert.Throws<ArgumentException>(() => interfaces.Add(typeof(IThing), thing));
|
Assert.Throws<ArgumentException>(() => interfaces.Add(typeof(IThing), thing));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemovedInterfaceIsRemoved()
|
||||||
|
{
|
||||||
|
var interfaces = new FeatureCollection();
|
||||||
|
var thing = new Thing();
|
||||||
|
|
||||||
|
interfaces.Add(typeof(IThing), thing);
|
||||||
|
|
||||||
|
Assert.Equal(interfaces[typeof(IThing)], thing);
|
||||||
|
|
||||||
|
Assert.True(interfaces.Remove(typeof(IThing)));
|
||||||
|
|
||||||
|
object thing2;
|
||||||
|
Assert.False(interfaces.TryGetValue(typeof(IThing), out thing2));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SetNullValueRemoves()
|
||||||
|
{
|
||||||
|
var interfaces = new FeatureCollection();
|
||||||
|
var thing = new Thing();
|
||||||
|
|
||||||
|
interfaces.Add(typeof(IThing), thing);
|
||||||
|
Assert.Equal(interfaces[typeof(IThing)], thing);
|
||||||
|
|
||||||
|
interfaces[typeof(IThing)] = null;
|
||||||
|
|
||||||
|
object thing2;
|
||||||
|
Assert.False(interfaces.TryGetValue(typeof(IThing), out thing2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue