mirror of https://github.com/abpframework/abp.git
4 changed files with 188 additions and 42 deletions
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using Volo.ExtensionMethods; |
|||
|
|||
namespace Volo.DependencyInjection |
|||
{ |
|||
public static class AutoRegistrationHelper |
|||
{ |
|||
public static IEnumerable<Type> GetExposedServices(Type type) |
|||
{ |
|||
var typeInfo = type.GetTypeInfo(); |
|||
|
|||
var customExposedServices = typeInfo |
|||
.GetCustomAttributes() |
|||
.OfType<IExposedServiceTypesProvider>() |
|||
.SelectMany(p => p.GetExposedServiceTypes()) |
|||
.ToList(); |
|||
|
|||
if (customExposedServices.Any()) |
|||
{ |
|||
return customExposedServices; |
|||
} |
|||
|
|||
return GetDefaultExposedServices(type); |
|||
} |
|||
|
|||
private static IEnumerable<Type> GetDefaultExposedServices(Type type) |
|||
{ |
|||
var typeInfo = type.GetTypeInfo(); |
|||
|
|||
var serviceTypes = new List<Type> { type }; |
|||
|
|||
foreach (var interfaceType in typeInfo.GetInterfaces()) |
|||
{ |
|||
var interfaceName = interfaceType.Name; |
|||
if (interfaceName.StartsWith("I")) |
|||
{ |
|||
interfaceName = interfaceName.Right(interfaceName.Length - 1); |
|||
} |
|||
|
|||
if (type.Name.EndsWith(interfaceName)) |
|||
{ |
|||
serviceTypes.Add(interfaceType); |
|||
} |
|||
} |
|||
|
|||
return serviceTypes; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
using System.Linq; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.DependencyInjection |
|||
{ |
|||
public class AutoRegistrationHelper_Tests |
|||
{ |
|||
[Fact] |
|||
public void Should_Get_Conventional_Exposed_Types_By_Default() |
|||
{ |
|||
//Act
|
|||
|
|||
var exposedServices = AutoRegistrationHelper.GetExposedServices(typeof(DefaultDerivedService)).ToList(); |
|||
|
|||
//Assert
|
|||
|
|||
exposedServices.Count.ShouldBe(3); |
|||
exposedServices.ShouldContain(typeof(DefaultDerivedService)); |
|||
exposedServices.ShouldContain(typeof(IService)); |
|||
exposedServices.ShouldContain(typeof(IDerivedService)); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Custom_Exposed_Types_If_Available() |
|||
{ |
|||
//Act
|
|||
|
|||
var exposedServices = AutoRegistrationHelper.GetExposedServices(typeof(ExplicitDerivedService)).ToList(); |
|||
|
|||
//Assert
|
|||
|
|||
exposedServices.Count.ShouldBe(1); |
|||
exposedServices.ShouldContain(typeof(IDerivedService)); |
|||
} |
|||
|
|||
public class DefaultDerivedService : IDerivedService |
|||
{ |
|||
} |
|||
|
|||
public interface IService |
|||
{ |
|||
} |
|||
|
|||
public interface IDerivedService : IService |
|||
{ |
|||
} |
|||
|
|||
[ExposeServices(typeof(IDerivedService))] |
|||
public class ExplicitDerivedService : IDerivedService |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue