# Register Instances

In Parsely, the RegisterInstance method registers a preexisting service instance, particularly useful for objects that cannot be created automatically due to complex configuration, data dependencies, or third-party APIs. Once registered, these instances behave as singletons, meaning the same instance is reused throughout the application. This ensures the registered object is consistently available as a dependency for other services, maintaining consistency and state across your application.

Let's go through the process of registering a service instance manually and using it within your application.

Assume you have an interface DataService and an implementation LocalDataService.

package internal

type DataService interface {
	FetchData() string
}

type localDataService struct{}

func (s *localDataService) FetchData() string {
	return "Data from local database"
}

func NewLocalDataService() DataService {
	return &localDataService{}
}

Manually create an instance of the service and register it using RegisterInstance. This method is ideal for instances that require specific configuration or are provided by an external factory. To resolve and use the registered service instance, simply request it from the resolver:

registry := registration.NewServiceRegistry()
registration.RegisterInstance[internal.DataService](registry, internal.NewLocalDataService())

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

actual, _ := resolving.ResolveRequiredService[internal.DataService](resolver, scope)
data := actual.FetchData()
fmt.Println(data)

By registering service instances manually, you can integrate complex, pre-configured, or third-party-provided objects into your application, ensuring they are available as dependencies for other services managed by Parsely. This method is handy when dealing with objects that cannot be easily instantiated within the standard service registration flow.