Coder Social home page Coder Social logo

Comments (4)

Monnoroch avatar Monnoroch commented on June 14, 2024 2

Option number one:

AutoInjectModule(new(MyServer),
  // This argument is a mapping of field names to annotations.
  // Omitted fields imply inject.AutoInject annotation.
  struct {
    AuthClient Auth
    FrontendClient Frontend
  }{}
) 

This method implies that the user defines how to provide a type, not the type's author. This method makes it possible and natural to separate the type and the module for providing it into two separate libraries and encourages to have multiple modules for providing the same type for different purposes.

This method requires the user to know the internals of the type and the user's code will break when they change.

from go-inject.

Monnoroch avatar Monnoroch commented on June 14, 2024 1

Option number two (thanks @GeniyX):

type MyServer struct {
  AuthClient grpc.Client
  // ${Field}Annotation field declares the annotation for the ${Field} field.
  // Omitting this field implies inject.AutoInject annotation.
  AuthClientAnnotation Auth
  FrontendClient grpc.Client
  FrontendClientAnnotation Frontend
}

Optionally, allow annotation fields to be private, which is possible.

This method implies that there is only one canonical way to provide a type, that the type's author pre-defined for everyone. It's still possible to write a custom provider to override this though.

This method is consistent with annotation providers, that have to be named ProvideAnnotaionXxx as opposed to ProvideXxx in normal providers.

from go-inject.

Monnoroch avatar Monnoroch commented on June 14, 2024 1

Option number three:

/// Implement this interface to make the type auto-injectable.
type AutoInjectable interface {
   /// Returns a mapping of field names to annotations.
   /// Omitted fields imply inject.AutoInject annotation.
   /// Not implementing this method implies all fields having inject.AutoInject annotation.
   ProvideAutoInjectAnnotations() interface{}
}

func (self MyServer) ProvideAutoInjectAnnotations() interface{} {
  return struct {
    AuthClient Auth
    FrontendClient Frontend
  }{}
}

This method implies that there is only one canonical way to provide a type, that the type's author pre-defined for everyone. It's still possible to write a custom provider to override this though.

This method can be used together with the first method: the interface implementation will define the default way to provide the type and the user will be able to override specific fields.

from go-inject.

Monnoroch avatar Monnoroch commented on June 14, 2024

It was decided to implement (1) as the most generic approach and (3) for non-general-purpose libraries.

from go-inject.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.