Application Layer Implementation Example based on @azure-net/kit
Preparation Stage
It is assumed that all types were described in the domain layer, and the infrastructure layer was created with the repositories we need already prepared. This example is a continuation of the infrastructure layer example; the entire example has already been described in previous sections and is simply assembled here. The only thing important to understand is the purpose of the layer and that in the infrastructure layer we get data from the backend and can bring data to the format we need without using business logic. In this layer, we execute scenarios, using domain business logic and data obtained by calling methods from the infrastructure. The layer has no right to UI logic and UI interaction, and also has no right to communicate with the API.
You have the right to create orchestrators and use-cases in this layer, if you need them and simple services are not enough for you.
1. Service
import { ClassMirror } from '@azure-net/kit';
import { AuthRepository } from '${ContextName}/infrastructure/http/repositories';
import type {IUser} from '${ContextName}/domain/user';
class AuthService extends ClassMirror<AuthRepository> {
constructor(private repository: AuthRepository) {
super(repository);
}
// Perhaps in the future we will extend these methods and remove the mirroring class; for now let's leave it in this form.
declare login: AuthRepository['login'];
declare logout: AuthRepository['logout'];
async current() {
const user = await this.authRepository.current().catch(() => undefined);
if (user && this.checkStatus(user)) {
return user;
}
return undefined; // or for example, throw Error; doesn't matter, just an example
}
checkStatus(user: IUser) {
// Some scenario by status
}
}2. Provider
import { createBoundaryProvider } from '@azure-net/kit';
import { InfrastructureProvider } from '${ContextName}/infrastructure';
import { AuthService } from '../services';
export const ApplicationProvider = createBoundaryProvider('{ContextName}ApplicationProvider', {
dependsOn: { InfrastructureProvider },
register: ({ InfrastructureProvider }) => ({
AuthService: () => new AuthService(InfrastructureProvider.AuthRepository)
})
});