You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/InjectorObject.md
+26-4Lines changed: 26 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,11 +18,19 @@ We can inject into a C# object like:
18
18
```
19
19
Note: Injecting into MonoBehaviours is perfectly valid however you must Inject as a Field only. Attempting to Inject a Property into a MonoBehaviour will compile but throw an exception at runtime.
20
20
21
+
### Templated Injection
22
+
[Inject] also supports templated values, which is a value or reference type associated to a string instead of a System.Type. You can define a field or property to be injected with a template like so:
23
+
```csharp
24
+
[Inject("IsFoo")] publicboolHasFoo {get; set;}
25
+
```
26
+
21
27
## Injection Types
22
-
Injection of two different variable types is supported: Singleton and Transient.
28
+
Injection of three different variable types is supported: Singleton, Transient, and Template.
23
29
24
30
The difference between a singleton and a transient is when you define a singleton you give it an instance of an object but when you define a transient you give it a type of an object. The injector will then assign either the instance of the singleton or create a brand new instance of that transient type when it injects, depending on what was requested.
25
31
32
+
A Template is an instance of an object bound to a string, not any type, interface, or concrete. Template injection can be useful if you need to inject default values or objects that may change based on exterior circumstances like a configuration file or application state.
33
+
26
34
Transient types can define default constructors through the Factory just like any other object which will be run before injection takes place.
To use a template type we use the Injector to map a string to an instance or value. Any value or reference type can be bound to a template.
64
+
65
+
##### Give the Injector a template:
66
+
```csharp
67
+
Injector.BindTemplate("IsFoo", true);
68
+
```
69
+
70
+
Note I'm using bool here as a simple example but you can use this to bind any instance. Attempting to assign into to something that can't be implicitly casted from the target will cause an assertion at runtime.
71
+
54
72
##### Define an object that injects a FooSystem:
55
73
```csharp
56
74
// define our object
57
75
classObjectA : IInitializable {
58
76
// precede injectable types with the [Inject] attribute
59
77
[Inject] publicIFooSystemAFooSystem {get; set;} // Field or Property injection is valid here
78
+
[Inject("IsFoo")] publicboolHasFoo {get; set;}
60
79
voidInitialize() {
61
-
AFooSystem.SomeMethod(); // AFooSystem is resolved here already assuming ObjectA was created by Factory
80
+
if(HasFoo) // this is true because of templated injection
81
+
AFooSystem.SomeMethod(); // AFooSystem is resolved here already assuming ObjectA was created by Factory
62
82
}
63
83
}
64
84
```
@@ -70,7 +90,9 @@ var myA = Factory.Create<ObjectA>(ConstructObjectA);
70
90
objectConstructObjectA(objectobj)
71
91
{
72
92
varobjA=objasObjectA;
73
-
objA.AFooSystem.SomeMethod(); // AFooSystem is resolved here already and we can use it if we need to
93
+
if(objA.HasFoo) // This is true because of templated injection
94
+
objA.AFooSystem.SomeMethod(); // AFooSystem is resolved here already and we can use it if we need to
Copy file name to clipboardExpand all lines: Docs/SceneObjectDataObject.md
+38-2Lines changed: 38 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# SceneObjectData
2
2
3
-
The SceneObjectData is a container for objects created at runtime. The container is what provides the invocation of the framework [Interfaces](Interfaces.md) available so any object you want to have the interface methods automatically invoked on needs to be inside of a SceneObjectData container.
3
+
The SceneObjectData is a container for objects created at runtime. The container is what provides the invocation of the framework [Interfaces](Interfaces.md) available so any object you want to have the interface methods automatically invoked on needs to be inside of an initialized, updating, and destroyed SceneObjectData container. This is done behind the scenes for the `SceneView` instance of SceneObjectData but it's important to note in case you want to reuse this object elsewhere.
4
4
5
5
## `InitializeSceneObjects()`
6
6
@@ -18,13 +18,14 @@ Calling `Update(deltaTime)` on the SceneObjectData instance will invoke the `Upd
18
18
19
19
Calling `Destroy()` on the SceneObjectData will invoke the `Destroy()` method on all contained objects implementing the `IDestroyable` interface. It will also clear all the containers and reset the initialization state of the object to false allowing you to re-use it with new object data if you want to.
20
20
21
-
## SceneDataObject contained object types
21
+
## Example code
22
22
23
23
For the following examples assume we have access to a SceneObjectData defined like:
24
24
```csharp
25
25
publicSceneObjectDataSceneObjects;
26
26
```
27
27
28
+
28
29
### Singleton
29
30
30
31
A SceneObjects Singleton type is a 1-to-1 mapping of a System.Type to an instance of that type.
@@ -59,6 +60,8 @@ for(var i = 0; i < 5; i++) {
59
60
```
60
61
61
62
#### Resolve a transient
63
+
64
+
##### By Name:
62
65
```csharp
63
66
// resolve instances of Foo named Foo0 through Foo4
64
67
List<Foo>fooList=newList<Foo>();
@@ -67,3 +70,36 @@ for(var i = 0; i < 5; i++) {
67
70
}
68
71
```
69
72
73
+
##### By Type:
74
+
```csharp
75
+
// return every transient object of type Foo found in the data and map it's name to it's instance
Upon removal of the object from the data `Destroy()` will be invoked on the object before it is returned.
82
+
83
+
Depending on the type the technique to remove the data varies:
84
+
85
+
#### Removing Singletons
86
+
87
+
A singleton can be removed or replaced by simply binding over it with another instance of the same object type or by binding it to `null`. Obviously no object will be returned here so be sure to cache the reference or destruct it properly before binding over it. For example:
88
+
89
+
```csharp
90
+
varoldFoo=SceneObjects.ResolveSingleton<IFoo>();
91
+
FoosFriends.SayGoodbye(oldFoo);
92
+
varnewFoo=SceneObjects.PushSingleton<IFoo>(newDifferentFooImpl()); // oldFoo.Destroy() method was invoked now
93
+
FoosFriends.SayHello(newFoo);
94
+
```
95
+
96
+
#### Removing Transients
97
+
98
+
A transient can be removed by calling `PopTransient<T>(string ...)` on the SceneObjectData. For example:
99
+
100
+
```csharp
101
+
// pop an object of type People named "Jeff" out of the data:
102
+
varjeff=SceneObjects.PopTransient<People>("Jeff"); // jeff.Destroy() was invoked now
0 commit comments