<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tag 'AutoBox'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=AutoBox&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'AutoBox'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Writing ASP.NET MVC bootstrapper with Autobox</title><link>http://msmvps.com/blogs/mehfuz/archive/2012/01/30/writing-asp-net-mvc-bootstrapper-with-autobox.aspx</link><pubDate>Mon, 30 Jan 2012 06:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1805404</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;This will post will show how you can use AutoBox to easily write a bootstrapper for ASP.NET MVC. I have used the latest version of AutoBox (available from &lt;em&gt;nuget, &lt;/em&gt;this version also includes &lt;em&gt;Castle.Windsor&lt;/em&gt; internally for managing dependencies rather using its own resolver and does not require interface to type naming convention [IAccountRepository –&amp;gt; AccountRepository]) . To understand what is AutoBox , how you can use it for caching using memcached and let it automatically handle dependencies for controllers and repositories, i would recommend to take a look at this post:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/mehfuzh/archive/2011/11/06/introducing-autobox-on-the-fly-dependency-injection-and-caching-container.aspx" target="_blank"&gt;http://weblogs.asp.net/mehfuzh/archive/2011/11/06/introducing-autobox-on-the-fly-dependency-injection-and-caching-container.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Moving forward , let’s consider a simple bootstrapper interface:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46c414d8-1abb-4173-80b2-ba530f922d60" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IBootStrapperTask&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Execute();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;The &lt;em&gt;Execute()&lt;/em&gt;&amp;#160; method will be invoked during initialization for registering routes, controllers, mappings (&lt;em&gt;AutoMapper&lt;/em&gt;), etc. We will have one static factory (Ex. BootStrapper)&amp;#160; that will initiate it through &lt;em&gt;CommonServiceLocator.&lt;/em&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a04f252d-8743-47af-b7b9-d2ccbdcffeac" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BootStrapper&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Executes registered tasks.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Execute()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;ServiceLocator&lt;/span&gt;.Current.GetAllInstances&amp;lt;&lt;span style="color:#2b91af;"&gt;IBootStrapperTask&lt;/span&gt;&amp;gt;().ToList().ForEach(task =&amp;gt;task.Execute());&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;A typical example of a bootstrapping task could be &lt;em&gt;RegisterRoutes&lt;/em&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5a4de124-0fb3-4dcd-83d7-502ff0ee928c" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;RegisterRoutes&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IBootStrapperTask&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;RouteCollection&lt;/span&gt; routes;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Intializes the new instance of &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;see cref=&amp;quot;RegisterRoutes&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; RegisterRoutes()&lt;/li&gt; &lt;li&gt;        : &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;RouteTable&lt;/span&gt;.Routes)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000;"&gt;// intentionally left blank.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; Intializes the new instance of &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;see cref=&amp;quot;RegisterRoutes&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; class.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080;"&gt;///&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;/span&gt;&lt;span style="color:#808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;internal&lt;/span&gt; RegisterRoutes(&lt;span style="color:#2b91af;"&gt;RouteCollection&lt;/span&gt; routes)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;.routes = routes;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Execute()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li&gt;        routes.Clear();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        routes.IgnoreRoute(&lt;span style="color:#a31515;"&gt;&amp;quot;{resource}.axd/{*pathInfo}&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        routes.IgnoreRoute(&lt;span style="color:#a31515;"&gt;&amp;quot;{*favicon}&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; {favicon = &lt;span style="color:#a31515;"&gt;@&amp;quot;(.*/)?favicon.ico(/.*)?&amp;quot;}&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Routes&lt;/span&gt;.Register(routes);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Here on line 29, i have added &lt;em&gt;Routes.Register &lt;/em&gt;which is similar to the &lt;em&gt;BootStrapper.Execute()&lt;/em&gt; that further narrows down to specific routing classes. For example. &lt;em&gt;AccountRoutes&lt;/em&gt;. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f3ebc510-760b-4507-a2a2-aaaf988dfb9c" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Register(&lt;span style="color:#2b91af;"&gt;RouteCollection&lt;/span&gt; routes)&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;ServiceLocator&lt;/span&gt;.Current.GetAllInstances&amp;lt;&lt;span style="color:#2b91af;"&gt;IRoutesRegistration&lt;/span&gt;&amp;gt;().ToList()&lt;/li&gt; &lt;li&gt;       .ForEach(task =&amp;gt; task.Register(routes));&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The code inside &lt;em&gt;Routes&lt;/em&gt; class again pretty straight forward and exactly identical to &lt;em&gt;BootStrapper&lt;/em&gt; except the &lt;em&gt;IRoutesRegistration &lt;/em&gt;interface&lt;em&gt;. W&lt;/em&gt;e can also have &lt;em&gt;RegisterControllers&lt;/em&gt; but since the dependencies are automatically wired by AutoBox, we only need to specify (if required) what repository methods to be data cached and for what duration.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a7c5b040-d85c-4841-90f3-6cb80699674d" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;RegisterControllers&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;IBootStrapperTask&lt;/span&gt;&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IBootStrapperTask&lt;/span&gt;.Execute()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Container&lt;/span&gt;.Setup&amp;lt;&lt;span style="color:#2b91af;"&gt;ProductRepository&lt;/span&gt;&amp;gt;(x =&amp;gt; x.GetProductDetails(0)).Caches(&lt;span style="color:#2b91af;"&gt;TimeSpan&lt;/span&gt;.FromMinutes(1)).VaryByArgs();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;ControllerBuilder&lt;/span&gt;.Current.SetControllerFactory(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyControllerFactory&lt;/span&gt;());&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;More on caching and how &lt;em&gt;MyControllerFactory&lt;/em&gt; should look like; please check my previous post as well as the product documentation. Finally, it’s about gluing the whole thing together and we just need to have these lines in &lt;em&gt;global.asax.cs&lt;/em&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:307b9ed7-d2b4-44e2-ad74-3938d15a59c4" class="wlWriterEditableSmartContent"&gt; &lt;div class="le-pavsc-container"&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Application_Start()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000;"&gt;// Initalizes autobox self.&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Container&lt;/span&gt;.Init();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;BootStrapper&lt;/span&gt;.Execute();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;When you will run the project; if you followed the flow then it should work as you have expected it. The project page for AutoBox can be reached here: &lt;a href="http://mehfuzh.github.com/AutoBox/" target="_blank"&gt;http://mehfuzh.github.com/AutoBox/&lt;/a&gt; .&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Hope that helps&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8272204" width="1" height="1" alt="" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/burncsharp?a=nJKr-9l6NCs:bPk87BsRdOY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/burncsharp?d=yIl2AUoC8zA" border="0" alt="" /&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/burncsharp?a=nJKr-9l6NCs:bPk87BsRdOY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/burncsharp?d=qj6IDK7rITs" border="0" alt="" /&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/burncsharp?a=nJKr-9l6NCs:bPk87BsRdOY:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/burncsharp?d=7Q72WNTAKBA" border="0" alt="" /&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/burncsharp?a=nJKr-9l6NCs:bPk87BsRdOY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/burncsharp?i=nJKr-9l6NCs:bPk87BsRdOY:F7zBnMyn0Lo" border="0" alt="" /&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/burncsharp/~4/nJKr-9l6NCs" height="1" width="1" alt="" /&gt;</description></item></channel></rss>