<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DucDigital &#187; controller</title>
	<atom:link href="http://www.ducdigital.com/tag/controller/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ducdigital.com</link>
	<description>for ( $girl = 1; $girl &#60; $required; $girl++ ) { echo "I love DucDigital"; }</description>
	<lastBuildDate>Sat, 07 Aug 2010 09:12:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Cache individual object in asp.net mvc</title>
		<link>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/</link>
		<comments>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 05:37:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[linq2sql]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[outputcache]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sap.net mvc]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=218</guid>
		<description><![CDATA[Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, [...]]]></description>
			<content:encoded><![CDATA[<p>Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, which is not practical in real life. So finally, i found out that i can easily cache objects into a cache provider in Asp.net Mvc. Very useful, all my queries go back to 0 (traced through Ling2sql profiler). </p>
<p>You can use this piece of code in anywhere of your code where you want to cache any object.<br />
(Notice: this code was used in a controller, so I used HttpContext.Cache. But if you use else where that make this code not working, try HttpContext.Current.Cache)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                link <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                HttpContext.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ObjName&quot;</span>, link, <span style="color: #0600FF;">null</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">NoAbsoluteExpiration</span>, <span style="color: #008000;">new</span> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">30</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">CacheItemPriority</span>.<span style="color: #0000FF;">High</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>What it did was to check if the ObjName exist in the context, if not, create one and dump the data into it. Remember when you call back from HttpContext, it's an object so you always need a cast. If you don't know about argument, let the intellisense do the work for you.</p>
<p>Time to exand further about the topic: <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(VS.80).aspxx">HttpContext.Cache.Add</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2010%2F01%2F14%2Fcache-individual-object-in-asp-net-mvc%2F&amp;linkname=Cache%20individual%20object%20in%20asp.net%20mvc"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Builtin Async Controller in ASP.net MVC 2</title>
		<link>http://www.ducdigital.com/2009/12/24/builtin-async-controller-in-asp-net-mvc-2/</link>
		<comments>http://www.ducdigital.com/2009/12/24/builtin-async-controller-in-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 02:00:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[Asynchronous]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=196</guid>
		<description><![CDATA[Well, it's Christmas Eve at the time I post this post in my place. I hope you enjoy your Christmas with your family and happy new year to all of you who read this post of mine. ------------------------ One thing that make me feel more powerful when using ASP.net MVC is the newly introduce Asynchronous [...]]]></description>
			<content:encoded><![CDATA[<p>Well, it's Christmas Eve at the time I post this post in my place. I hope you enjoy your Christmas with your family and happy new year to all of you who read this post of mine.</p>
<p>------------------------</p>
<p>One thing that make me feel more powerful when using ASP.net MVC is the newly introduce Asynchronous Controller Action. </p>
<p>Imagine, you have a front page with multiple widget, like news, weather, personal information, new forum thread etc... You are going to run it from top to bottom, query, query... Your basic solution for this is to query news first since it's kinda important, then weather, etc...</p>
<p><strong>Why it's not a good solution?</strong> Because if you going to query everything like that, your program need to wait for each query to finish before continue to work with the code behind...</p>
<p><strong>Better Solution?</strong> I could suggest you using Async Controller in this case. Separate each query into different Data Context, query to the database, Close Current Data Context, and process the info with code behind, separately and process at the same time. This helps you bring down processing time twice or thrice as fast than the normal approach...</p>
<p>Asynchronous Process has been introduce to ASP.net long before, but now, apply it to asp.net MVC even easier than what you really expect.</p>
<ul>
<li>Inherit to "AsyncController" instead of "Controller" class</li>
<li>Write async method</li>
<li>Write Complete method</li>
<li>Write functions to handle async request</li>
</ul>
<p>First step:</p>
<p>Look out for the head of your controller, you will always see this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyController <span style="color: #008000;">:</span> Controller</pre></div></div>

<p>change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyController <span style="color: #008000;">:</span> AsyncController</pre></div></div>

<p>Don't worry because everything will work just as normal even if you don't use AsyncController.</p>
<p>2nd Step, write the MethodAsync and MethodCompleted. The important is the prefix in this case, Async and Completed. </p>
<ul>
<li><strong>Method </strong>is the action name, like Create, Delete, Details</li>
<li><strong>Async </strong>is in Void type, and everything request first will come to this void method, you can get the input from form, url here in this method. Async will pass parameter to Completed using <strong>AsyncManager.Parameter</strong>s that you can see later in this article</li>
<li><strong>Completed </strong> is an ActionResult method. just like your normal Create, Delete action. You can return a View or RedirectResult here. The param of this method corresponding to the <em>AsyncManager.Parameter </em>that was used in <em>MethodAsync</em></li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #008080; font-style: italic;">// Http://local/Controller/Method/</span>
<span style="color: #008080; font-style: italic;">// As you can see i am using the attribute that can be use in the normal Actions</span>
<span style="color: #000000;">&#91;</span>AcceptVerbs<span style="color: #000000;">&#40;</span>HttpVerbs.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#41;</span>, ActionName<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Method&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> MethodAsync<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> id, <span style="color: #FF0000;">string</span> hello<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This Increment() use to identify how many async process are there, </span>
	<span style="color: #008080; font-style: italic;">// once everything is back to 0, MethodCompleted is fire-up</span>
&nbsp;
	AsyncManager.<span style="color: #0000FF;">OutstandingOperations</span>.<span style="color: #0000FF;">Increment</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This used to handle functions that are not Async Ready</span>
    <span style="color: #008080; font-style: italic;">// since they don't have any async event ready.</span>
	<span style="color: #008080; font-style: italic;">// using this you can start with any method.</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// the first int, string is the parameter data type, the last bool is the return type.</span>
	<span style="color: #008080; font-style: italic;">// DoTestAsync is the method name that we going to write later on</span>
&nbsp;
    Func<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span> , <span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;</span> doTestHandler <span style="color: #008000;">=</span> DoTestAsync<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Begin async</span>
	<span style="color: #008080; font-style: italic;">// ID, Hello is the param passing to method DoTestAsync</span>
    doTestHandler.<span style="color: #0000FF;">BeginInvoke</span><span style="color: #000000;">&#40;</span>id, hello, ar <span style="color: #008000;">=&gt;</span>
    <span style="color: #000000;">&#123;</span>
&nbsp;
        var handler <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Func<span style="color: #008000;">&lt;/</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;&lt;</span><span style="color: #FF0000;">int</span> , <span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">bool</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>ar.<span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// AsyncManager.Parameters[&quot;returned&quot;] now cantaining the result of </span>
		<span style="color: #008080; font-style: italic;">// method DoTestAsync after finish the process. The datatype is bool.</span>
        AsyncManager.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;returned&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> handler.<span style="color: #0000FF;">EndInvoke</span><span style="color: #000000;">&#40;</span>ar<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        AsyncManager.<span style="color: #0000FF;">OutstandingOperations</span>.<span style="color: #0000FF;">Decrement</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>, doTagHandler<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// This will process after the Async finished. The param of</span>
<span style="color: #008080; font-style: italic;">// this method is what we have used with AsyncManager.Parameters,</span>
<span style="color: #008080; font-style: italic;">// in this case, returned with type bool</span>
<span style="color: #0600FF;">public</span> ActionResult MethodCompleted<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> returned<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// You can actually make use of the param here to make some ViewData or something</span>
	returned <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>returned<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Since it's a ActionResult, i must return a View or something related.</span>
	<span style="color: #008080; font-style: italic;">// return View(); -&gt; will use the default view (Controller\Method.aspx)</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// i want to show some Content:</span>
	<span style="color: #0600FF;">return</span> Content<span style="color: #000000;">&#40;</span>returned.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Now we will write the method for our async controller </span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> DoTestAsync<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> a, <span style="color: #FF0000;">string</span> b<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Everything you need to do with the param in here, </span>
	<span style="color: #008080; font-style: italic;">// include query to your database or anything you can</span>
	<span style="color: #008080; font-style: italic;">// posibly think of. For me, i will just return something.</span>
&nbsp;
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>The result of this is:</p>
<pre>
"False"
</pre>
<p>Isn't it simple, what you need is just to follow this and there you go, you can now have a Web application that run twice or thrice as fast.</p>
<p>Good luck...</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2009%2F12%2F24%2Fbuiltin-async-controller-in-asp-net-mvc-2%2F&amp;linkname=Builtin%20Async%20Controller%20in%20ASP.net%20MVC%202"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2009/12/24/builtin-async-controller-in-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
