<?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>Andrey Shchekin &#187; Solutions</title>
	<atom:link href="http://blog.ashmind.com/category/solutions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ashmind.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 18:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Putting Web snapshots to practical use</title>
		<link>http://blog.ashmind.com/2008/09/28/putting-web-snapshots-to-practical-use/</link>
		<comments>http://blog.ashmind.com/2008/09/28/putting-web-snapshots-to-practical-use/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 10:50:04 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[Solutions]]></category>
		<category><![CDATA[Zoho]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2008/09/28/putting-web-snapshots-to-practical-use/</guid>
		<description><![CDATA[I was always interested if the service that, for example, WebSnapr provides is practically useful. Having snapshots of a linked pages looks very cool indeed, but does not really provide much information, at least for me. But recently I have found a nice use case for a similar service. For tables in my IoC Frameworks [...]]]></description>
			<content:encoded><![CDATA[<p>I was always interested if the service that, for example, <a href="http://www.websnapr.com/">WebSnapr</a> provides is practically useful. Having snapshots of a linked pages looks very cool indeed, but does not really provide much information, at least for me.</p>
<p>But recently I have found a nice use case for a similar service. For tables in my <a href="http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/">IoC Frameworks</a> <a href="http://blog.ashmind.com/index.php/2008/09/08/comparing-net-di-ioc-frameworks-part-2/">posts</a>, I was using parts of a <a href="http://sheet.zoho.com">Zoho</a> spreadsheet embedded through an iframe. However, Google Code wikis do not allow iframe embedding.</p>
<p>The <em>easy</em> solution was to use built-in wiki table markup, but in this case I would lose all color coding and ability to download the whole sheet as Excel. The <em>interesting</em> solution was to embed <em>snapshot</em> of an iframe, as an image. This would also allow me to solve the iframe problem for RSS readers that <a href="http://blog.ashmind.com/index.php/2008/08/20/google-reader-and-zoho-spreadsheets/">do not support them</a>.</p>
<p>My original idea was to use Firefox or Chrome (via <a href="http://code.google.com/chromium/">Chromium</a> project) to do the snapshots, but I have not managed to use the <a href="http://www.mono-project.com/WebBrowser">Mono.Mozilla</a> on Windows and I was too lazy to dive into Chromium.</p>
<p>IE was the simplest remaining choice, so using information from the <a href="http://www.eggheadcafe.com/tutorials/aspnet/b7cce396-e2b3-42d7-9571-cdc4eb38f3c1/build-a-selfcaching-asp.aspx">great article</a> of Peter Bromberg, I have built my own implementation of web snapshotting service. The main difference between my snapshots and other services is that my service can actually determine the correct size of the snapshot.</p>
<p>For example, this is a snapshot of my IoC frameworks table:</p>
<p><a style="display: inline-block; max-width: 100%; overflow: hidden" href="http://sheet.zoho.com/publishrange.do?id=a2b5ce1196a44db265f0466150348bc1"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://snapshots.ashmind.com/?url=http://sheet.zoho.com/publishrange.do?id=a2b5ce1196a44db265f0466150348bc1&amp;stub=this_is.png"></a></p>
<p>You can click it to see the source of the snapshot. As you can see, it has correct size (instead of being fixed to 1024&#215;768 or another <em>screen</em> size). On an unrelated note, it is an updated version of framework comparison, now including <a href="http://code.google.com/p/linfu/">LinFu</a> (which was updated to pass <strong>all</strong> feature tests).</p>
<p>In Google Code it looks like <a href="http://code.google.com/p/net-ioc-frameworks/wiki/Configuration">this</a>.</p>
<p>Additional interesting usage for this would be iframe-widget embedding to locations that do not support iframes. For example, it is possible to embed a list of RSS feed headers or twitter messages in Google Code using this technique.</p>
<p>You can svn-download source code from <a href="http://ashmind-web-ui.googlecode.com/svn/trunk/AshMind.Web.Snapshots">Google Code</a>. It is really bare for now, no good error-handling and no resizing support (all snapshots are always displayed in full size). However it may be a good starting point.</p>
<p>Looking at this project, I think that it would be very interesting to have a .NET wrapper for Chrome APIs that would allow anyone to automate Chrome, which probably will make snapshot extraction much faster and not COM-reliant.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2008/09/28/putting-web-snapshots-to-practical-use/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>DI Framework Challenges, 2: Lists</title>
		<link>http://blog.ashmind.com/2008/06/26/di-framework-challenges-2-lists/</link>
		<comments>http://blog.ashmind.com/2008/06/26/di-framework-challenges-2-lists/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 20:15:36 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2008/06/26/di-framework-challenges-2-lists/</guid>
		<description><![CDATA[There is another feature that I do not find in the dependency injection frameworks I&#8217;ve seen &#8212; list injection. For example, let&#8217;s suppose you have a kind of a notification service that sends messages using all possible delivery providers. It makes sense to have a list of these in the service: public class NotificationService { [...]]]></description>
			<content:encoded><![CDATA[<p>There is another feature that I do not find in the dependency injection frameworks I&#8217;ve seen &#8212; list injection.</p>
<p>For example, let&#8217;s suppose you have a kind of a notification service that sends messages using all possible delivery providers. It makes sense to have a list of these in the service:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> NotificationService {
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IDeliveryProvider[] providers;

    <span class="kwrd">public</span> NotificationService(IDeliveryProvider[] providers) {
        <span class="kwrd">this</span>.providers = providers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> SendAll(INotification[] notifications) {
        <span class="kwrd">var</span> deliveries = <span class="kwrd">from</span> notification <span class="kwrd">in</span> notifications
                         <span class="kwrd">from</span> provider <span class="kwrd">in</span> providers
                         <span class="kwrd">from</span> delivery <span class="kwrd">in</span> provider.GetDeliveries(notification)
                         <span class="kwrd">select</span> delivery;

        <span class="kwrd">this</span>.DeliverAll(deliveries);
    }
}</pre>
<p>Now the problem is that there is no out-of-the-box support for such things in Castle, which is somewhat unexpected.</p>
<p>Yes, I can set arrays <a href="http://www.castleproject.org/container/documentation/v1rc3/usersguide/arrayslistsanddicts.html#servicearrays">through configuration</a>. But I never use XML configuration unless there is an extremely important reason for this to be configurable at deployment time, which does not happen very often with IoC containers. Also, documented approach implies that I have to hardcode a list of specific services here, which makes it a change preventer.</p>
<p>So configuration-only solution is out of the question.</p>
<p>Fortunately, in contrast with <a href="http://blog.ashmind.com/index.php/2008/06/23/di-framework-challenges-1-simple-factories/">previous challenges</a>, it is possible to implement this myself. There is a minor problem &#8212; Castle feature for getting an array of services (kernel.ResolveServices) is generic-only in RC3. But if I have to choose between hackarounds and using nameless trunk version, I prefer trunk version.</p>
<p>There is a naive version of the resolver:</p>
<pre class="csharpcode"><span class="kwrd">internal</span> <span class="kwrd">class</span> ListSubDependencyResolver : ISubDependencyResolver {
    <span class="kwrd">private static readonly</span> HashSet&lt;Type&gt; SupportedInterfaces = <span class="kwrd">new</span> HashSet&lt;Type&gt; {
        <span class="kwrd">typeof</span>(IEnumerable&lt;&gt;), <span class="kwrd">typeof</span>(ICollection&lt;&gt;), <span class="kwrd">typeof</span>(IList&lt;&gt;)
    };

    <span class="kwrd">private readonly</span> IKernel kernel;

    <span class="kwrd">public</span> ListSubDependencyResolver(IKernel kernel) {
        <span class="kwrd">this</span>.kernel = kernel;
    }

    <span class="kwrd">public bool</span> CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency) {
        <span class="kwrd">if</span> (parentResolver.CanResolve(context, parentResolver, model, dependency))
            <span class="kwrd">return true</span>;

        <span class="kwrd">var</span> targetType = dependency.TargetType;
        <span class="kwrd">if</span> (targetType.IsArray)
            <span class="kwrd">return true</span>;</pre>
<pre class="csharpcode">        <span class="kwrd">return</span> targetType.IsInterface
            &amp;&amp; targetType.IsGenericType
            &amp;&amp; SupportedInterfaces.Contains(targetType.GetGenericTypeDefinition());</pre>
<pre class="csharpcode">    }

    <span class="kwrd">public object</span> Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency) {
        <span class="kwrd">if</span> (parentResolver.CanResolve(context, parentResolver, model, dependency))
            <span class="kwrd">return</span> parentResolver.Resolve(context, parentResolver, model, dependency);

        <span class="kwrd">var</span> type = (
            from @interface <span class="kwrd">in</span> dependency.TargetType.GetInterfaces()
            <span class="kwrd">where</span> @interface.IsGenericType
               &amp;&amp; @interface.GetGenericTypeDefinition() == <span class="kwrd">typeof</span>(IEnumerable&lt;&gt;)
            <span class="kwrd">select</span> @interface.GetGenericArguments().First()
        ).Single();

        <span class="kwrd">return this</span>.kernel.ResolveAll(type, new Hashtable());
    }
}</pre>
<blockquote>
<p><strong>Side note</strong>: here we can see two minor problems with API design &#8212; first one is the mysterious parentResolver (what should I pass as parentResolver <em>to</em> parentResolver?), second one is that shiny new ResolveAll has no overload for the case when I do not need additionalArguments.</p>
</blockquote>
<p>This is a very naive implementation (no checks if I can actually resolve generic parameter), but it works quite well with the original case (as well as with the other IList/ICollection/IEnumerable requirements).</p>
<p>However, there is a logical consistency problem with Singletons &#8212; they will get different parameters depending on whether you have resolved them before registering all their dependencies or after. If a Singleton has such dependencies, it can not be Startable, unless you make sure to register all dependencies before it.</p>
<p>I have found almost no information on using lists with DI, so I am not sure how others solve this. But I do not need Startable and all my components are registered at almost same time, so this is not a problem for me for now.</p>
<blockquote>
<p><strong>Update: </strong>Thanks to Victor Kornov, I have found a <a href="http://hammett.castleproject.org/?p=257">this post</a> by Castle developer, which describes the same solution. I have added <a href="http://groups.google.com/group/castle-project-devel/browse_thread/thread/645e871f7bba4206">a proposition</a> to the corresponding Google Group to make it a built-in feature. I prefer it being enabled by default, but I would like to see it either way.</p>
</blockquote>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2008/06/26/di-framework-challenges-2-lists/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Mocking internal interfaces with Moq</title>
		<link>http://blog.ashmind.com/2008/05/09/mocking-internal-interfaces-with-moq/</link>
		<comments>http://blog.ashmind.com/2008/05/09/mocking-internal-interfaces-with-moq/#comments</comments>
		<pubDate>Fri, 09 May 2008 08:57:00 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2008/05/09/mocking-internal-interfaces-with-moq/</guid>
		<description><![CDATA[Is a same method you would use with NMock2, but with &#8220;DynamicProxyGenAssembly2&#8243; in InternalsVisibleTo. This strange name is a default dynamic assembly name used by Castle.DynamicProxy. Do not forget that the attribute should be added to the assembly with internal types, not to the tests assembly (obvious, but I got it wrong the first time). [...]]]></description>
			<content:encoded><![CDATA[<p>Is a <a href="http://blog.ashmind.com/index.php/2007/07/07/mocking-internal-interfaces-with-nmock2/">same method</a> you would use with NMock2, but with &#8220;DynamicProxyGenAssembly2&#8243; in InternalsVisibleTo.<br /> This strange name is a default dynamic assembly name used by Castle.DynamicProxy.</p>
<p>Do not forget that the attribute should be added to the assembly with internal types, not to the tests assembly (obvious, but I got it wrong the first time).</p>
<blockquote><p><strong>Update:</strong> <a href="http://groups.google.com/groups/profile?hl=en&#038;enc_user=MrWd7RcAAABFAu4WJ_yZyMt4VseX6ryvHqZiDvCVswhrZ6TQxKj0ww">yesthatmcgurk</a> has <a href="http://groups.google.com/group/moqdisc/browse_thread/thread/ebe69d235dfa69d9?hl=en">pointed me</a> to the fact that for strongly named assemblies you have to specify the public key as well. So the correct attribute for the strongly named assembly should be</p>
<pre class="csharpcode">[<span class="kwrd">assembly</span>:InternalsVisibleTo(<span class="str">"DynamicProxyGenAssembly2,
PublicKey=002400000480000094000000060200000024000052534
1310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b
3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d926665
4753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb
4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c486
1eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7"</span>)]
<span class="rem">// without line-breaks</span></pre>
</blockquote>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2008/05/09/mocking-internal-interfaces-with-moq/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Metaprogramming in JavaScript: Auto properties for ASP.NET AJAX</title>
		<link>http://blog.ashmind.com/2007/10/08/metaprogramming-in-javascript-auto-properties-for-aspnet-ajax/</link>
		<comments>http://blog.ashmind.com/2007/10/08/metaprogramming-in-javascript-auto-properties-for-aspnet-ajax/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 19:20:05 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[ASP.NET AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Metaprogramming]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/10/08/metaprogramming-in-javascript-auto-properties-for-aspnet-ajax/</guid>
		<description><![CDATA[.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Sometimes, large company is no better than several independent companies.<br /> I always get this thought when looking at ASP.NET AJAX property accessors.</p>
<p>For people not familiar with the matter:<br /> Microsoft ASP.NET AJAX requires developer to use property accessors to encapsulate javascript fields.<br /> This would be a great idea, if only Microsoft IE supported javascript <a href="http://ejohn.org/blog/javascript-getters-and-setters/">getters and setters</a>.<br /> Mozilla supports them, and Opera will in 9.50, but IE does not (they are not standard).<br /> Also, IE does not have any hacky way to achieve the same effect (except in VBScript, which seems to be limited).</p>
<p>So in ASP.NET AJAX each property requires two functions named get<kbd>_propertyName</kbd> and set<kbd>_propertyName</kbd>. </p>
<p>Fortunately, javascript is very powerful in class member manipulation.<br /> So I do not have to write things like this:</p>
<pre class="csharpcode">MyControl.prototype = {
    get_text : <span class="kwrd">function</span>() {
        <span class="kwrd">return</span> <span class="kwrd">this</span>._text;
    },

    set_text : <span class="kwrd">function</span>(value) {
        <span class="kwrd">this</span>._text = value;
    }
}</pre>
<p>And I do not have to wait for auto properties as it is the case for C#.</p>
<p>Instead I just made an helper object named Auto and do things this way:</p>
<pre class="csharpcode">
MyControl.prototype = {…}
Auto.properties(MyControl.prototype, [
    <span class="str">'text'</span>,
    <span class="str">'value'</span>,
    …
]);</pre>
<p>Code for this helper can be very simple.<br />
(in actual project I have additional complexity like Auto support for INotifyPropertyChanged).</p>
<pre class="csharpcode">
<span class="kwrd">var</span> Auto = {
    property : <span class="kwrd">function</span>(prototype, name) {
        <span class="kwrd">var</span> getter = <span class="kwrd">function</span>() { <span class="kwrd">return</span> <span class="kwrd">this</span>[<span class="str">'_'</span> + name]; };
        prototype[<span class="str">'get_'</span> + name] = prototype[<span class="str">'get_'</span> + name] || getter;

        <span class="kwrd">var</span> setter = <span class="kwrd">function</span>(value) { <span class="kwrd">this</span>[<span class="str">'_'</span> + name] = value; };
        prototype[<span class="str">'set_'</span> + name] = prototype[<span class="str">'set_'</span> + name] || setter;
    },

    properties : <span class="kwrd">function</span>(prototype, names) {
        names.forEach(<span class="kwrd">function</span>(name) {
            Auto.property(prototype, name);
        });
    }
}</pre>
<p>This also requires forEach method on Array, but that one is pretty obvious.</p>
<p>I like to show this code when I hear about Javascript being assembly language and whatever-to-javascript compilation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/10/08/metaprogramming-in-javascript-auto-properties-for-aspnet-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CSS container class trick</title>
		<link>http://blog.ashmind.com/2007/10/08/css-container-class-trick/</link>
		<comments>http://blog.ashmind.com/2007/10/08/css-container-class-trick/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 17:48:21 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/10/08/css-container-class-trick/</guid>
		<description><![CDATA[.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: Consolas, "Courier New", Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Recently I had a debugging session with one of my colleagues and I remebered a simple trick I use often.<br /> Imagine that you have a list of items, and you want to simultaneously change them in response to a user action.</p>
<p>For example, what if you have this list:</p>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">ul</span> <span class="attr">class</span><span class="kwrd">="Contacts"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">li</span><span class="kwrd">&gt;</span>Andrey <span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;</span>Shchekin<span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;&lt;/</span><span class="html">li</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">li</span><span class="kwrd">&gt;</span>Nina <span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;</span>Philippova<span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;&lt;/</span><span class="html">li</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">li</span><span class="kwrd">&gt;</span>Gordon <span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;</span>Freeman<span class="kwrd">&lt;</span><span class="html">span</span><span class="kwrd">&gt;&lt;/</span><span class="html">li</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">ul</span><span class="kwrd">&gt;</span></pre>
</p>
<p>And you want to show/hide (expand/collapse) all spans in javascript.</p>
<p>One way to do this would be to go through all spans and change their className.<br />
But that can be quite slow on large list.</p>
<p>The better way is to use something like addClassName (from prototype.js) on <code class="csharpcode"><span class="kwrd">&lt;</span><span class="html">ul</span><span class="kwrd">&gt;</span></code>.</p>
<p>So you change it to <code class="csharpcode"><span class="kwrd">&lt;</span><span class="html">ul</span> <span class="attr">class</span><span class="kwrd">="Contacts AllCollapsed"</span><span class="kwrd">&gt;</span></code> and then apply styles to <code class="csharpcode">.AllCollapsed li span</code>.</p>
<p>This way the browser is responsible for finding all contained elements, which should be faster.<br />
Also, if elements are added dynamically, you do not have to process them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/10/08/css-container-class-trick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evaluating Javascript in WatiN</title>
		<link>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/</link>
		<comments>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 19:52:03 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Solutions]]></category>
		<category><![CDATA[WatiN]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/09/05/evaluating-javascript-in-watin/</guid>
		<description><![CDATA[The WatiN framework is quite cool, but it lacks two important things.First one is searching by CSS selectors, or, at least, classes.Find.ByCustom(&#8220;className&#8221;, &#8220;X&#8221;) is way too ugly. Or am I missing something? The second (more important) one is a weak access to Javascript.First thing I wanted to do with WatiN was to change something and [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://watin.sourceforge.net/">WatiN</a> framework is quite cool, but it lacks two important things.<br />First one is searching by CSS selectors, or, at least, classes.<br />Find.ByCustom(&#8220;className&#8221;, &#8220;X&#8221;) is way too ugly. Or am I missing something? </p>
<p>The second (more important) one is a weak access to Javascript.<br />First thing I wanted to do with WatiN was to change something and then check some script state.<br />And getting some values from script was not obvious.</p>
<p>I didn&#8217;t want to use <a href="http://www.ayende.com/Blog/archive/2007/04/03/Eval-with-WatiN.aspx">Ayende&#8217;s evil hack</a> (no harm intentended, it gets the work done) — putting javascript state into DOM is not pretty and too string oriented.<br />I thought that browser COM interfaces should definitely have a way to get Javascript <em>objects</em> outside, that is just the way MS/COM people think.<br />Thanks to <a href="http://www.ayende.com/Blog/archive/2007/04/03/Eval-with-WatiN.aspx#Comments_ascx_CommentList_ctl04_NameLink">Jeff Brown&#8217;s comment</a> for explaining last obstacles.</p>
<p>So here goes the code.<br />It is quite basic, but it allows you to get value of any Javascript evaluation.<br />As you can see, I hadn&#8217;t included any error handling, I had no time to look into it. </p>
<pre class="csharpcode">    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> JS {
        <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">object</span> Eval(Document document, <span class="kwrd">string</span> code) {
            IExpando window = JS.GetWindow(document);
            PropertyInfo property = JS.GetOrCreateProperty(window, <span class="str">"__lastEvalResult"</span>);

            document.RunScript(<span class="str">"window.__lastEvalResult = "</span> + code + <span class="str">";"</span>);

            <span class="kwrd">return</span> property.GetValue(window, <span class="kwrd">null</span>);
        }

        <span class="kwrd">private</span> <span class="kwrd">static</span> PropertyInfo GetOrCreateProperty(IExpando expando, <span class="kwrd">string</span> name) {
            PropertyInfo property = expando.GetProperty(name, BindingFlags.Instance);
            <span class="kwrd">if</span> (property == <span class="kwrd">null</span>)
                property = expando.AddProperty(name);

            <span class="kwrd">return</span> property;
        }

        <span class="kwrd">private</span> <span class="kwrd">static</span> IExpando GetWindow(Document document) {
            <span class="kwrd">return</span> document.HtmlDocument.parentWindow <span class="kwrd">as</span> IExpando;
        }
    }</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p style="font-size: 90%">Nitpicking:<br />
By the way, <a href="http://www.ayende.com/Blog">Ayende</a>, getting permalinks to comments in your blog is not obvious (I used View Source).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Mocking internal interfaces with NMock2</title>
		<link>http://blog.ashmind.com/2007/07/07/mocking-internal-interfaces-with-nmock2/</link>
		<comments>http://blog.ashmind.com/2007/07/07/mocking-internal-interfaces-with-nmock2/#comments</comments>
		<pubDate>Sat, 07 Jul 2007 19:05:21 +0000</pubDate>
		<dc:creator>Andrey Shchekin</dc:creator>
				<category><![CDATA[Mocking]]></category>
		<category><![CDATA[NMock2]]></category>
		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://blog.ashmind.com/index.php/2007/07/07/mocking-internal-interfaces-with-nmock2/</guid>
		<description><![CDATA[Yesterday I stumbled into a question of how to mock an internal interface. The interface is visible in the test assembly due to InternalVisibleTo attribute, but Mockery was unable to create a mock of it. The very first idea was additional InternalVisibleTo, but I had to find out what assembly name to use. Since dynamic [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I stumbled into a question of how to mock an internal interface.</p>
<p>The interface is visible in the test assembly due to InternalVisibleTo attribute, but Mockery was unable to create a mock of it.<br />
The very first idea was additional InternalVisibleTo, but I had to find out what assembly name to use.</p>
<p>Since dynamic mocks are actual classes created on-demand for the specified interface, they do not belong to NMock2 assembly.<br />
But they have their own dynamic assembly with consistent name &#8212; in case of NMock2 it is &#8220;Mocks&#8221; (I got it from Reflector).<br />
So I used InternalVisibleTo(&#8220;Mocks&#8221;) and it actually worked.</p>
<p>I am not sure if it would work with strong names, but, fortunately, I do not need to strong name this project.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ashmind.com/2007/07/07/mocking-internal-interfaces-with-nmock2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

