-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetting_started.html
More file actions
146 lines (119 loc) · 4.85 KB
/
getting_started.html
File metadata and controls
146 lines (119 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Divineinject.GitHub.io by DivineInject</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/github-dark.css">
<link rel="stylesheet" href="stylesheets/buttons.css">
<link rel="stylesheet" href="stylesheets/nav.css">
<script src="javascripts/scale.fix.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header class="without-description">
<h1>DivineInject.GitHub.io</h1>
<p></p>
<p class="view"><a href="https://github.com/DivineInject">View the Project on GitHub <small>DivineInject</small></a></p>
<ul>
<li class="single"><a href="https://github.com/DivineInject/DivineInject.github.io">View On <strong>GitHub</strong></a></li>
</ul>
</header>
<ul id="nav">
<li><a href="index.html" class="myButton">About</a></li>
<li><a href="why.html" class="myButton">Why</a></li>
<li><a href="getting_started.html" class="myButton">Getting Started</a></li>
<li><a href="scopes.html" class="myButton">Scopes</a></li>
<li><a href="rich_domain_objects.html" class="myButton">Rich Domain</a></li>
</ul>
<section>
<h1>
<a id="divine-inject" class="anchor" href="#divine-inject" aria-hidden="true"><span class="octicon octicon-link"></span></a>Getting Started</h1>
<p>
This page explains how to use DivineInject for dependency injection. It assumes you already know
<a href="http://stackoverflow.com/questions/130794/what-is-dependency-injection">what dependency injection is</a>.
Basic usage of DivineInject boils down to two topics:
<ul>
<li><a href="#binding">Configuring bindings</li>
<li><a href="#creating">Creating root objects</li>
</ul>
</p>
<!--
constructor selection
-->
<h2>
<a id="binding" class="anchor" href="#binding" aria-hidden="true"><span class="octicon octicon-link"></span></a>Configuring Bindings</h2>
<p>
When DivineInject creates a new instance of a class it calls a constructor, each of the constructor arguments is a <i>dependency</i> of
the class — something external to the class, for which an implementation must be provided. But how do we know which value to pass for each dependency?
This is controlled by the <i>bindings</i>.
</p>
<p>
<b>Your bindings must be configured near the start of your application — e.g. in the main method or global.asax.</b>
</p>
<p>
There are basically two ways to configure bindings with DivineInject.
</p>
<p>
1) bind an interface to a concrete type. DivineInject will pass the same (singleton) instance of the given
concrete type whenever it encounters a constructor argument of the interface type.</p>
<pre>
DivineInjector.Current
.Bind<IOrdersService>().To<OrdersService>();
</pre>
<p>
2) bind an interface to a specific instance. DivineInject will pass the given instance whenever it encounters
a constructor argument of the interface type.
</p>
<pre>
var myOrdersService = new OrdersService(...);
DivineInjector.Current
.Bind<IOrdersService>().ToInstance(myOrdersService);
</pre>
<h2>
<a id="creating" class="anchor" href="#creating" aria-hidden="true"><span class="octicon octicon-link"></span></a>Creating Root Objects</h2>
<p>
DivineInject allows you to create a tree of objects — each object has references to dependencies, which in turn reference their own
dependencies; forming a tree of objects. This tree is created starting with the <i>root object</i> — e.g. in a WPF
application the root would be the outermost ViewModel; in a WCF application it would be the service class.
</p>
<p>
The root object is created by calling DivineInject — any arguments the root object constructor requires are taken from the bindings.
This should be the <u>only time</u> you explicitly call DivineInject to create objects.
</p>
<p>
There are two ways of creating the root object:
</p>
<p>
1) by explicit type:
</p>
<pre>
private MainWindowViewModel CreateViewModel()
{
return DivineInjector.Current.Get<MainWindowViewModel>();
}
</pre>
<p>
2) by passing the type as an argument:
</p>
<pre>
public object GetInstance(InstanceContext instanceContext, Message message)
{
return DivineInjector.Current.Get(_instanceType);
}
</pre>
<hr/>
<p>Having understood how to configure and create singleton objects, read more about <a href="scopes.html">scopes in DivineInject</a>.
</section>
</div>
<footer>
<p>Hosted on GitHub Pages — Theme by <a href="https://github.com/orderedlist">orderedlist</a></p>
</footer>
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
</body>
</html>