iris
iris copied to clipboard
dependency question
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
Yes @xieyj-apple it's the same instance because it doesn't contain any dynamic dependencies, not new every time.
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,
}
}