Skip to content

Commit

Permalink
Fixes #123
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Jul 22, 2018
1 parent 3437640 commit a8d4df9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
16 changes: 16 additions & 0 deletions AutoDI.Tests/ContainerMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@ public void CanResolveMultipleArrayContructoreParameter()
@class.Parameter.Select(x => x.GetType()).ToArray());
}

[TestMethod]
public void CanRemovedRegisteredMap()
{
var map = new ContainerMap();
var services = new AutoDIServiceCollection();
services.AddAutoDISingleton<IInterface, Derived1>();
services.AddAutoDISingleton<IInterface, Derived2>();
map.Add(services);

bool wasRemoved = map.Remove<IInterface>();
var interfaces = map.Get<IEnumerable<IInterface>>(null);

Assert.IsTrue(wasRemoved);
Assert.IsNull(interfaces);
}

private interface IInterface { }

private interface IInterface2 { }
Expand Down
11 changes: 7 additions & 4 deletions AutoDI/ContainerMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ static ContainerMap()

public void Add(IServiceCollection services)
{
////TODO: This re-grouping seems off somewhere...
foreach (IGrouping<Type, ServiceDescriptor> serviceDescriptors in
from ServiceDescriptor service in services
let autoDIService = service as AutoDIServiceDescriptor
Expand Down Expand Up @@ -63,6 +62,10 @@ private void AddInternal(DelegateContainer container, Type key)
}
}

public bool Remove<T>() => Remove(typeof(T));

public bool Remove(Type serviceType) => _accessors.Remove(serviceType);

public T Get<T>(IServiceProvider provider)
{
//https://github.com/Keboo/DoubleDownWat
Expand All @@ -74,15 +77,15 @@ public T Get<T>(IServiceProvider provider)
return default(T);
}

public object Get(Type key, IServiceProvider provider)
public object Get(Type serviceType, IServiceProvider provider)
{
if (TryGet(key, provider ?? new ContainerServiceProvider(this), out object result))
if (TryGet(serviceType, provider ?? new ContainerServiceProvider(this), out object result))
{
return result;
}

//Type key not found
var args = new TypeKeyNotFoundEventArgs(key);
var args = new TypeKeyNotFoundEventArgs(serviceType);
TypeKeyNotFound?.Invoke(this, args);
return args.Instance;
}
Expand Down
5 changes: 4 additions & 1 deletion AutoDI/IContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public interface IContainer : IEnumerable<Map>
event EventHandler<TypeKeyNotFoundEventArgs> TypeKeyNotFound;

T Get<T>(IServiceProvider provider);
object Get(Type key, IServiceProvider provider);
object Get(Type serviceType, IServiceProvider provider);

bool Remove<T>();
bool Remove(Type serviceType);

IContainer CreatedNestedContainer();
}
Expand Down
6 changes: 3 additions & 3 deletions AutoDI/TypeKeyNotFoundEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace AutoDI
{
public class TypeKeyNotFoundEventArgs : EventArgs
{
public Type ServiceKey { get; }
public Type ServiceType { get; }

public object Instance { get; set; }

public TypeKeyNotFoundEventArgs(Type serviceKey)
public TypeKeyNotFoundEventArgs(Type serviceType)
{
ServiceKey = serviceKey;
ServiceType = serviceType;
}
}
}

0 comments on commit a8d4df9

Please sign in to comment.