Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inversify #26

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"react-redux": "^8.0.5",
"react-router-dom": "^6.4.3",
"rxjs": "^7.5.7",
"styled-components": "^5.3.6"
"styled-components": "^5.3.6",
"inversify": "^6.0.1",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
Expand Down
171 changes: 0 additions & 171 deletions src/core/ServiceLocator.ts

This file was deleted.

79 changes: 79 additions & 0 deletions src/core/diContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Container } from "inversify";
import {
CountryRepository,
FavoriteRepository,
ProductRepository,
SearchRepository,
GetCurrentCountryUseCase,
UpdateCurrentCountryUseCase,
AddFavoriteUseCase,
GetFavoritesUseCase,
GetProductsUseCase,
RemoveFavoriteUseCase,
AddLastSearchUseCase,
CleanSearchUseCase,
GetLastSearchUseCase,
} from "../domain";
import {
axiosInstance,
CountryRepositoryImpl,
FavoriteRepositoryImpl,
LocalStorage,
ProductRepositoryImpl,
SearchRepositoryImpl,
} from "../data";
import diService from "./diService";

const configRepositories = (container: Container) => {
container
.bind<CountryRepository>(diService.CountryRepository)
.toConstantValue(new CountryRepositoryImpl(new LocalStorage(localStorage)));
container
.bind<FavoriteRepository>(diService.FavoriteRepository)
.toConstantValue(
new FavoriteRepositoryImpl(new LocalStorage(localStorage))
);
container
.bind<ProductRepository>(diService.ProductRepository)
.toConstantValue(new ProductRepositoryImpl(axiosInstance));
container
.bind<SearchRepository>(diService.SearchRepository)
.toConstantValue(
new SearchRepositoryImpl(new LocalStorage(sessionStorage))
);
};

const configUseCases = (container: Container) => {
container
.bind<GetCurrentCountryUseCase>(diService.GetCurrentCountryUseCase)
.to(GetCurrentCountryUseCase);
container
.bind<UpdateCurrentCountryUseCase>(diService.UpdateCurrentCountryUseCase)
.to(UpdateCurrentCountryUseCase);
container
.bind<AddFavoriteUseCase>(diService.AddFavoriteUseCase)
.to(AddFavoriteUseCase);
container
.bind<GetFavoritesUseCase>(diService.GetFavoritesUseCase)
.to(GetFavoritesUseCase);
container
.bind<GetProductsUseCase>(diService.GetProductsUseCase)
.to(GetProductsUseCase);
container
.bind<RemoveFavoriteUseCase>(diService.RemoveFavoriteUseCase)
.to(RemoveFavoriteUseCase);
container
.bind<AddLastSearchUseCase>(diService.AddLastSearchUseCase)
.to(AddLastSearchUseCase);
container
.bind<CleanSearchUseCase>(diService.CleanSearchUseCase)
.to(CleanSearchUseCase);
container
.bind<GetLastSearchUseCase>(diService.GetLastSearchUseCase)
.to(GetLastSearchUseCase);
};

const diContainer = new Container();
configRepositories(diContainer);
configUseCases(diContainer);
export { diContainer };
21 changes: 21 additions & 0 deletions src/core/diService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "reflect-metadata";

const diService = {
//repositories
CountryRepository: Symbol("CountryRepository"),
FavoriteRepository: Symbol("FavoriteRepository"),
ProductRepository: Symbol("ProductRepository"),
SearchRepository: Symbol("SearchRepository"),
//use cases
GetCurrentCountryUseCase: Symbol("GetCurrentCountryUseCase"),
UpdateCurrentCountryUseCase: Symbol("UpdateCurrentCountryUseCase"),
AddFavoriteUseCase: Symbol("AddFavoriteUseCase"),
GetFavoritesUseCase: Symbol("GetFavoritesUseCase"),
GetProductsUseCase: Symbol("GetProductsUseCase"),
RemoveFavoriteUseCase: Symbol("RemoveFavoriteUseCase"),
AddLastSearchUseCase: Symbol("AddLastSearchUseCase"),
CleanSearchUseCase: Symbol("CleanSearchUseCase"),
GetLastSearchUseCase: Symbol("GetLastSearchUseCase"),
};

export default diService;
3 changes: 2 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./diContainer";
export * from "./diService";
export * from "./i18n";
export * from "./ServiceLocator";
10 changes: 6 additions & 4 deletions src/domain/usecases/country/GetCurrentCountryUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { inject, injectable } from "inversify";
import {
analyzeException,
Country,
CountryRepository,
DataResult,
Left,
} from "@/domain";
import diService from "@/core/diService";

@injectable()
export class GetCurrentCountryUseCase {
private repository: CountryRepository;
constructor(_repository: CountryRepository) {
this.repository = _repository;
}
constructor(
@inject(diService.CountryRepository) private repository: CountryRepository
) {}

invoke(): DataResult<Country> {
try {
Expand Down
10 changes: 6 additions & 4 deletions src/domain/usecases/country/UpdateCurrentCountryUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { inject, injectable } from "inversify";
import {
analyzeException,
Country,
CountryRepository,
DataResult,
Left,
} from "@/domain";
import diService from "@/core/diService";

@injectable()
export class UpdateCurrentCountryUseCase {
private repository: CountryRepository;
constructor(_repository: CountryRepository) {
this.repository = _repository;
}
constructor(
@inject(diService.CountryRepository) private repository: CountryRepository
) {}

invoke(countryCode: string): DataResult<Country> {
try {
Expand Down
Loading