Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created August 29, 2021 06:02
Show Gist options
  • Save percybolmer/45383dffcac443b24db9a6ffa0228581 to your computer and use it in GitHub Desktop.
Save percybolmer/45383dffcac443b24db9a6ffa0228581 to your computer and use it in GitHub Desktop.
// Package services holds all the services that connects repositories into a business flow
package services
import (
"github.com/percybolmer/ddd-go/domain/customer"
)
// OrderConfiguration is an alias for a function that will take in a pointer to an OrderService and modify it
type OrderConfiguration func(os *OrderService) error
// OrderService is a implementation of the OrderService
type OrderService struct {
customers customer.CustomerRepository
}
// NewOrderService takes a variable amount of OrderConfiguration functions and returns a new OrderService
// Each OrderConfiguration will be called in the order they are passed in
func NewOrderService(cfgs ...OrderConfiguration) (*OrderService, error) {
// Create the orderservice
os := &OrderService{}
// Apply all Configurations passed in
for _, cfg := range cfgs {
// Pass the service into the configuration function
err := cfg(os)
if err != nil {
return nil, err
}
}
return os, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment