@ -244,25 +244,35 @@ One restriction of property injection is that you cannot use the dependency in y
Property injection is also useful when you want to design a base class that has some common services injected by default. If you're going to use constructor injection, all derived classes should also inject depended services into their own constructors which makes development harder. However, be very careful using property injection for non-optional services as it makes it harder to clearly see the requirements of a class.
Property injection is also useful when you want to design a base class that has some common services injected by default. If you're going to use constructor injection, all derived classes should also inject depended services into their own constructors which makes development harder. However, be very careful using property injection for non-optional services as it makes it harder to clearly see the requirements of a class.
### Resolve Service from IServiceProvider
#### DisablePropertyInjectionAttribute
You may want to resolve a service directly from ``IServiceProvider``. In that case, you can inject IServiceProvider into your class and use ``GetService`` method as shown below:
You can use `[DisablePropertyInjection]` attribute on class or properties to disable property injection for the whole class or some specific properties.
public MyService(IServiceProvider serviceProvider)
public class MyService : ITransientDependency
{
{
_serviceProvider = serviceProvider;
public ILogger<MyService> Logger { get; set; }
}
public void DoSomething()
[DisablePropertyInjection]
{
public ITaxCalculator TaxCalculator { get; set; }
var taxCalculator = _serviceProvider.GetService<ITaxCalculator>();
}
//...
}
````
### Resolve Service from IServiceProvider
You may want to resolve a service directly from ``IServiceProvider``. In that case, you can inject IServiceProvider into your class and use ``GetService`` method as shown below:
@ -67,10 +68,11 @@ public static class AbpRegistrationBuilderExtensions
TypeimplementationType)
TypeimplementationType)
whereTActivatorData:ReflectionActivatorData
whereTActivatorData:ReflectionActivatorData
{
{
//Enable Property Injection only for types in an assembly containing an AbpModule
// Enable Property Injection only for types in an assembly containing an AbpModule and without a DisablePropertyInjection attribute on class or properties.