Skip to content

Commit

Permalink
Add container name.
Browse files Browse the repository at this point in the history
Add build state with hierarchy.
Fix readme formatting.
  • Loading branch information
AlekNS committed May 21, 2019
1 parent 8daca57 commit a253912
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TODO
#### Values

```
container := inversify.Container()
container := inversify.NewContainer("name of container")
container.Bind(1).To("Hello")
container.Build()
Expand Down Expand Up @@ -77,7 +77,7 @@ TODO
#### Named dependencies

```
container := inversify.Container()
container := inversify.NewContainer("name of container")
container.Bind(1).To("empty")
container.Bind(1, "dev").To("Hello")
Expand All @@ -93,6 +93,7 @@ TODO

```
mergedContainer := container1.Merge(container2)
mergedContainer.Build()
```

#### Hierarchy
Expand All @@ -105,8 +106,6 @@ TODO
subContainer2 := Container()
...
baseContainer.Build()
subContainer1.SetParent(baseContainer)
subContainer1.Build()
Expand Down Expand Up @@ -144,14 +143,14 @@ TODO

```
authModule := NewModule("auth").
Register(func(c ContainerBinder) error {
// c.Bind()
return nil
}).
UnRegister(func(c ContainerBinder) error {
// c.Unbind()
return nil
})
Register(func(c ContainerBinder) error {
c.Bind()
return nil
}).
UnRegister(func(c ContainerBinder) error {
// c.Unbind()
return nil
})
container.Load(authModule)
container.Load(otherModule)
Expand Down
31 changes: 30 additions & 1 deletion auto_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type AutowireStructTestSuite struct {
}

func (t *AutowireStructTestSuite) TestBasic() {
c := NewContainer()
c := NewContainer("base")
c.Bind("values1").To(map[string]interface{}{
"value1": "1",
"value2": "2",
Expand Down Expand Up @@ -58,3 +58,32 @@ func (t *AutowireStructTestSuite) TestBasic() {
func TestAutowireStructSuite(t *testing.T) {
suite.Run(t, new(AutowireStructTestSuite))
}

func BenchmarkContainerAutowireStructure(b *testing.B) {
c := NewContainer("base")
c.Bind("values1").To(map[string]interface{}{
"value1": "1",
"value2": "2",
})
c.Bind((*config)(nil)).To(&config{1})
c.Bind((*iTaskRepository)(nil)).ToFactory(func() (Any, error) {
return &iTaskRepositoryImpl{
val: 2,
}, nil
})
c.Bind(1, "another").To(1000)
c.Build()

s := autowireTestStruct{}

b.ReportAllocs()

b.StartTimer()
for i := 0; i < b.N; i++ {
s.Config = nil
s.Scheduler = nil
s.TaskRepository = nil
AutowireStruct(c, &s)
}
b.StopTimer()
}
33 changes: 26 additions & 7 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Container interface {
Build()

// Merge with another container
Merge(Container) Container
Merge(container Container, name string) Container
// SetParent supports for hierarchical DI systems
SetParent(Container)

Expand Down Expand Up @@ -63,7 +63,9 @@ func Named(dep Any, name string) Any {
}

type containerDefault struct {
parent *containerDefault
name string
parent *containerDefault
isBuilt bool

factories map[Any]map[string]*Binding
}
Expand Down Expand Up @@ -104,6 +106,7 @@ func (c *containerDefault) bindInternal(isRebinding bool, symbol Any, names ...s
}

bindings[name] = binding
c.isBuilt = false
return binding
}

Expand All @@ -118,6 +121,7 @@ func (c *containerDefault) Unbind(symbol Any, names ...string) Container {
}
}
// else panic!?
c.isBuilt = false
return c
}

Expand All @@ -134,10 +138,20 @@ func (c *containerDefault) findFactory(symbol Any, name string) (*Binding, bool)
}

func (c *containerDefault) Build() {
if c.parent != nil {
c.parent.Build()
}

if c.isBuilt {
return
}

err := resolveContainerDependencies(c)
if err != nil {
panic(err.Error())
}

c.isBuilt = true
}

func (c *containerDefault) Get(symbol Any, names ...string) (Any, error) {
Expand All @@ -163,8 +177,8 @@ func (c *containerDefault) hasFactory(symbol Any, names ...string) bool {
return ok
}

func (c *containerDefault) Merge(other Container) Container {
container := newDefaultContainer()
func (c *containerDefault) Merge(other Container, name string) Container {
container := newDefaultContainer(name)

otherImpl, ok := other.(*containerDefault)
if !ok {
Expand Down Expand Up @@ -203,13 +217,18 @@ func (c *containerDefault) UnLoad(module *Module) error {
return module.unRegisterCallback(newContainerBinderProxy(c))
}

func newDefaultContainer() *containerDefault {
func (c *containerDefault) String() string {
return c.name
}

func newDefaultContainer(name string) *containerDefault {
return &containerDefault{
name: name,
factories: make(map[Any]map[string]*Binding),
}
}

// NewContainer .
func NewContainer() Container {
return newDefaultContainer()
func NewContainer(name string) Container {
return newDefaultContainer(name)
}
28 changes: 12 additions & 16 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
)

func (t *ContainerTestSuite) TestBasic() {
c1 := NewContainer()
c1 := NewContainer("basic")
c1.Bind(testDep1).To(resolvedValue)
c1.Build()

Expand All @@ -58,7 +58,7 @@ func (t *ContainerTestSuite) TestBasic() {
}

func (t *ContainerTestSuite) TestNamedBasic() {
c1 := NewContainer()
c1 := NewContainer("withNames")
c1.Bind(testDep1).To(resolvedValue)
c1.Bind(testDep1, "other").To(resolvedValue + resolvedValue)
c1.Build()
Expand All @@ -78,7 +78,7 @@ func (t *ContainerTestSuite) TestNamedBasic() {
}

func (t *ContainerTestSuite) TestNamedCycle() {
c1 := NewContainer()
c1 := NewContainer("withNamesAndCycle")
c1.Bind(testDep1).ToFactory(func(i Any) (Any, error) {
return 1, nil
}, Named(testDep3, "named"))
Expand All @@ -95,7 +95,7 @@ func (t *ContainerTestSuite) TestNamedCycle() {
}

func (t *ContainerTestSuite) TestFallthroughError() {
c1 := NewContainer()
c1 := NewContainer("withError")
c1.Bind((*testInterface1)(nil)).ToFactory(func() (Any, error) {
return nil, errors.New("FactoryError")
})
Expand All @@ -111,7 +111,7 @@ func (t *ContainerTestSuite) TestFallthroughError() {
}

func (t *ContainerTestSuite) TestMerge() {
c1 := NewContainer()
c1 := NewContainer("src1")
c1.Bind(testDep1).To("val1")
c1.Bind((*testInterface1)(nil)).To(&testInterface1Impl{})
c1.Bind((*testInterface2)(nil)).To(&testInterface2Impl{})
Expand All @@ -125,13 +125,11 @@ func (t *ContainerTestSuite) TestMerge() {
t.Equal(1, t2.get1())
return "val3val4", nil
}, (*structure)(nil), (*testInterface1)(nil))
c1.Build()

c2 := NewContainer()
c2 := NewContainer("src2")
c2.Bind(testDep2).To("val2")
c2.Build()

c3 := c1.Merge(c2)
c3 := c1.Merge(c2, "dst")
c3.Build()

t.True(c3.IsBound(testDep1))
Expand All @@ -147,17 +145,15 @@ func (t *ContainerTestSuite) TestMerge() {
}

func (t *ContainerTestSuite) TestParent() {
c1 := NewContainer()
c1 := NewContainer("parent")

c1.Bind(testDep1).To("V1")
c1.Bind(testDep2).ToTypedFactory(func(i1 string, any Any) (string, error) {
t.Nil(any)
return fmt.Sprintf("V2(1:%s)", i1), nil
}, testDep1, Optional(Named(testOtherDep2, "test")))

c1.Build()

c2 := NewContainer()
c2 := NewContainer("child")
c2.Bind(testDep3).ToFactory(func(i1 Any, i2 Any, i3 Any) (Any, error) {
t.Nil(i3)
return fmt.Sprintf("V3(1:%s,2:%s,3:%v)", i1, i2, i3), nil
Expand Down Expand Up @@ -186,7 +182,7 @@ func TestContainerSuite(t *testing.T) {
}

func BenchmarkContainerGet(b *testing.B) {
c1 := NewContainer()
c1 := NewContainer("")
c1.Bind(testDep1).To("val1")
c1.Build()

Expand All @@ -200,14 +196,14 @@ func BenchmarkContainerGet(b *testing.B) {
}

func BenchmarkContainerGetHierarchy(b *testing.B) {
c1 := NewContainer()
c1 := NewContainer("")

c1.Bind(testDep1).To("V1")
c1.Bind(testDep2).To("V2")

c1.Build()

c2 := NewContainer()
c2 := NewContainer("")
c2.Bind(testDep3).ToFactory(func(i1 Any, i2 Any, i3 Any) (Any, error) {
return i1, nil
}, testDep1, testDep2, Optional(testOtherDep1))
Expand Down
2 changes: 1 addition & 1 deletion module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (t *ModuleTestSuite) TestBasic() {
mdl1 := getModuleTest1()
mdl2 := getModuleTest2()

c := NewContainer()
c := NewContainer("basic")
c.Bind(0).ToTypedFactory(func() (string, error) {
return "Modules, ", nil
})
Expand Down

0 comments on commit a253912

Please sign in to comment.