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

[BUG] When using Dependency Injection , Iris call new method everytime to create a new struct #1564

Closed
sheunglaili opened this issue Jul 21, 2020 · 6 comments

Comments

@sheunglaili
Copy link

Describe the bug
When passing in New function into

app.Register(
    environment.New
    // ...dependencies
)

When requesting endpoints , the New function will run again. Shouldn't the result of the new constructor be stored in some kind of bean factory instead ?

To Reproduce
Steps to reproduce the behavior:

  1. Create a Iris project.
  2. Setup mvc.
  3. in the Dependencies new function fmt.Println some sentences
  4. register dependencies in the mvc
  5. register the controller and use the dependencies in that controller
  6. start the application and request the endpoint
  7. should see println in New function print out everytime when requesting endpoint.

Expected behavior
New function should be called once only , but not everytime when there is request.

Screenshots
Screenshot 2020-07-21 at 11 00 49 AM

the Json is the configuration I printed from environment struct new function.
the line beneath it is the log in controller method

Desktop (please complete the following information):

  • OS: MAC
@kataras
Copy link
Owner

kataras commented Jul 21, 2020

@sheunglaili In Iris we have two bindings mode, static and request-scope (dynamic) and there is a smart factory, that's why Iris' MVC is the fastest out there . If I don't see your // ...dependencies I can't tell you what type your dependency is. Please post a code snippet that I can run, this is missing the important part.

@sheunglaili
Copy link
Author

sheunglaili commented Jul 21, 2020

so for static , does that means we have to wire up our bean first ?

        logger := app.Logger()
        profileAPI := app.Party("/api")

	mvc.Configure(profileAPI, func(app *mvc.Application) {

		env := environment.New(logger)

		db := database.New(env, logger)

		app.Register(
			logger,
			env,
			db,
			service.NewProfileService,
		)

		app.Handle(new(controller.ProfileController))
	})

like that ?

so what if there is a lot dependencies ? so what to wire it up ourselves before register them ?

@sheunglaili
Copy link
Author

sheunglaili commented Jul 22, 2020

@kataras I have written some demo code in https://github.com/sheunglaili/iris-DI-issue

you can see when you request it the GET localhost:8080/api/ , the log in server will indicate the dependency is reinitialized again.

@kataras
Copy link
Owner

kataras commented Jul 24, 2020

That was an issue because a dependency depend on other dependencies and you are right, if all accepted dependencies are static so the user of those should be static too. Fixed on master branch. Thanks a lot @sheunglaili!

kataras added a commit that referenced this issue Jul 26, 2020
Former-commit-id: d17b205a56f95f476db8b846c88b4cb6df8b5239
@sheunglaili
Copy link
Author

thanks a lot @kataras

@ezewu
Copy link

ezewu commented Aug 4, 2021

Is this problem solved?

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

No branches or pull requests

3 participants