Customizing the Application Modules: Overriding Services

You may need to change behavior (business logic) of a depended module for your application. In this case, you can use the power of the dependency injection system to replace a service, controller or even a page model of the depended module by your own implementation.

Replacing a service is possible for any type of class registered to the dependency injection, including services of the ABP Framework.

You have different options can be used based on your requirement those will be explained in the next sections.

Notice that some service methods may not be virtual, so you may not be able to override. We make all virtual by design. If you find any method that is not overridable, please create an issue or do it yourself and send a pull request on GitHub.

Replacing an Interface

If given service defines an interface, like the IdentityUserAppService class implements the IIdentityUserAppService, you can re-implement the same interface and replace the current implementation by your class. Example:

  1. public class MyIdentityUserAppService : IIdentityUserAppService, ITransientDependency
  2. {
  3. //...
  4. }

MyIdentityUserAppService replaces the IIdentityUserAppService by naming convention (since both ends with IdentityUserAppService). If your class name doesn’t match, you need to manually expose the service interface:

  1. [ExposeServices(typeof(IIdentityUserAppService))]
  2. public class TestAppService : IIdentityUserAppService, ITransientDependency
  3. {
  4. //...
  5. }

The dependency injection system allows to register multiple services for the same interface. The last registered one is used when the interface is injected. It is a good practice to explicitly replace the service.

Example:

  1. [Dependency(ReplaceServices = true)]
  2. [ExposeServices(typeof(IIdentityUserAppService))]
  3. public class TestAppService : IIdentityUserAppService, ITransientDependency
  4. {
  5. //...
  6. }

In this way, there will be a single implementation of the IIdentityUserAppService interface, while it doesn’t change the result for this case. Replacing a service is also possible by code:

  1. context.Services.Replace(
  2. ServiceDescriptor.Transient<IIdentityUserAppService, MyIdentityUserAppService>()
  3. );

You can write this inside the ConfigureServices method of your module.

Overriding a Service Class

In most cases, you will want to change one or a few methods of the current implementation for a service. Re-implementing the complete interface would not be efficient in this case. As a better approach, inherit from the original class and override the desired method.

Example: Overriding an Application Service

  1. //[RemoteService(IsEnabled = false)] // If you use dynamic controller feature you can disable remote service. Prevent creating duplicate controller for the application service.
  2. [Dependency(ReplaceServices = true)]
  3. [ExposeServices(typeof(IIdentityUserAppService), typeof(IdentityUserAppService))]
  4. public class MyIdentityUserAppService : IdentityUserAppService
  5. {
  6. //...
  7. public MyIdentityUserAppService(
  8. IdentityUserManager userManager,
  9. IIdentityUserRepository userRepository,
  10. IGuidGenerator guidGenerator
  11. ) : base(
  12. userManager,
  13. userRepository,
  14. guidGenerator)
  15. {
  16. }
  17. public override async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
  18. {
  19. if (input.PhoneNumber.IsNullOrWhiteSpace())
  20. {
  21. throw new AbpValidationException(
  22. "Phone number is required for new users!",
  23. new List<ValidationResult>
  24. {
  25. new ValidationResult(
  26. "Phone number can not be empty!",
  27. new []{"PhoneNumber"}
  28. )
  29. }
  30. ); }
  31. return await base.CreateAsync(input);
  32. }
  33. }

This class overrides the CreateAsync method of the IdentityUserAppService application service to check the phone number. Then calls the base method to continue to the underlying business logic. In this way, you can perform additional business logic before and after the base logic.

You could completely re-write the entire business logic for a user creation without calling the base method.

Example: Overriding a Domain Service

  1. [Dependency(ReplaceServices = true)]
  2. [ExposeServices(typeof(IdentityUserManager))]
  3. public class MyIdentityUserManager : IdentityUserManager
  4. {
  5. public MyIdentityUserManager(
  6. IdentityUserStore store,
  7. IIdentityRoleRepository roleRepository,
  8. IIdentityUserRepository userRepository,
  9. IOptions<IdentityOptions> optionsAccessor,
  10. IPasswordHasher<IdentityUser> passwordHasher,
  11. IEnumerable<IUserValidator<IdentityUser>> userValidators,
  12. IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators,
  13. ILookupNormalizer keyNormalizer,
  14. IdentityErrorDescriber errors,
  15. IServiceProvider services,
  16. ILogger<IdentityUserManager> logger,
  17. ICancellationTokenProvider cancellationTokenProvider) :
  18. base(store,
  19. roleRepository,
  20. userRepository,
  21. optionsAccessor,
  22. passwordHasher,
  23. userValidators,
  24. passwordValidators,
  25. keyNormalizer,
  26. errors,
  27. services,
  28. logger,
  29. cancellationTokenProvider)
  30. {
  31. }
  32. public override async Task<IdentityResult> CreateAsync(IdentityUser user)
  33. {
  34. if (user.PhoneNumber.IsNullOrWhiteSpace())
  35. {
  36. throw new AbpValidationException(
  37. "Phone number is required for new users!",
  38. new List<ValidationResult>
  39. {
  40. new ValidationResult(
  41. "Phone number can not be empty!",
  42. new []{"PhoneNumber"}
  43. )
  44. }
  45. );
  46. }
  47. return await base.CreateAsync(user);
  48. }
  49. }

