Skip to content

Commit

Permalink
add remove api (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnaner authored Mar 26, 2020
1 parent 9c642ce commit 4b10b34
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface IServiceContext : IEnumerable<ServiceDefinition>

void Add(ServiceDefinition item);

bool Remove(ServiceDefinition item);

bool Contains(Type serviceType);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace AspectCore.DependencyInjection
{
Expand Down Expand Up @@ -62,5 +63,26 @@ public static IServiceContext AddDelegate<TService>(this IServiceContext service
serviceContext.Add(new DelegateServiceDefinition(typeof(TService), implementationDelegate, lifetime));
return serviceContext;
}

public static IServiceContext RemoveAll<TService>(this IServiceContext serviceContext) where TService : class
{
return RemoveAll(serviceContext, typeof(TService));
}

public static IServiceContext RemoveAll(this IServiceContext serviceContext, Type serviceType)
{
var serviceDefinitions = new List<ServiceDefinition>();
foreach (var serviceDefinition in serviceContext)
{
if (serviceDefinition.ServiceType == serviceType)
{
serviceDefinitions.Add(serviceDefinition);
}
}

serviceDefinitions.ForEach(t => serviceContext.Remove(t));
return serviceContext;
}

}
}
2 changes: 2 additions & 0 deletions src/AspectCore.Core/DependencyInjection/ServiceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ private void AddInternalServices()

public void Add(ServiceDefinition item) => _collection.Add(item);

public bool Remove(ServiceDefinition item) => _collection.Remove(item);

public bool Contains(Type serviceType) => _collection.Any(x => x.ServiceType == serviceType);

public IEnumerator<ServiceDefinition> GetEnumerator() => _collection.GetEnumerator();
Expand Down

0 comments on commit 4b10b34

Please sign in to comment.