iris icon indicating copy to clipboard operation
iris copied to clipboard

dependency question

Open apple-1024 opened this issue 2 years ago • 2 comments

when i use RegisterDependency to implements singleton bean

func main(){
  IrisApp = iris.New()
  IrisApp.RegisterDependency(NewService)
party:=IrisApp.Party("/test")
irisMvc := mvc.New(party)
irisMvc.handle(new(controller))
}

func NewService(){
   return &Test{name:"kangkang"}
}
type Test struct{
   name string
}

type controller struct{
  test Test
}

func (c *controller)Get(){
  fmt.Println(&c.test)
}

myquestion is that does the c.test is singleton? not new every time

apple-1024 avatar Jun 16 '23 01:06 apple-1024

Yes @xieyj-apple it's the same instance because it doesn't contain any dynamic dependencies, not new every time.

kataras avatar Jun 22 '23 02:06 kataras

hi @kataras

How do I make my UserService to singleton? I have a lot of Controllers or other services using UserService. I see that, for each controller or service that depends on UserService, a new instance of UserService will be created.

User Service

func NewService(
	transactor dbx.Transactor,
	repo Repository,
	customerUserRepo CustomerUserRepository,
	shipperUserRepo ShipperUserRepository) domain.UserService {
	return &service{transactor: transactor,
		repo:             repo,
		customerUserRepo: customerUserRepo,
		shipperUserRepo:  shipperUserRepo,
	}
}

User Controller

type UserController struct {
	Service domain.UserService
}

CusomterService

func NewService(transactor dbx.Transactor,
	repo Repository,
	resellerCustomerRepo ResellerCustomerRepository,
	userService domain.UserService) domain.CustomerService {
	return &service{
		transactor:           transactor,
		customerRepo:         repo,
		resellerCustomerRepo: resellerCustomerRepo,
		userService:          userService,
	}
}

vominhtrius avatar Oct 07 '23 07:10 vominhtrius