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

mvc.Register and App.RegisterDependency #2158

Closed
xieyj-apple opened this issue Jun 16, 2023 · 8 comments
Closed

mvc.Register and App.RegisterDependency #2158

xieyj-apple opened this issue Jun 16, 2023 · 8 comments

Comments

@xieyj-apple
Copy link

what diference between mvc.Register and App.RegisterDependency

App = iris.New()
App.RegisterDependency(xx)

_party := App .Party("/test")
mvc := mvc.New(_party)
mvc.Register(xxx)
@xieyj-apple
Copy link
Author

controllerA -> serviceB -> serviceC

when i use
App.RegisterDependency(new(serviceB))
App.RegisterDependency(new(serviceC))

result :B.C is empty

@kataras
Copy link
Owner

kataras commented Jun 22, 2023

The only difference is that app.RegisterDependency registers one or more dependencies to all of the mvc.New apps coming after that, so you don't need to call mvc.Register on each of your controller group. I don't understand the B.C is empty thing, please post a re-producable go code snippet for these cases so we can give you a more specific answer to your question. Thanks @xieyj-apple

@xieyj-apple
Copy link
Author


type A struct {
	b B
}

type B struct {
	c C
}

type C struct {
	Name string
}

func newC() *C {
	return &C{Name: "4234"}
}

type TestController struct {
	a A
}

func (h *TestController) Get(ctx iris.Context) {
	fmt.Println("h say hello...", h.a.b.c.Name)
}

func main() {
	IrisApp := iris.New()
	IrisApp.RegisterDependency(newC)
	IrisApp.RegisterDependency(new(B))
	IrisApp.RegisterDependency(new(A))

	_party := IrisApp.Party("test")
	irisMvc := mvc.New(_party)
	irisMvc.Handle(new(TestController))
	if err := IrisApp.Listen("0.0.0.0:9988"); err != nil {
		_ = IrisApp.Shutdown(context.Background())
	}
}

when i visit http://localhost:9988/test it print : "h say hello..." not "h say hello...4234" why?

@kataras
Copy link
Owner

kataras commented Jul 1, 2023

@xieyj-apple Because TestController.a is un-exported/private field and reflect has no access over it, so it can't set the newC->new(A) to the TestController.a dependency.

@xieyj-apple
Copy link
Author

like this? it not work


type A struct {
	B B
}

type B struct {
	C C
}

type C struct {
	Name string
}

func newC() *C {
	return &C{Name: "4234"}
}

type TestController struct {
	A A
}

func (h *TestController) Get(ctx iris.Context) {
	fmt.Println("h say hello...", h.A.B.C.Name)
}

func main() {
	IrisApp := iris.New()
	IrisApp.RegisterDependency(newC)
	IrisApp.RegisterDependency(new(B))
	IrisApp.RegisterDependency(new(A))

	_party := IrisApp.Party("test")
	irisMvc := mvc.New(_party)
	irisMvc.Handle(new(TestController))
	if err := IrisApp.Listen("0.0.0.0:9988"); err != nil {
		_ = IrisApp.Shutdown(context.Background())
	}
}

@kataras kataras closed this as completed in 6add1ba Jul 7, 2023
@kataras
Copy link
Owner

kataras commented Jul 7, 2023

Fixed @xieyj-apple :)

@xieyj-apple
Copy link
Author

had new tag or version to fixed it?

kataras added a commit that referenced this issue Jul 17, 2023
@kataras
Copy link
Owner

kataras commented Jul 17, 2023

Hello @xieyj-apple, I wasn't sure about the stability of this change that's why I didn't publish a new release, until today. Many apps would be broken if we change the default behavior like that. That's why I've added the app.ConfigureContainer().EnableStructDependents() and mvc.Application.EnableStructDependents() methods, you have to call this first in order for struct fields to be automatically binded based on previous dependencies that are structs. Here's a complete and working example:

package main

import (
	"fmt"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

type A struct {
	B *B
}

type B struct {
	C *C
}

type C struct {
	Name string
}

func newC() *C {
	return &C{Name: "4234"}
}

type TestController struct {
	A *A
}

func (h *TestController) Get(ctx iris.Context) {
	fmt.Println("h say hello...", h.A.B.C.Name)
}

func main() {
	app := iris.New()
	app.Logger().SetLevel("debug")

	app.ConfigureContainer().EnableStructDependents()
	app.RegisterDependency(newC())
	app.RegisterDependency(new(B))
	app.RegisterDependency(new(A))

	testAPI := app.Party("/test")
	m := mvc.New(testAPI)
	// OR
	// m.EnableStructDependents()
	// m.Register(newC())
	// m.Register(new(B))
	// m.Register(new(A))
	m.Handle(new(TestController))
	app.Listen(":8080")
}

The latest release tag is v12.2.1

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

2 participants