Browse Source

Added DisablePropertyInjection Attribute with functionality

pull/13844/head
Merdan Gochmuradov 4 years ago
parent
commit
e796d03c0e
  1. 5
      framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs
  2. 8
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs
  3. 39
      framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs

5
framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs

@ -67,8 +67,9 @@ public static class AbpRegistrationBuilderExtensions
Type implementationType)
where TActivatorData : ReflectionActivatorData
{
//Enable Property Injection only for types in an assembly containing an AbpModule
if (moduleContainer.Modules.Any(m => m.Assembly == implementationType.Assembly))
//Enable Property Injection only for types in an assembly containing an AbpModule and without a DisablePropertyInjection attribute
if (moduleContainer.Modules.Any((IAbpModuleDescriptor m) => m.Assembly == implementationType.Assembly)
&& !implementationType.GetCustomAttributes(typeof(DisablePropertyInjectionAttribute), true).Any())
{
registrationBuilder = registrationBuilder.PropertiesAutowired();
}

8
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/DisablePropertyInjectionAttribute.cs

@ -0,0 +1,8 @@
using System;
namespace Volo.Abp.DependencyInjection;
[AttributeUsage(AttributeTargets.Class)]
public class DisablePropertyInjectionAttribute : Attribute
{
}

39
framework/test/Volo.Abp.Core.Tests/Microsoft/Extensions/DependencyInjection/DependencyInjection_Tests.cs

@ -62,6 +62,24 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest<Dep
GetRequiredService<ConcreteGenericServiceWithPropertyInject>().ProperyInjectedService.ShouldNotBeNull();
}
[Fact]
public void Should_Not_Inject_Services_As_Properties_When_Disabled()
{
GetRequiredService<ServiceWithPropertyInjectDisabled>().ProperyInjectedService.ShouldBeNull();
}
[Fact]
public void Should_Not_Inject_Services_As_Properties_For_Generic_Classes_When_Disabled()
{
GetRequiredService<GenericServiceWithPropertyInjectDisabled<int>>().ProperyInjectedService.ShouldBeNull();
}
[Fact]
public void Should_Not_Inject_Services_As_Properties_For_Generic_Concrete_Classes_When_Disabled()
{
GetRequiredService<ConcreteGenericServiceWithPropertyInjectDisabled>().ProperyInjectedService.ShouldBeNull();
}
[Fact]
public void Singletons_Exposing_Multiple_Services_Should_Returns_The_Same_Instance()
{
@ -145,7 +163,7 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest<Dep
context.Services.AddType<ServiceWithPropertyInject>();
context.Services.AddType<MySingletonExposingMultipleServices>();
context.Services.AddTransient(typeof(GenericServiceWithPropertyInject<>));
context.Services.AddTransient(typeof(ConcreteGenericServiceWithPropertyInject));
context.Services.AddTransient(typeof(GenericServiceWithPropertyInjectDisabled<>));
}
}
@ -165,4 +183,23 @@ public abstract class DependencyInjection_Standard_Tests : AbpIntegratedTest<Dep
{
}
[DisablePropertyInjection]
public class ServiceWithPropertyInjectDisabled : ITransientDependency
{
public MyEmptyTransientService ProperyInjectedService { get; set; }
}
[DisablePropertyInjection]
public class GenericServiceWithPropertyInjectDisabled<T> : ITransientDependency
{
public MyEmptyTransientService ProperyInjectedService { get; set; }
public T Value { get; set; }
}
public class ConcreteGenericServiceWithPropertyInjectDisabled : GenericServiceWithPropertyInjectDisabled<string>
{
}
}

Loading…
Cancel
Save