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

Injecting dependencies into an entity or repository. #3864

Closed
CesarHuelamo opened this issue Mar 21, 2019 · 3 comments
Closed

Injecting dependencies into an entity or repository. #3864

CesarHuelamo opened this issue Mar 21, 2019 · 3 comments
Labels

Comments

@CesarHuelamo
Copy link

Issue type:

[X] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[X] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[X] 0.2.11 (or put your version here)

Steps to reproduce or a small repository showing the problem:
Is there a way to inject clases from outside of the typeorm environment, like a class service. This is what i'm trying to do, but it doesn't work in any way:

@Entity()
export class Users {
  constructor(private readonly usersService: UsersService){}
  @PrimaryGeneratedColumn()
  @ApiResponseModelProperty()
  id: number;
}

UsersService being a declared class with dependencies injected into it.

@vlapo
Copy link
Contributor

vlapo commented Mar 27, 2019

We do not know your environment :) so it is hard to help you with this problem. But I do not think so entity is right place to work with services. You should move this functionality to service or another place with business logic.

Note: In current version of typeorm constructor parameters have to be optional (this is subject of change in next version).

@ksuhiyp
Copy link

ksuhiyp commented May 27, 2020

@vlapo I can give a use case of this, suppose we have an AWS service and we need to trigger deleteObject method from that service by using @BeforeRemove(), How could you do that?

@neuro-sys
Copy link

neuro-sys commented Sep 7, 2020

@ksuhiyp

@vlapo I can give a use case of this, suppose we have an AWS service and we need to trigger deleteObject method from that service by using @BeforeRemove(), How could you do that?

How are you going to rollback if the database operation fails?

This is one of the reasons why (also in domain driven design patterns) entities don't do I/O themselves. And in the above specific case (AWS deleteObject), unless you have a global transaction manager, your best bet is to dispatch a persistent message which will be asynchronously processed outside your database transaction and be "eventually consistent".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants