Giter Site home page Giter Site logo

Comments (12)

David200197 avatar David200197 commented on May 23, 2024 1

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.

ivandotv avatar ivandotv commented on May 23, 2024

I'm not sure where the problem is, can you post the problem first?
Is this a types problem?

from pumpit.

David200197 avatar David200197 commented on May 23, 2024

Yes, in bindClass' Generic, i will show you an example

image
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.

ivandotv avatar ivandotv commented on May 23, 2024

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.

David200197 avatar David200197 commented on May 23, 2024

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.

ivandotv avatar ivandotv commented on May 23, 2024

There is no difference in what I gave you in the previous example and the example you posted from inversify.

from pumpit.

David200197 avatar David200197 commented on May 23, 2024

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.

ivandotv avatar ivandotv commented on May 23, 2024

Can you write me an example with pumpit that doesn't work?

from pumpit.

David200197 avatar David200197 commented on May 23, 2024

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.

ivandotv avatar ivandotv commented on May 23, 2024

bindClass method is not generic.
https://github.com/ivandotv/pumpit#registering-classes

from pumpit.

David200197 avatar David200197 commented on May 23, 2024

If you want, I can fork the project and add the generic to bindClass to make it more adaptable

from pumpit.

ivandotv avatar ivandotv commented on May 23, 2024

There is no reason for bindClass to be generic.

from pumpit.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.