mirror of https://github.com/abpframework/abp.git
committed by
GitHub
5 changed files with 159 additions and 0 deletions
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using Autofac; |
||||
|
using Autofac.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Autofac; |
||||
|
|
||||
|
public class AutoFacInjectPropertiesService : IInjectPropertiesService, ITransientDependency |
||||
|
{ |
||||
|
protected IServiceProvider ServiceProvider { get; } |
||||
|
|
||||
|
public AutoFacInjectPropertiesService(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public virtual TService InjectProperties<TService>(TService instance) where TService : notnull |
||||
|
{ |
||||
|
return InjectProperties(instance, false); |
||||
|
} |
||||
|
|
||||
|
public virtual TService InjectUnsetProperties<TService>(TService instance) where TService : notnull |
||||
|
{ |
||||
|
return InjectProperties(instance, true); |
||||
|
} |
||||
|
|
||||
|
protected virtual TService InjectProperties<TService>(TService instance, bool onlyForUnsetProperties) |
||||
|
where TService : notnull |
||||
|
{ |
||||
|
if (instance == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(nameof(instance)); |
||||
|
} |
||||
|
|
||||
|
if (ServiceProvider is not AutofacServiceProvider) |
||||
|
{ |
||||
|
throw new AbpException($"The {nameof(ServiceProvider)} must be an instance of {nameof(AutofacServiceProvider)}!"); |
||||
|
} |
||||
|
|
||||
|
return onlyForUnsetProperties |
||||
|
? ServiceProvider.As<AutofacServiceProvider>().LifetimeScope.InjectUnsetProperties(instance) |
||||
|
: ServiceProvider.As<AutofacServiceProvider>().LifetimeScope.InjectProperties(instance); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
public interface IInjectPropertiesService |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Set any properties on <paramref name="instance"/> that can be resolved by IServiceProvider.
|
||||
|
/// </summary>
|
||||
|
TService InjectProperties<TService>(TService instance) where TService : notnull; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Set any null-valued properties on <paramref name="instance"/> that can be resolved by the IServiceProvider.
|
||||
|
/// </summary>
|
||||
|
TService InjectUnsetProperties<TService>(TService instance) where TService : notnull; |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
namespace Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
[Dependency(TryRegister = true)] |
||||
|
public class NullInjectPropertiesService : IInjectPropertiesService, ITransientDependency |
||||
|
{ |
||||
|
public TService InjectProperties<TService>(TService instance) |
||||
|
where TService : notnull |
||||
|
{ |
||||
|
return instance; |
||||
|
} |
||||
|
|
||||
|
public TService InjectUnsetProperties<TService>(TService instance) |
||||
|
where TService : notnull |
||||
|
{ |
||||
|
return instance; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,67 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Autofac.Interception; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Autofac; |
||||
|
|
||||
|
public class AutoFacInjectingPropertiesService_Tests : Autofac_Interception_Test |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void AutoFacInjectingPropertiesService_Should_Replaces_NullInjectingPropertiesService() |
||||
|
{ |
||||
|
ServiceProvider.GetRequiredService<IInjectPropertiesService>().GetType().ShouldBe(typeof(AutoFacInjectPropertiesService)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void InjectProperties() |
||||
|
{ |
||||
|
var injectPropertiesService = ServiceProvider.GetRequiredService<IInjectPropertiesService>(); |
||||
|
var serviceB = new TestServiceB(); |
||||
|
injectPropertiesService.InjectProperties(serviceB); |
||||
|
|
||||
|
serviceB.NullTestServiceA.ShouldNotBeNull(); |
||||
|
serviceB.NullTestServiceA.Name.ShouldBe("Default Name"); |
||||
|
serviceB.NotNullTestServiceA.ShouldNotBeNull(); |
||||
|
serviceB.NotNullTestServiceA.Name.ShouldBe("Default Name"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void InjectUnsetProperties() |
||||
|
{ |
||||
|
var injectPropertiesService = ServiceProvider.GetRequiredService<IInjectPropertiesService>(); |
||||
|
var serviceB = new TestServiceB(); |
||||
|
injectPropertiesService.InjectUnsetProperties(serviceB); |
||||
|
|
||||
|
serviceB.NullTestServiceA.ShouldNotBeNull(); |
||||
|
serviceB.NullTestServiceA.Name.ShouldBe("Default Name"); |
||||
|
serviceB.NotNullTestServiceA.ShouldNotBeNull(); |
||||
|
serviceB.NotNullTestServiceA.Name.ShouldBe("My Name"); // This is not null property.
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
interface ITestServiceA |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
} |
||||
|
|
||||
|
class TestServiceA : ITestServiceA, ITransientDependency |
||||
|
{ |
||||
|
public string Name { get; set; } = "Default Name"; |
||||
|
} |
||||
|
|
||||
|
interface ITestServiceB |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
class TestServiceB : ITestServiceB, ITransientDependency |
||||
|
{ |
||||
|
public ITestServiceA NullTestServiceA { get; set; } |
||||
|
|
||||
|
public ITestServiceA NotNullTestServiceA { get; set; } = new TestServiceA() |
||||
|
{ |
||||
|
Name = "My Name" |
||||
|
}; |
||||
|
} |
||||
Loading…
Reference in new issue