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 IdentityRoleManager<TRole>
object.IUserManager
This is a wrapper for the ASP.Net IdentityUserManager<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 fromIGameService
toIStatsService
.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
- Removed Database access from all services and abstracted it via Repository Interfaces. These can now be mocked.
Helpers
- Introduced the
Result<T>
struct. Service methods can now return aResult<T>
which either represents the value of typeT
or anException
. 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 withIQuestionsFileAccessor
and refactored the methods. - Refactored the
GameService
- used DRY to refactor a lot and moved some functionality toIStatsService
.
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
- Seperated the ExtensionMethods in the DependecyInjection files (see AMOGUS.Core, AMOGUS.Infrastructure) to make them shorter and more cohesive.
Reference
For reference see the Pull Requests: #70, #71, #74.
Last update:
2023-06-09