Last active
April 21, 2021 06:49
-
-
Save n1215/eb346762d43494dfc3f9eab2d7336e01 to your computer and use it in GitHub Desktop.
TypeScript classless DI container design
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { container, scopes } from "./library" | |
import { ListUsers, listUsersFactory } from "./listUsers" | |
import { UserRepositoryInterface } from "./UserRepoistoryInterface" | |
import { userRepositoryFactory } from "./userResistory" | |
const users = [{id: 1, name: "user1"}]; | |
container.bind<UserRepositoryInterface>().toFactory(userRepositoryFactory, {users}).in(scopes.GLOBAL) | |
container.bind<ListUsers>().toFactory(listUsersFactory).in(scopes.GLOBAL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ListUsers } from "./listUsers"; | |
const requestHandlerFactory = ({ | |
listUsers | |
}: {listUsers: ListUsers}) => | |
(request: Request, response: Response): void => { | |
const users = listUsers(); | |
response.send(users); | |
} | |
export default requestHandlerFactory; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Factory<T, Dep extends object> = ({...dependencies}: Dep) => T; | |
export type Scope = "global" | "request" | "transient"; | |
export const scopes = { | |
GLOBAL: "global" as Scope, | |
REQUEST: "request" as Scope, | |
TRANSIENT: "transient" as Scope | |
} | |
export type Builder<T> = { | |
to(instance: T): Builder<T> | |
factory<Dep extends object>(factory: Factory<T, Dep>): Builder<T> | |
scope(scope: Scope): Builder<T> | |
} | |
const bind: <T>() => Builder<T> = <T>() => ({ | |
to(): Builder<T> { | |
throw new Error("not implemented"); | |
}, | |
factory(): Builder<T> { | |
throw new Error("not implemented"); | |
}, | |
scope(): Builder<T> { | |
throw new Error("not implemented"); | |
} | |
}) | |
const get: <T>() => T = <T>() => { | |
throw new Error("not implemented"); | |
} | |
export const container = { | |
get, | |
bind | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { UserRepositoryInterface } from "./UserRepoistoryInterface"; | |
export const listUsersFactory = ({userRepository}: {userRepository: UserRepositoryInterface}) => | |
() => userRepository.list() | |
export type ListUsers = ReturnType<typeof listUsersFactory>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User, UserRepositoryInterface } from "./UserRepoistoryInterface"; | |
export const userRepositoryFactory = ({users}: {users: User[]}) => ( | |
{ | |
list: () => users | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type User = { | |
id: number | |
name: string | |
} | |
export interface UserRepositoryInterface { | |
list(): User[] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment