/
AutoWiring.htm
685 lines (685 loc) · 51.4 KB
/
AutoWiring.htm
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Auto Wiring</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="structuremap.js"></script>
</head>
<body>
<p>
The best way to use an IoC container is to allow "Auto Wiring" to do most of the
work for you. IoC Containers like StructureMap are an infrastructure
concern, and as such, should be isolated from as much of your code as possible.
Before examining Auto Wiring in depth, let's look at a common anti pattern of
IoC usage:</p>
<h2>
IoC Container Anti-Pattern</h2>
<p>
One of the worst, but sadly most common, usages of an IoC container is
shown below:</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green128\blue0;\red0\green0\blue255;\red43\green145\blue175;}??\fs20 \cf3 // This is the way to write a Constructor Function with an IoC tool\par ??\cf0 \cf3 // Let the IoC container "inject" services from outside, and keep\par ??\cf0 \cf3 // ShippingScreenPresenter ignorant of the IoC infrastructure\par ??\cf0 \cf4 public\cf0 ShippingScreenPresenter(\cf5 IShippingService\cf0 service, \cf5 IRepository\cf0 repository)\par ?? \{\par ?? _service = service;\par ?? _repository = repository;\par ?? \}\par ??\par ?? \cf3 // FAIL!\par ??\cf0 \cf3 // This is the wrong way to use an IoC container. Do NOT invoke the container from\par ??\cf0 \cf3 // the constructor function. This tightly couples the ShippingScreenPresenter to\par ??\cf0 \cf3 // the IoC container in a harmful way. This class cannot be used in either\par ??\cf0 \cf3 // production or testing without a valid IoC configuration. Plus, you're writing more\par ??\cf0 \cf3 // code\par ??\cf0 \cf4 public\cf0 ShippingScreenPresenter()\par ?? \{\par ?? _service = \cf5 ObjectFactory\cf0 .GetInstance<\cf5 IShippingService\cf0 >();\par ?? _repository = \cf5 ObjectFactory\cf0 .GetInstance<\cf5 IRepository\cf0 >();\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
<span style="color: green;">// This is the way to
write a Constructor Function with an IoC tool</span></p>
<p style="margin: 0px;">
<span style="color: green;">// Let the IoC container
"inject" services from outside, and keep</span></p>
<p style="margin: 0px;">
<span style="color: green;">//
ShippingScreenPresenter ignorant of the IoC infrastructure</span></p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
ShippingScreenPresenter(<span style="color: #2b91af;">IShippingService</span>
service, <span style="color: #2b91af;">IRepository</span> repository)</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
_service = service;</p>
<p style="margin: 0px;">
_repository = repository;</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: green;">// FAIL!</span></p>
<p style="margin: 0px;">
<span style="color: green;">// This is the wrong way
to use an IoC container. Do NOT invoke the container from</span></p>
<p style="margin: 0px;">
<span style="color: green;">// the constructor
function. This tightly couples the ShippingScreenPresenter to</span></p>
<p style="margin: 0px;">
<span style="color: green;">// the IoC container in a
harmful way. This class cannot be used in either</span></p>
<p style="margin: 0px;">
<span style="color: green;">// production or testing
without a valid IoC configuration. Plus, you're writing more</span></p>
<p style="margin: 0px;">
<span style="color: green;">// code</span></p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
ShippingScreenPresenter()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
_service =
<span style="color: #2b91af;">ObjectFactory</span>.GetInstance<<span
style="color: #2b91af;">IShippingService</span>>();</p>
<p style="margin: 0px;">
_repository =
<span style="color: #2b91af;">ObjectFactory</span>.GetInstance<<span
style="color: #2b91af;">IRepository</span>>();</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<hr />
<h2>
Example</h2>
<p>
Typically, you’ll try to minimize the number of
<a href="%20%20%20%20%20%20%20%20%20%20%20%20Typically,%20you’ll%20try%20to%20minimize%20the%20number%20of%20Service%20Locator%20usages%20in%20">Service Locator</a>
(Container.Get*****) usages in
your system to a bare minimum (I found 8 in my current system, but I think I’ll
find a way to prune half of those later). Most of the value of an IoC tool
is in automatically doing Dependency Injection. I’m working with the new
MVC framework at the moment, so it’s a handy sample. Let’s say that we
have a Controller class for a typical CRUD screen. That Controller class
will generally need to interact with both validation services and the data
access functionality of the Repository. Here’s a representative Controller
class:<div class="code-sample"><p style="MARGIN: 0px" >
<span style="COLOR: blue">public</span> <span style="COLOR: blue">class</span>
<span style="COLOR: #2b91af">SomeScreenController</span> :
<span style="COLOR: #2b91af">IController</span></p>
<p style="MARGIN: 0px">
{</p>
<p style="MARGIN: 0px">
<span style="COLOR: blue">private</span>
<span style="COLOR: blue">readonly</span> <span style="COLOR: #2b91af">
IRepository</span> _repository;</p>
<p style="MARGIN: 0px">
<span style="COLOR: blue">private</span>
<span style="COLOR: blue">readonly</span> <span style="COLOR: #2b91af">
IValidator</span> _validator;</p>
<p style="MARGIN: 0px">
</p>
<p style="MARGIN: 0px">
<span style="COLOR: green">// SomeScreenController
depends on both IRepository and IValidator</span></p>
<p style="MARGIN: 0px">
<span style="COLOR: blue">public</span>
SomeScreenController(<span style="COLOR: #2b91af">IRepository</span> repository,
<span style="COLOR: #2b91af">IValidator</span> validator)</p>
<p style="MARGIN: 0px">
{</p>
<p style="MARGIN: 0px">
_repository = repository;</p>
<p style="MARGIN: 0px">
_validator = validator;</p>
<p style="MARGIN: 0px">
}</p>
<p style="MARGIN: 0px">
</p>
<p style="MARGIN: 0px">
}</p>
</div>
<p>
So let’s get StructureMap set up for this SomeScreenController class:</p>
<div style="BORDER-RIGHT: black thin solid; BORDER-TOP: black thin solid; FONT-SIZE: 10pt; BACKGROUND: white; OVERFLOW: scroll; BORDER-LEFT: black thin solid; COLOR: black; BORDER-BOTTOM: black thin solid; FONT-FAMILY: courier new">
<p style="MARGIN: 0px">
<span style="COLOR: #2b91af">
ObjectFactory</span>.Initialize(x =></p>
<p style="MARGIN: 0px">
{</p>
<p style="MARGIN: 0px">
<span style="COLOR: green">// Set up the IValidator</span></p>
<p style="MARGIN: 0px">
x.ForRequestedType<<span style="COLOR: #2b91af">IValidator</span>>().TheDefaultIsConcreteType<<span
style="COLOR: #2b91af">Validator</span>>();</p>
<p style="MARGIN: 0px">
</p>
<p style="MARGIN: 0px">
<span style="COLOR: green">// Set up the IRepository</span></p>
<p style="MARGIN: 0px">
x.ForRequestedType<<span style="COLOR: #2b91af">IRepository</span>>().Use<<span
style="COLOR: #2b91af">Repository</span>>()</p>
<p style="MARGIN: 0px">
.WithCtorArg(<span style="COLOR: #a31515">"connectionString"</span>).EqualToAppSetting(<span
style="COLOR: #a31515">"CONNECTION-STRING"</span>);</p>
<p style="MARGIN: 0px">
});</p>
</div>
<p>
You’ll notice that we didn’t make any explicit configuration for the
SomeScreenController class, but yet we could now call:</p>
<div style="BORDER-RIGHT: black thin solid; BORDER-TOP: black thin solid; FONT-SIZE: 10pt; BACKGROUND: white; OVERFLOW: scroll; BORDER-LEFT: black thin solid; COLOR: black; BORDER-BOTTOM: black thin solid; FONT-FAMILY: courier new">
<p style="MARGIN: 0px">
<span style="COLOR: blue">var</span>
controller = <span style="COLOR: #2b91af">ObjectFactory</span>.GetInstance<<span
style="COLOR: #2b91af">SomeScreenController</span>>();</p>
</div>
<p>
and StructureMap will happily create a new instance of the SomeScreenController
class by invoking its constructor and passing in a new Validator object and a
new Repository object created with the connection string from the App.config
file. We didn’t need to tell StructureMap how to construct
SomeScreenController because:
</p>
<ul>
<li>StructureMap can look at the constructor function of SomeScreenController and
see that it depends on IValidator and IRepository </li>
<li>StructureMap “knows” about the default way to create and return an IValidator
and an IRepository </li>
</ul>
<p>
This feature is known as “auto wiring,” and all the mainstream IoC containers
support this feature to some extent or another.
</p>
<hr />
<h2>StructureMap's Policies for Auto Wiring</h2>
<p>By default, as long as an object is being created by invoking its constructor
function, StructureMap will try to create/resolve/find an object for each
non-primitive dependency in the requested concrete type. If StructureMap
doesn't "know" how to find a requested dependency, it will throw an exception.
By design, StructureMap cannot auto wire primitive arguments like strings and
numbers. The Auto Wiring can be overriden by explicit configuration (this
might actually be easier with Xml configuration):</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 registry.InstanceOf<\cf3 Rule\cf0 >()\par ?? .Is.OfConcreteType<\cf3 WidgetRule\cf0 >()\par ?? .WithName(\cf4 "TheWidgetRule"\cf0 )\par ?? .CtorDependency<\cf3 IWidget\cf0 >().Is(i => i.TheInstanceNamed(\cf4 "Yellow"\cf0 ));}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
registry.InstanceOf<<span style="color: #2b91af;">Rule</span>>()</p>
<p style="margin: 0px;">
.Is.OfConcreteType<<span style="color: #2b91af;">WidgetRule</span>>()</p>
<p style="margin: 0px;">
.WithName(<span style="color: #a31515;">"TheWidgetRule"</span>)</p>
<p style="margin: 0px;">
.CtorDependency<<span style="color: #2b91af;">IWidget</span>>().Is(i =>
i.TheInstanceNamed(<span style="color: #a31515;">"Yellow"</span>));</p>
</div>
<!--EndFragment-->
<p>In the example above, the IWidget dependency of the WidgetRule class is
overriden.</p>
<hr />
<h4>Object Identity within a Single Request</h4>
<p>Within a single object request, StructureMap will only create a single object for
a single Instance configuration. What that means in effect is that if two
or more objects in a single request need the same dependency, those two objects
will get the exact same instance of that dependency. Let's immediately
jump into code to demonstrate this.</p>
<p>This auto wiring policy was intended for objects that need to be shared by lots
of other objects. A common example of this is some sort of DataContext
class:</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf3 public\cf0 \cf3 class\cf0 \cf4 DataContext\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf4 Guid\cf0 _id = \cf4 Guid\cf0 .NewGuid();\par ??\par ?? \cf3 public\cf0 \cf3 override\cf0 \cf3 string\cf0 ToString()\par ?? \{\par ?? \cf3 return\cf0 \cf3 string\cf0 .Format(\cf5 "Id: \{0\}"\cf0 , _id);\par ?? \}\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">class</span> <span style="color: #2b91af;">
DataContext</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
private</span> <span style="color: #2b91af;">Guid</span> _id =
<span style="color: #2b91af;">Guid</span>.NewGuid();</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> <span style="color: blue;">override</span>
<span style="color: blue;">string</span> ToString()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">return</span> <span style="color: blue;">string</span>.Format(<span
style="color: #a31515;">"Id: {0}"</span>, _id);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<p>Now, let's say that I have a hierarchy of classes that all need to work on a
DataContext:</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf3 public\cf0 \cf3 class\cf0 \cf4 Class1\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 DataContext\cf0 _context;\par ??\par ?? \cf3 public\cf0 Class1(\cf4 DataContext\cf0 context)\par ?? \{\par ?? _context = context;\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 override\cf0 \cf3 string\cf0 ToString()\par ?? \{\par ?? \cf3 return\cf0 \cf3 string\cf0 .Format(\cf5 "Class1 has Context: \{0\}"\cf0 , _context);\par ?? \}\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 class\cf0 \cf4 Class2\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 Class1\cf0 _class1;\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 DataContext\cf0 _context;\par ??\par ?? \cf3 public\cf0 Class2(\cf4 Class1\cf0 class1, \cf4 DataContext\cf0 context)\par ?? \{\par ?? _class1 = class1;\par ?? _context = context;\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 override\cf0 \cf3 string\cf0 ToString()\par ?? \{\par ?? \cf3 return\cf0 \cf3 string\cf0 .Format(\cf5 "Class2 has Context: \{0\}\\n\{1\}"\cf0 , _context, _class1);\par ?? \}\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 class\cf0 \cf4 Class3\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 Class2\cf0 _class2;\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 DataContext\cf0 _context;\par ??\par ?? \cf3 public\cf0 Class3(\cf4 Class2\cf0 class2, \cf4 DataContext\cf0 context)\par ?? \{\par ?? _class2 = class2;\par ?? _context = context;\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 override\cf0 \cf3 string\cf0 ToString()\par ?? \{\par ?? \cf3 return\cf0 \cf3 string\cf0 .Format(\cf5 "Class3 has Context: \{0\}\\n\{1\}"\cf0 , _context, _class2);\par ?? \}\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">class</span> <span style="color: #2b91af;">Class1</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> Class1(<span style="color: #2b91af;">DataContext</span> context){}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> <span style="color: blue;">override</span>
<span style="color: blue;">string</span> ToString()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">return</span> <span style="color: blue;">string</span>.Format(<span
style="color: #a31515;">"Class1 has Context: {0}"</span>, _context);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">class</span> <span style="color: #2b91af;">Class2</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> Class2(<span style="color: #2b91af;">Class1</span> class1,
<span style="color: #2b91af;">DataContext</span> context) {}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> <span style="color: blue;">override</span>
<span style="color: blue;">string</span> ToString()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">return</span> <span style="color: blue;">string</span>.Format(<span
style="color: #a31515;">"Class2 has Context: {0}\n{1}"</span>, _context,
_class1);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">class</span> <span style="color: #2b91af;">Class3</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span> Class3(<span style="color: #2b91af;">Class2</span>
class2, <span style="color: #2b91af;">DataContext</span> context) {}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">
public</span> <span style="color: blue;">override</span>
<span style="color: blue;">string</span> ToString()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">return</span> <span style="color: blue;">string</span>.Format(<span
style="color: #a31515;">"Class3 has Context: {0}\n{1}"</span>, _context,
_class2);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<p>When you request an object of Class3 with a call to
Container.GetInstance<Class3>() like this:</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 [\cf3 Test\cf0 ]\par ?? \cf4 public\cf0 \cf4 void\cf0 demonstrate_session_identity()\par ?? \{\par ?? \cf4 var\cf0 class3 = container.GetInstance<\cf3 Class3\cf0 >();\par ?? \cf3 Debug\cf0 .WriteLine(class3);\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
[<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">void</span> demonstrate_session_identity()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
var</span> class3 = container.GetInstance<<span style="color: #2b91af;">Class3</span>>();</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(class3);</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<p>The output is:</p>
<pre>
Class3 has Context: Id: 3abe0330-e94f-48a3-b8c3-56d278eea07f
Class2 has Context: Id: 3abe0330-e94f-48a3-b8c3-56d278eea07f
Class1 has Context: Id: 3abe0330-e94f-48a3-b8c3-56d278eea07f
</pre>
<p>In the sample above, when we write out the Class3, Class2, and Class1 objects to Debug.WriteLine, we find that each of these objects have a reference to the same DataContext. If we were to run this test again, the output might be:</p>
<pre>
Class3 has Context: Id: 109329ce-4058-4a35-9fd1-46d47c1e69e7
Class2 has Context: Id: 109329ce-4058-4a35-9fd1-46d47c1e69e7
Class1 has Context: Id: 109329ce-4058-4a35-9fd1-46d47c1e69e7
</pre>
<p>We see the exact same behavior, but it was a different object instance of DataContext for the new object graph.</p>
<p>This behavior also applies to objects passed in to the Container as an explicit argument:</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 [\cf3 Test\cf0 ]\par ?? \cf4 public\cf0 \cf4 void\cf0 demonstrate_session_identity_with_explicit_argument()\par ?? \{\par ?? \cf3 DataContext\cf0 context = \cf4 new\cf0 \cf3 DataContext\cf0 ();\par ?? \cf3 Debug\cf0 .WriteLine(\cf5 "The context being passed in is "\cf0 + context);\par ??\par ?? \cf4 var\cf0 class3 = container.With(context).GetInstance<\cf3 Class3\cf0 >();\par ?? \cf3 Debug\cf0 .WriteLine(class3);\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
[<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">void</span>
demonstrate_session_identity_with_explicit_argument()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
DataContext</span> context = <span style="color: blue;">new</span>
<span style="color: #2b91af;">DataContext</span>();</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(<span style="color: #a31515;">"The context being passed
in is "</span> + context);</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">
var</span> class3 = container.With(context).GetInstance<<span
style="color: #2b91af;">Class3</span>>();</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(class3);</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<p>The output of this unit test is:</p>
<pre>
The context being passed in is Id: 87ddccfd-a441-41fd-a86d-3f32987496ba
Class3 has Context: Id: 87ddccfd-a441-41fd-a86d-3f32987496ba
Class2 has Context: Id: 87ddccfd-a441-41fd-a86d-3f32987496ba
Class1 has Context: Id: 87ddccfd-a441-41fd-a86d-3f32987496ba
</pre>
<p>The point of the sample above is just to show that the object instance of DataContext passed into the Container is used to create the Class3, Class2, and Class1 objects.</p>
<hr />
<h4>Injecting Arrays of Services</h4>
<p>StructureMap has always supported Dependency Injection of arrays of dependency
objects. New in StructureMap 2.5+ is a policy that if any array of
dependencies is not explicitly specified, StructureMap will inject all possible
instances of that dependency type. The sample below illustrates this auto
wiring policy. I have a class called "ClassThatUsesValidators" that needs
an array of IValidator objects. Below, I've configured two different
Instances of ClassThatUsesValidator, one that explicitly configures its children
IValidator and another Instance that is just going to let auto wiring inject the
IValidator's.</p>
<!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf3 public\cf0 \cf3 interface\cf0 \cf4 IValidator\par ??\cf0 \{\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 class\cf0 \cf4 Validator\cf0 : \cf4 IValidator\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf3 string\cf0 _name;\par ??\par ?? \cf3 public\cf0 Validator(\cf3 string\cf0 name)\par ?? \{\par ?? _name = name;\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 override\cf0 \cf3 string\cf0 ToString()\par ?? \{\par ?? \cf3 return\cf0 \cf3 string\cf0 .Format(\cf5 "Name: \{0\}"\cf0 , _name);\par ?? \}\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 class\cf0 \cf4 ClassThatUsesValidators\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf3 readonly\cf0 \cf4 IValidator\cf0 [] _validators;\par ??\par ?? \cf3 public\cf0 ClassThatUsesValidators(\cf4 IValidator\cf0 [] validators)\par ?? \{\par ?? _validators = validators;\par ?? \}\par ??\par ?? \cf3 public\cf0 \cf3 void\cf0 Write()\par ?? \{\par ?? \cf3 foreach\cf0 (\cf4 IValidator\cf0 validator \cf3 in\cf0 _validators)\par ?? \{\par ?? \cf4 Debug\cf0 .WriteLine(validator);\par ?? \}\par ?? \}\par ?? \}\par ??\par ?? [\cf4 TestFixture\cf0 ]\par ?? \cf3 public\cf0 \cf3 class\cf0 \cf4 ValidatorExamples\par ??\cf0 \{\par ?? \cf3 private\cf0 \cf4 Container\cf0 container;\par ??\par ?? [\cf4 SetUp\cf0 ]\par ?? \cf3 public\cf0 \cf3 void\cf0 SetUp()\par ?? \{\par ?? container = \cf3 new\cf0 \cf4 Container\cf0 (x =>\par ?? \{\par ?? x.ForRequestedType<\cf4 IValidator\cf0 >().AddInstances(o =>\par ?? \{\par ?? o.OfConcreteType<\cf4 Validator\cf0 >().WithCtorArg(\cf5 "name"\cf0 ).EqualTo(\cf5 "Red"\cf0 ).WithName(\cf5 "Red"\cf0 );\par ?? o.OfConcreteType<\cf4 Validator\cf0 >().WithCtorArg(\cf5 "name"\cf0 ).EqualTo(\cf5 "Blue"\cf0 ).WithName(\cf5 "Blue"\cf0 );\par ?? o.OfConcreteType<\cf4 Validator\cf0 >().WithCtorArg(\cf5 "name"\cf0 ).EqualTo(\cf5 "Purple"\cf0 ).WithName(\cf5 "Purple"\cf0 );\par ?? o.OfConcreteType<\cf4 Validator\cf0 >().WithCtorArg(\cf5 "name"\cf0 ).EqualTo(\cf5 "Green"\cf0 ).WithName(\cf5 "Green"\cf0 );\par ?? \});\par ??\par ?? x.ForRequestedType<\cf4 ClassThatUsesValidators\cf0 >().AddInstances(o =>\par ?? \{\par ?? \cf6 // Define an Instance of ClassThatUsesValidators that depends on AutoWiring\par ??\cf0 o.OfConcreteType<\cf4 ClassThatUsesValidators\cf0 >().WithName(\cf5 "WithAutoWiring"\cf0 );\par ??\par ?? \cf6 // Define an Instance of ClassThatUsesValidators that overrides AutoWiring\par ??\cf0 o.OfConcreteType<\cf4 ClassThatUsesValidators\cf0 >().WithName(\cf5 "ExplicitArray"\cf0 )\par ?? .TheArrayOf<\cf4 IValidator\cf0 >().Contains(y =>\par ?? \{\par ?? y.TheInstanceNamed(\cf5 "Red"\cf0 );\par ?? y.TheInstanceNamed(\cf5 "Green"\cf0 );\par ?? \});\par ?? \});\par ?? \});\par ?? \}\par ??\par ?? [\cf4 Test\cf0 ]\par ?? \cf3 public\cf0 \cf3 void\cf0 what_are_the_validators()\par ?? \{\par ?? \cf4 Debug\cf0 .WriteLine(\cf5 "With Auto Wiring"\cf0 );\par ?? container.GetInstance<\cf4 ClassThatUsesValidators\cf0 >(\cf5 "WithAutoWiring"\cf0 ).Write();\par ?? \cf4 Debug\cf0 .WriteLine(\cf5 "================================="\cf0 );\par ?? \cf4 Debug\cf0 .WriteLine(\cf5 "With Explicit Configuration"\cf0 );\par ?? container.GetInstance<\cf4 ClassThatUsesValidators\cf0 >(\cf5 "ExplicitArray"\cf0 ).Write();\par ?? \}\par ?? \}}
-->
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: black thin solid;">
<p style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">interface</span>
<span style="color: #2b91af;">IValidator</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">Validator</span> : <span style="color: #2b91af;">
IValidator</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">private</span>
<span style="color: blue;">readonly</span> <span style="color: blue;">string</span>
_name;</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span> Validator(<span
style="color: blue;">string</span> name)</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
_name = name;</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">override</span> <span style="color: blue;">string</span>
ToString()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
return</span> <span style="color: blue;">string</span>.Format(<span
style="color: #a31515;">"Name: {0}"</span>, _name);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">ClassThatUsesValidators</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">private</span>
<span style="color: blue;">readonly</span> <span style="color: #2b91af;">
IValidator</span>[] _validators;</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
ClassThatUsesValidators(<span style="color: #2b91af;">IValidator</span>[]
validators)</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
_validators = validators;</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">void</span> Write()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">
foreach</span> (<span style="color: #2b91af;">IValidator</span> validator
<span style="color: blue;">in</span> _validators)</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">Debug</span>.WriteLine(validator);</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
[<span style="color: #2b91af;">TestFixture</span>]</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span> <span style="color: blue;">class</span>
<span style="color: #2b91af;">ValidatorExamples</span></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: blue;">private</span>
<span style="color: #2b91af;">Container</span> container;</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
[<span style="color: #2b91af;">SetUp</span>]</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">void</span> SetUp()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
container =
<span style="color: blue;">new</span> <span style="color: #2b91af;">Container</span>(x
=></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
x.ForRequestedType<<span style="color: #2b91af;">IValidator</span>>().AddInstances(o
=></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">Validator</span>>().WithCtorArg(<span
style="color: #a31515;">"name"</span>).EqualTo(<span
style="color: #a31515;">"Red"</span>).WithName(<span
style="color: #a31515;">"Red"</span>);</p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">Validator</span>>().WithCtorArg(<span
style="color: #a31515;">"name"</span>).EqualTo(<span
style="color: #a31515;">"Blue"</span>).WithName(<span
style="color: #a31515;">"Blue"</span>);</p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">Validator</span>>().WithCtorArg(<span
style="color: #a31515;">"name"</span>).EqualTo(<span
style="color: #a31515;">"Purple"</span>).WithName(<span
style="color: #a31515;">"Purple"</span>);</p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">Validator</span>>().WithCtorArg(<span
style="color: #a31515;">"name"</span>).EqualTo(<span
style="color: #a31515;">"Green"</span>).WithName(<span
style="color: #a31515;">"Green"</span>);</p>
<p style="margin: 0px;">
});</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
x.ForRequestedType<<span style="color: #2b91af;">ClassThatUsesValidators</span>>().AddInstances(o
=></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: green;">// Define an Instance of ClassThatUsesValidators
that depends on AutoWiring</span></p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">ClassThatUsesValidators</span>>().WithName(<span
style="color: #a31515;">"WithAutoWiring"</span>);</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
<span style="color: green;">// Define an Instance of ClassThatUsesValidators
that overrides AutoWiring</span></p>
<p style="margin: 0px;">
o.OfConcreteType<<span style="color: #2b91af;">ClassThatUsesValidators</span>>().WithName(<span
style="color: #a31515;">"ExplicitArray"</span>)</p>
<p style="margin: 0px;">
.TheArrayOf<<span style="color: #2b91af;">IValidator</span>>().Contains(y =></p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
y.TheInstanceNamed(<span style="color: #a31515;">"Red"</span>);</p>
<p style="margin: 0px;">
y.TheInstanceNamed(<span style="color: #a31515;">"Green"</span>);</p>
<p style="margin: 0px;">
});</p>
<p style="margin: 0px;">
});</p>
<p style="margin: 0px;">
});</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
</p>
<p style="margin: 0px;">
[<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;">
<span style="color: blue;">public</span>
<span style="color: blue;">void</span> what_are_the_validators()</p>
<p style="margin: 0px;">
{</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(<span style="color: #a31515;">"With Auto Wiring"</span>);</p>
<p style="margin: 0px;">
container.GetInstance<<span
style="color: #2b91af;">ClassThatUsesValidators</span>>(<span
style="color: #a31515;">"WithAutoWiring"</span>).Write();</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(<span style="color: #a31515;">"================================="</span>);</p>
<p style="margin: 0px;">
<span style="color: #2b91af;">
Debug</span>.WriteLine(<span style="color: #a31515;">"With Explicit
Configuration"</span>);</p>
<p style="margin: 0px;">
container.GetInstance<<span
style="color: #2b91af;">ClassThatUsesValidators</span>>(<span
style="color: #a31515;">"ExplicitArray"</span>).Write();</p>
<p style="margin: 0px;">
}</p>
<p style="margin: 0px;">
}</p>
</div>
<!--EndFragment-->
<p>The output of what_are_the_validators() is:</p>
<pre>
With Auto Wiring
Name: Red
Name: Blue
Name: Purple
Name: Green
=================================
With Explicit Configuration
Name: Red
Name: Green
</pre>
<hr />
</body>
</html>