English 中文(简体)
在 Nest,为模块化工作提供抽象的班子
原标题:In Nestjs, Providing abstract class to module doesn t work properly
const userRepositoryProvider = {
  provide: UserRepository, // abstract class
  useClass: ConcreteUserRepository, // concrete class
};

在模块A中,Im提供和出口一个以上这样的供应商。 (所有计划都可以注射)

在另一个单元B中,Im 进口模块A.和模块B的服务中,Im 注射从模块A进口的供应商,例如:

private readonly userRepo: UserRepository

在我建造和执行 app时, Nest没有解决依赖问题,告诉用户保存机构是“吗?


奇怪的是,当我确定供应商时,可以证明:

const userRepositoryProvider = {
  provide: "UserRepository", // abstract class
  useClass: ConcreteUserRepository, // concrete class
};

并注入提供者,如:

@Inject("UserRepository") private readonly userRepo: UserRepository

它与我毫无例外地打算完美地开展工作。

因此,为什么这样做? 这难道可能是一种ug吗?


最后,我提供这两个抽象和具体的课程。 但是,我认为,这确实很重要,因为扼杀能够适当提供象征性的工作,而抽象的班级则提供象征性的工作。

@Injectable()
export abstract class UserRepository {
  getUser(id: bigint): User {
    return User.create(id);
  }
}

@Injectable()
export class ConcreteUserRepository extends UserRepository {}
const userRepositoryProvider = {
  provide: UserRepository, // abstract class
  useClass: ConcreteUserRepository, // concrete class
};

在模块A中,Im提供和出口一个以上这样的供应商。 (所有计划都可以注射)

在另一个单元B中,Im 进口模块A.和模块B的服务中,Im 注射从模块A进口的供应商,例如:

private readonly userRepo: UserRepository

在我建造和执行 app时, Nest没有解决依赖问题,告诉用户保存机构是“吗?


奇怪的是,当我确定供应商时,可以证明:

const userRepositoryProvider = {
  provide: "UserRepository", // abstract class
  useClass: ConcreteUserRepository, // concrete class
};

并注入提供者,如:

@Inject("UserRepository") private readonly userRepo: UserRepository

它与我毫无例外地打算完美地开展工作。

因此,为什么这样做? 这难道可能是一种ug吗?


最后,我提供这两个抽象和具体的课程。 但是,我认为,这确实很重要,因为扼杀能够适当提供象征性的工作,而抽象的班级则提供象征性的工作。

@Injectable()
export abstract class UserRepository {
  getUser(id: bigint): User {
    return User.create(id);
  }
}

@Injectable()
export class ConcreteUserRepository extends UserRepository {}

  • properly working case enter image description here

  • improperly working case enter image description here

  • the error log enter image description here

最佳回答

尽管我尚未解决该问题,但我发现 Nest框架不是直接原因。

我的服务器应用在AWS Lambda环境中。

Maybe some problems happen during either transpiling, bundling, or maybe execution etc.

When I get the reason, I will update this.


I got the reason..! Actually I don t know the fundamental reason but I got the solution.

I changed all my import paths to absolute paths. and it works. I think the issue occurred during esbuild bundling time.

www.un.org/spanish/ecosoc 我不得不用人工方式向建筑商注入所有附属设施,并用@Inject()。

问题回答

Nest 在你定义提供者的情况下,对于向被点人注射,你必须使用“强硬字面”(正如你在问题中提到的那样) OR,但你可以确定一个注射机制工作:

export const UserRepositoryToken = Symbol( UserRepository );

现在,你可以建立这样的供应商,并在所希望的单元的供应商中登记:

const userRepositoryProvider: Provider = {
  provide: UserRepositoryToken, // injection token
  useClass: ConcreteUserRepository, // concrete class
};

并最后将其注入服务建设者:

@Inject(UserRepositoryToken) private readonly userRepo: UserRepository




相关问题
asp.net cookie from a resource project (+ a general info)

i d like to use a class to manage a certain cookie, but not directly on the page where i have access to all the httpcookie stuff like so HttpContext.Current.Request.Cookies["CookieName"].Value;. ...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

C# Class Definition

In XCode and objective c, if I want to use a UIGlassButton in interface builder, I put the following line in my header file: @class UIGlassButton; I am now using monotouch c#. Is there a similar ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签