-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
176 lines (139 loc) · 13.9 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>StructureMap - Registration</title>
<link href="/content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="/content/prism.css" rel="stylesheet" type="text/css" />
<link href="/content/theme.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />
<link rel="icon" type="image/x-icon" href="/content/images/structuremap-logo.ico">
<link rel="icon" type="image/png" href="/content/images/structuremap-logo-60x60.png">
<link rel="apple-touch-icon" type="image/png" href="/content/images/structuremap-logo-60x60.png">
<link rel="apple-touch-icon" type="image/png" sizes="72x72" href="/content/images/structuremap-logo-72x72.png">
<link rel="apple-touch-icon" type="image/png" sizes="114x114" href="/content/images/structuremap-logo-114x114.png">
<link rel="apple-touch-icon" type="image/png" sizes="144x144" href="/content/images/structuremap-logo-144x144.png">
<!-- CSS code from Bootply.com editor -->
<link href="/content/affix.css" rel="stylesheet" type="text/css" />
</head>
<!-- HTML code from Bootply.com editor -->
<body >
<a href="https://github.com/structuremap/structuremap"><img style="z-index: 5000; position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"></a>
<nav class="navbar navbar-default navbar-fixed-top" role="banner">
<div class="container">
<div class="navbar-header">
<a href="/" class="navbar-brand">StructureMap 4.7.0</a>
</div>
<nav class="collapse navbar-collapse" role="navigation">
<ul class="nav navbar-nav pull-right">
<li>
<a href="/quickstart">A Gentle Quickstart</a>
</li>
<li>
<a href="/documentation">Documentation</a>
</li>
<li>
<a href="https://groups.google.com/forum/#!forum/structuremap-users">Google Group</a>
</li>
<li>
<a href="https://gitter.im/structuremap/structuremap?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://camo.githubusercontent.com/da2edb525cde1455a622c58c0effc3a90b9a181c/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667" alt="Join the chat at https://gitter.im/structuremap/structuremap" data-canonical-src="https://badges.gitter.im/Join%20Chat.svg" style="max-width:100%;"></a>
</li>
<li><a href="/setter-injection" title="Setter Injection">Previous</a></li>
<li><a href="/registration/registry-dsl" title="Registry DSL">Next</a></li>
</ul>
<div class="navbar-form navbar-left" role="search">
<div class="form-group">
<input id="search" type="search" class="form-control" placeholder="Search">
</div>
</div>
</nav>
</div>
</nav>
<div class="container">
<nav class="navbar-inverse">
<ol class="breadcrumb"><li><a href="/">StructureMap</a></li><li class="active">Registration</li></ol>
</nav>
</div>
<!--main-->
<div class="container">
<div class="row">
<!--left-->
<div class="col-md-3" id="leftCol">
<ul class="nav nav-stacked affix" id="sidebar">
<li><h3><img src="/content/images/structuremap-logo-210x210.png" alt="StructureMap 4.7.0" width="210" height="210"></h3></li>
<li><h3 class="no-margin">Next</h3><p><a href="/registration/registry-dsl">Registry DSL</a></p></li>
<li><h3 class="no-margin">Previous</h3><a href="/setter-injection">Setter Injection</a></p></li>
</ul>
</div><!--/left-->
<!--right-->
<div class="col-md-9">
<h1>Registration <a href="https://github.com/structuremap/structuremap/blob/master/documentation/registration/index.md" class="text-muted small pull-right fa fa-github" style="margin-top: 10px"> Edit on GitHub</a></h1>
<hr />
<div id="main-pane">
<!--Title: Registration-->
<!--Url: registration-->
<p>As of the 3.0 release, StructureMap provides a streamlined fluent interface called the <em>Registry DSL</em> to configure a StructureMap
Container with both explicit registrations and conventional auto-registrations. StructureMap no longer supports Xml configuration or MEF-style attribute configuration -- but there is some facility for rolling your own attribute-based configuration support.</p>
<p>The first step in using StructureMap is configuring a <code>Container</code> object. The following examples are based on the usage of the <a href="/registration/registry-dsl">Registry DSL</a>.</p>
<p>Let's say that you have a simple set of services like this:</p>
<pre><code class="language-csharp">
public interface IBar
{
}

public class Bar : IBar
{
}

public interface IFoo
{
}

public class Foo : IFoo
{
 public IBar Bar { get; private set; }

 public Foo(IBar bar)
 {
 Bar = bar;
 }
}

</code></pre>
<p>A simple configuration of a StructureMap Container might then be:</p>
<pre><code class="language-csharp">
// Example #1 - Create an container instance and directly pass in the configuration.
 var container1 = new Container(c =>
 {
 c.For<IFoo>().Use<Foo>();
 c.For<IBar>().Use<Bar>();
 });

// Example #2 - Create an container instance but add configuration later.
 var container2 = new Container();

 container2.Configure(c =>
 {
 c.For<IFoo>().Use<Foo>();
 c.For<IBar>().Use<Bar>();
 });
