Comments (12)
oh I understand. I needed it to be generic to comply with the dependency inversion principle, and although I extended PumpIt Class to be able to use it, it seemed like a good idea to me for it to be in the original library, since it seems to me like a very good library for cases where there is incompatibility with the decorators
from pumpit.
I'm not sure where the problem is, can you post the problem first?
Is this a types
problem?
from pumpit.
Yes, in bindClass' Generic, i will show you an example
Type 'UserService' does not satisfy the constraint 'ClassValue'.
this is UserService
import type { Maybe } from "~/core/libs/maybe";
import type { UsersEntity } from "./users.entity";
import type { UserEntity } from "./user.entity";
export interface UserService {
findOne(id: string): Promise<Maybe<UserEntity>>;
findAll(): Promise<UsersEntity>;
}
this is UserBaseService
import type { Maybe } from "~/core/libs/maybe";
import type { UserEntity } from "../models/user.entity";
import type { UserService } from "../models/user.service";
import type { UsersEntity } from "../models/users.entity";
import type { UserRepository } from "../models/user.repository";
import { USER_TOKEN } from "../di/tokens";
import type { Inject } from "~/core/models/inject";
export class UserBaseService implements UserService {
static inject: Inject = [USER_TOKEN.REPOSITORY];
constructor(private readonly userRepository: UserRepository) {}
findOne = async (id: string): Promise<Maybe<UserEntity>> => {
return await this.userRepository.findOne(id);
};
findAll = async (): Promise<UsersEntity> => {
return await this.userRepository.findAll();
};
}
from pumpit.
You don't need generics when binding.
You can just do:
container.bindClass(TOKEN, Class)
And when you resolve, you need generics:
container.resolve<WhatEverType>(TOKEN)
and the result from the resolve will be WhatEverType
from pumpit.
You are right, for a common case, I do not require interfaces in bindClass, but I am using a Dependency inversion principle, and this allows me to switch between classes without affecting the original behavior, which allows me to scale my project without problems. I have got this idea from another project using inversify.js https://github.com/inversify/InversifyJS#step-1-declare-your-interfaces-and-types, and it seemed like a good idea to me being able to implement it with this library
from pumpit.
There is no difference in what I gave you in the previous example and the example you posted from inversify.
from pumpit.
oh excuse me, here is the example https://github.com/inversify/InversifyJS#step-3-create-and-configure-a-container, it puts interfaces to the bind generics to control which classes you pass through the arguments of the bind method. If there is something you don´t quite understand, feel free to tell me ´cause my english isn´t pretty good
from pumpit.
Can you write me an example with pumpit
that doesn't work?
from pumpit.
I show a simple example
interface Vehicle {
run(): void
}
class Car implements Vehicle {
run(){
console.log("the car is running")
}
}
class Motorcycle implements Vehicle {
run(){
console.log("the motorcycle is running")
}
}
const container = new PumpIt()
//case 1
container.bindClass<Vehicle>("VEHICLE", Car) // error: Type 'Vehicle' does not satisfy the constraint 'ClassValue'.
//case 2: I change Car to Motorcycle
container.bindClass<Vehicle>("VEHICLE", Motorcycle) // error: Type 'Vehicle' does not satisfy the constraint 'ClassValue'.
from pumpit.
bindClass
method is not generic.
https://github.com/ivandotv/pumpit#registering-classes
from pumpit.
If you want, I can fork the project and add the generic to bindClass to make it more adaptable
from pumpit.
There is no reason for bindClass
to be generic.
from pumpit.
Related Issues (9)
- Dependency Dashboard
- Pretty print circular dependency tree
- Implement decorators
- Implement validation
- Implement post construct method for classes HOT 1
- Implement validate dependencies method
- Support --moduleResolution node16 in typescript HOT 3
- Hi does this library support aliasing like in Nest Js depedency injection HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pumpit.