-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from fgm/8-convert-container-to-interface
Issue #8: convert Container to an interface.
- Loading branch information
Showing
7 changed files
with
205 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/fgm/izidic/examples/di" | ||
) | ||
|
||
func main() { | ||
dic := di.Resolve(os.Stdout, os.Args[0], os.Args[1:]) | ||
app := dic.MustService("app").(di.App) | ||
log.Printf("app: %#v\n", app) | ||
if err := app(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package di | ||
|
||
import "log" | ||
|
||
// App represents whatever an actual application as a function would be. | ||
type App func() error | ||
|
||
func makeAppFeature(name string, logger *log.Logger) App { | ||
return func() error { | ||
logger.Println(name) | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package di | ||
|
||
import ( | ||
"io" | ||
"log" | ||
|
||
"github.com/fgm/izidic" | ||
) | ||
|
||
// Container is an application-specific wrapper for a basic izidic container, | ||
// adding typed accessors for simpler use by application code, obviating the need | ||
// for type assertions. | ||
type Container struct { | ||
izidic.Container | ||
} | ||
|
||
// Logger is a typed service accessor. | ||
func (c *Container) Logger() *log.Logger { | ||
return c.MustService("logger").(*log.Logger) | ||
} | ||
|
||
// Name is a typed parameter accessor. | ||
func (c *Container) Name() string { | ||
return c.MustParam("name").(string) | ||
} | ||
|
||
// Resolve is the location where the parameters and services in the container | ||
// | ||
// are assembled and the container readied for use. | ||
func Resolve(w io.Writer, name string, args []string) izidic.Container { | ||
dic := izidic.New() | ||
dic.Store("name", name) | ||
dic.Store("writer", w) | ||
dic.Register("app", appService) | ||
dic.Register("logger", loggerService) | ||
dic.Freeze() | ||
return dic | ||
} | ||
|
||
func appService(dic izidic.Container) (any, error) { | ||
wdic := Container{dic} // wrapped Container with typed accessors | ||
logger := wdic.Logger() // typed service instance: *log.Logger | ||
name := wdic.Name() // typed parameter value: string | ||
appFeature := makeAppFeature(name, logger) | ||
return appFeature, nil | ||
} | ||
|
||
// loggerService is an izidic.Service also containing a one-time initialization action. | ||
// | ||
// Keep in mind that the initialization will only be performed once the service has | ||
// actually been instantiated. | ||
func loggerService(dic izidic.Container) (any, error) { | ||
w := dic.MustParam("writer").(io.Writer) | ||
log.SetOutput(w) // Support dependency code not taking an injected logger. | ||
logger := log.New(w, "", log.LstdFlags) | ||
return logger, nil | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.