</code></pre>
<p>Initializing or configuring the container is usually done at application startup and is located as close as possible to the application's entry point.
This place is sometimes referred to as the composition root of the application.
In our example we are composing our application's object graph by connecting abstractions to concrete types.</p>
<p>We are using the fluent API <code>For<TInterface>().Use<TConcrete>()</code> which registers a default instance for a given plugin type (the TInterface type in this case). In our example we want an new instance of <code>Foo</code> every time we request the abstraction <code>IFoo</code>.</p>
<p>The recommended way of using the <a href="/registration/registry-dsl">Registry DSL</a> is by defining one or more <code>Registry</code> classes. Typically, you would subclass the <code>Registry</code> class,
then use the Fluent API methods exposed by the <code>Registry</code> class to describe a <code>Container</code> configuration.</p>
<p>Here's a sample <code>Registry</code> class used to configure the same types as in our previous example:</p>
<pre><code class="language-csharp">
public class FooBarRegistry : Registry
{
 public FooBarRegistry()
 {
 For<IFoo>().Use<Foo>();
 For<IBar>().Use<Bar>();
 }
}

</code></pre>
<p>When you set up a <code>Container</code> , you need to simply direct the <code>Container</code> to use the configuration in that <code>Registry</code> class.</p>
<pre><code class="language-csharp">
// Example #1
 var container1 = new Container(new FooBarRegistry());

// Example #2
 var container2 = new Container(c => { c.AddRegistry<FooBarRegistry>(); });

// Example #3 -- create a container for a single Registry
 var container3 = Container.For<FooBarRegistry>();
</code></pre>
<div class="alert alert-info" role="alert">The StructureMap team highly recommends using `Registry` classes for your real application configuration. The syntax is shorter inside the Registry class constructor than from within the `Container` constructor method. In addition, Registry classes can be used to modularize the configuration of a bigger application into more manageable chunks. Lastly, using `Registry` classes makes it easier to stand up additional `Container` objects in testing scenarios that reflect the real application composition.</div>
<p>In real world applications you also have to deal with repetitive similar registrations. Such registrations are tedious, easy to forget and can be a weak spot in your application. StructureMap provides <a href="/registration/auto-registration-and-conventions">Auto-Registration and Conventions</a> which mitigates this pain and eases the maintenance burden. StructureMap exposes this feature through the <a href="/registration/registry-dsl">Registry DSL</a> by the <code>Scan</code> method.</p>
<p>In our example there is an reoccuring pattern, we are connecting the plugin type <code>ISomething</code> to a concrete type <code>Something</code>, meaning <code>IFoo</code> to <code>Foo</code> and <code>IBar</code> to <code>Bar</code>. Wouldn't it be cool if we could write a convention for exactly doing that? Fortunatly StructureMap has already one build in. Let's see how we can create an container with the same configuration as in the above examples.</p>
<pre><code class="language-csharp">
// Example #1
 var container1 = new Container(c =>
 c.Scan(scanner =>
 {
 scanner.TheCallingAssembly();
 scanner.WithDefaultConventions();
 }));

// Example #2
 var container2 = new Container();

 container2.Configure(c =>
 c.Scan(scanner =>
 {
 scanner.TheCallingAssembly();
 scanner.WithDefaultConventions();
 }));


</code></pre>
<p>We instruct the scanner to scan through the calling assembly with default conventions on. This will find and register the default instance for <code>IFoo</code> and <code>IBar</code> which are obviously the concrete types <code>Foo</code> and <code>Bar</code>. Now whenever you add an additional interface <code>IMoreFoo</code> and a class <code>MoreFoo</code> to your application's code base, it's automatically picked up by the scanner.</p>
<p>Sometimes classes need to be supplied with some primitive value in its constructor. For example the <code>System.Data.SqlClient.SqlConnection</code> needs to be supplied with the connection string in its constructor. No problem, just set up the value of the constructor argument in the bootstrapping:</p>
<pre><code class="language-csharp">
var container = new Container(c =>
{
 //just for demo purposes, normally you don't want to embed the connection string directly into code.
 c.For<IDbConnection>().Use<SqlConnection>().Ctor<string>().Is("YOUR_CONNECTION_STRING");
 //a better way would be providing a delegate that retrieves the value from your app config. 
});
</code></pre>
<p>So far you have seen an couple of ways to work with the <a href="/registration/registry-dsl">Registry DSL</a> and configure a <code>Container</code> object. We have seen examples of configuration that allow us to build objects that don't depend on anything like the <code>Bar</code> class, or do depend on other types like the <code>Foo</code> class needs an instance of <code>IBar</code>. In our last example we have seen configuration for objects that need some primitive types like strings in its constructor function.</p>
</div>
<hr />
<nav>
<span>
<strong>Previous: </strong><a href="/setter-injection">Setter Injection</a>
</span>
<span class="pull-right">
<strong>Next: </strong><a href="/registration/registry-dsl">Registry DSL</a>
</span>
</nav>
</div><!--/right-->
</div><!--/row-->
</div><!--/container-->
</body>
<foot>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type='text/javascript' src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/content/embed.js"></script>
<script type="text/javascript" src="/content/prism.js"></script>
<script type="text/javascript" src="/content/sidebar.js"></script>
<script type="text/javascript" src="/content/affix.js"></script>
<script>
$('#search').keyup(function(e){
if(e.keyCode == 13) {
var search = $('#search').val();
var url = 'https://www.google.com/#q=site:structuremap.github.io ' + search;
url = encodeURI(url);
//alert(url);
window.location.href = url;
e.stopPropagation();
if (e.cancelBubble!=null) e.cancelBubble = true;
return false;
}
});
</script>
</foot>
</html>