Skip to content

Backend Refactor - Summary 20.04.2023

Additional Information

For unit testing we have to mock some services and classes. For that we use the Moq library. To mock methods of a dependency, it has to be an interface. So we split some functions into new Interfaces to be able to test independently. The Single Responsibility Principle and the Dependency Inversion Principle were used for that.

Abstractions

New / Changed Interfaces

  • ITokenFactory
    Added some Methods used in the authentication process to create JWT Tokens with custom Claims. Authentication can now be mocked when Unit Tested.
  • IRoleManager
    This is a wrapper for the ASP.Net Identity RoleManager<TRole> object.
  • IUserManager
    This is a wrapper for the ASP.Net Identity UserManager<TUser> object.
  • IQuestionFileAccessor
    This is used as an abstraction for the file-access used to load and save the questions. This has to be mocked to be system independent.
  • IStatsService
    Moved some functionality from IGameService to IStatsService.
  • IDateTime
    Abstraction for the System Clock.
  • Added multiple interfaces to access environment variables which wrap the ASP.Net IConfiguration object (IJwtConfiguration, IQuestionRepoConfiguration). Configuration variables can now be mocked.

Repositories

Helpers

  • Introduced the Result<T> struct. Service methods can now return a Result<T> which either represents the value of type T or an Exception. This avoids Exception throwing (which in my opinion is bad style in this case) and makes the return of methods more meaningful and simple.
  • Added some new Exception types.

Code Changes

  • Refactored the AuthService to increase testability of the methods.
  • Refactored the UserService to have smaller Methods and better code style in general.
  • Refactored the ExerciseService - replaced file-access methods with IQuestionsFileAccessor and refactored the methods.
  • Refactored the GameService - used DRY to refactor a lot and moved some functionality to IStatsService.

Formatter

We finally introduced an .editorconfig which gives us the ability to quickly clean up our code (sort usings, proper indentations...) using the inbuilt formatter in Visual Studio.

Misc

Reference

For reference see the Pull Requests: #70, #71, #74.


Last update: 2023-06-09