This example class inherits from the IdentityUserManager domain service and overrides the CreateAsync method to perform the same phone number check implemented above. The result is same, but this time we’ve implemented it inside the domain service assuming that this is a core domain logic for our system.

[ExposeServices(typeof(IdentityUserManager))] attribute is required here since IdentityUserManager does not define an interface (like IIdentityUserManager) and dependency injection system doesn’t expose services for inherited classes (like it does for the implemented interfaces) by convention.

Check the localization system to learn how to localize the error messages.

Overriding Other Classes

Overriding controllers, framework services, view component classes and any other type of classes registered to dependency injection can be overridden just like the examples above.

Extending Data Transfer Objects

Extending entities is possible as described in the Extending Entities document. In this way, you can add custom properties to entities and perform additional business logic by overriding the related services as described above.

It is also possible to extend Data Transfer Objects (DTOs) used by the application services. In this way, you can get extra properties from the UI (or client) and return extra properties from the service.

Example

Assuming that you’ve already added a SocialSecurityNumber as described in the Extending Entities document and want to include this information while getting the list of users from the GetListAsync method of the IdentityUserAppService.

You can use the object extension system to add the property to the IdentityUserDto. Write this code inside the YourProjectNameDtoExtensions class comes with the application startup template:

  1. ObjectExtensionManager.Instance
  2. .AddOrUpdateProperty<IdentityUserDto, string>(
  3. "SocialSecurityNumber"
  4. );

This code defines a SocialSecurityNumber to the IdentityUserDto class as a string type. That’s all. Now, if you call the /api/identity/users HTTP API (which uses the IdentityUserAppService internally) from a REST API client, you will see the SocialSecurityNumber value in the extraProperties section.

  1. {
  2. "totalCount": 1,
  3. "items": [{
  4. "tenantId": null,
  5. "userName": "admin",
  6. "name": "admin",
  7. "surname": null,
  8. "email": "[email protected]",
  9. "emailConfirmed": false,
  10. "phoneNumber": null,
  11. "phoneNumberConfirmed": false,
  12. "twoFactorEnabled": false,
  13. "lockoutEnabled": true,
  14. "lockoutEnd": null,
  15. "concurrencyStamp": "b4c371a0ab604de28af472fa79c3b70c",
  16. "isDeleted": false,
  17. "deleterId": null,
  18. "deletionTime": null,
  19. "lastModificationTime": "2020-04-09T21:25:47.0740706",
  20. "lastModifierId": null,
  21. "creationTime": "2020-04-09T21:25:46.8308744",
  22. "creatorId": null,
  23. "id": "8edecb8f-1894-a9b1-833b-39f4725db2a3",
  24. "extraProperties": {
  25. "SocialSecurityNumber": "123456789"
  26. }
  27. }]
  28. }

Manually added the 123456789 value to the database for now.

All pre-built modules support extra properties in their DTOs, so you can configure easily.

Definition Check

When you define an extra property for an entity, it doesn’t automatically appear in all the related DTOs, because of the security. The extra property may contain a sensitive data and you may not want to expose it to the clients by default.

So, you need to explicitly define the same property for the corresponding DTO if you want to make it available for the DTO (as just done above). If you want to allow to set it on user creation, you also need to define it for the IdentityUserCreateDto.

If the property is not so secure, this can be tedious. Object extension system allows you to ignore this definition check for a desired property. See the example below:

  1. ObjectExtensionManager.Instance
  2. .AddOrUpdateProperty<IdentityUser, string>(
  3. "SocialSecurityNumber",
  4. options =>
  5. {
  6. options.MapEfCore(b => b.HasMaxLength(32));
  7. options.CheckPairDefinitionOnMapping = false;
  8. }
  9. );

This is another approach to define a property for an entity (ObjectExtensionManager has more, see its document). This time, we set CheckPairDefinitionOnMapping to false to skip definition check while mapping entities to DTOs and vice verse.

If you don’t like this approach but want to add a single property to multiple objects (DTOs) easier, AddOrUpdateProperty can get an array of types to add the extra property:

  1. ObjectExtensionManager.Instance
  2. .AddOrUpdateProperty<string>(
  3. new[]
  4. {
  5. typeof(IdentityUserDto),
  6. typeof(IdentityUserCreateDto),
  7. typeof(IdentityUserUpdateDto)
  8. },
  9. "SocialSecurityNumber"
  10. );

About the User Interface

This system allows you to add extra properties to entities and DTOs and execute custom business code, however it does nothing related to the User Interface.

See Overriding the User Interface guide for the UI part.

How to Find the Services?

Module documents includes the list of the major services they define. In addition, you can investigate their source code to explore all the services.