# Passing specific instances to the resolver

In 

Parsley's ResolveWithOptions method allows you to pass specific instances into the resolver, providing certain dependencies dynamically at resolution time. This method is handy when overriding registered dependencies or injecting unregistered instances into your objects.

In the following example, the newClient constructor function registers a client service with a transient lifetime. During the resolution, an existing transport instance is passed to the resolver using WithInstance, ensuring that this specific instance is used for the client's transport dependency.

package main

import (
	"context"
	"fmt"
	"reflect"

	"github.com/matzefriedrich/parsley/pkg/registration"
	"github.com/matzefriedrich/parsley/pkg/resolving"
	"github.com/matzefriedrich/parsley/pkg/types"
)

func main() {

	registry := registration.NewServiceRegistry()
	registry.Register(newClient, types.LifetimeTransient)

	resolver := resolving.NewResolver(registry)
	ctx := resolving.NewScopedContext(context.Background())

	resolveServiceType := types.MakeServiceType[*client]()
	transportInstance := resolving.WithInstance[*transport](&transport{})
	instance, _ := resolver.ResolveWithOptions(ctx, resolveServiceType, transportInstance)

	fmt.Println(reflect.ValueOf(instance).Pointer())
}

type transport struct {
}

type client struct {
	transport *transport
}

func newClient(transport *transport) *client {
	return &client{
		transport: transport,
	}
}

Parsley's ability to inject specific instances dynamically improves flexibility in managing dependencies and provides greater control over the instantiation process. This feature is particularly beneficial for the following use cases:

  • Runtime configuration: Dynamically configure services based on runtime conditions by passing specific instances.

  • Dependency injection in testing: Use mock objects or specific instances during tests to simulate various scenarios without altering the registration setup.

  • Third-party integrations: Integrate with third-party services or libraries that require preconfigured instances, ensuring they are used as dependencies when needed.

By using ResolveWithOptions, you can push unregistered instances into the resolver, ensuring the correct instances are used when resolving dependencies.