Skip to content

Remove usage of Service Provider #799

@alexandrosfilios

Description

@alexandrosfilios

This includes any instance where we access services using the view context.

The main reasons:

  • testable code
  • clear dependency management
  • unique IoC mechanism (i.e. DI)

This can be done in steps to avoid breaking changes as much as possible:

  1. Extend the DIG container to provide factories in a similar way as done today:
c.Provide(NewAcceptCashViewFactory, WithInitiators(&WithdrawView{}, &TransferView{}))
c.Provide(NewWithdrawFactory, WithFactoryId("withdrawal"))
  1. Move digutils.Register from the core SDKs to the end SDKs (integration-test SDKs).
  2. Ensure DI is used for remaining core components, e.g. viewManager
  3. Refactor views to support both modes. Example of refactoring
    Before
func NewMyView(...) *MyView {...}

type MyView struct {
 Wallet string
}

func (v *MyView) Call(ctx view.Context) (interface{}, error) {
 ...
 GetKVS(ctx).Get(...)


 GetIdentityProvider(ctx)...
}

After

func NewMyView(...) *myView {}

// Keeps exactly the same inputs as MyView did before for backward compatibility
type myView struct {
 Wallet string
}

func (v *myView) Call(ctx view.Context) (interface{}, error) {
 return ctx.RunView(&MyView{
  Wallet: v.Wallet,
  kvss: GetKVS(ctx),
  identityProvider: GetIdentityProvider(ctx),
 })
}

type MyView struct {
 Wallet string

 kvss *kvs.KVS
 identityProvider driver.IdentityProvider
}

// Is exactly the same method as before, but now the services are provided as fields and not fetched through the ctx
func (v *MyView) Call(ctx view.Context) (interface{}, error) {
 ...
}
  1. Remove RegisterFactory, RegisterResponder from the test topology and create SDK's for each node, using the new Provide method of the DIG container with the corresponding options explained above.
  2. Removed unused views (e.g. myView) and digutils.Register from the integration-test SDKs.
  3. Remove the service provider from the context and any other reference to it.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions