<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-1390798273045590911</atom:id><lastBuildDate>Thu, 05 Sep 2024 18:57:29 +0000</lastBuildDate><category>multithreading</category><category>Service locator</category><category>dynamic proxy</category><category>reflection</category><category>remoting</category><title>Reshef&#39;s tip of the day</title><description>.net development tips</description><link>http://reshefstipoftheday.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-7345262674838216226</guid><pubDate>Wed, 18 Jun 2008 12:54:00 +0000</pubDate><atom:updated>2008-06-18T15:54:04.849+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">multithreading</category><category domain="http://www.blogger.com/atom/ns#">Service locator</category><title>Lock-Free Service Locator</title><description>&lt;p&gt;Usually, when developing small application there is no real justification for using a full-fledged IoC container for managing services. &lt;/p&gt;  &lt;p&gt;However, cross-cutting , single instance services are something common that still has to managed. This leaves us with the following options for accessing services:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Implement the service as singleton.&lt;/li&gt;    &lt;li&gt;Move the service reference between objects.&lt;/li&gt;    &lt;li&gt;Use a service locator.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I don&#39;t like the first option as it introduces global variables to the application and the second option is tedious very hard to manage.&lt;/p&gt;  &lt;p&gt;This leaves us with the service locator which in general should be the only singleton(static) in the application and provide access to the services according to their interface.&lt;/p&gt;  &lt;p&gt;The service locator usually looks like that:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;ServiceLocator&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;private static readonly &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;IDictionary&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;gt; services = &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Dictionary&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Type&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;private static readonly object &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;locker = &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;new object&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;();&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static void &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;SetService&amp;lt;T&amp;gt;(T instance) &lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;where &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T : &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;class&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;if &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(instance == &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;)&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;throw new &lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;ArgumentNullException&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #a31515&quot;&gt;&amp;quot;instance&amp;quot;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;);&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;lock &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(locker)&lt;br /&gt;        {&lt;br /&gt;            services.Add(&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(T), instance);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T GetService&amp;lt;T&amp;gt;() &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;where &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T : &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;class&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;object &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;service;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;lock &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(locker)&lt;br /&gt;        {&lt;br /&gt;            services.&lt;br /&gt;                TryGetValue(&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(T), &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;out &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;service);&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;service &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;as &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It hold a dictionary of services. The key is the type and the value is the instance. It is quite simple, however I do not like the fact that it has to lock the services dictionary on each access. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When trying to eliminate the need for locking, I remembered &lt;a href=&quot;http://www.rgoarchitects.com/nblog/2008/03/31/SingletonInNET.aspx&quot;&gt;this&lt;/a&gt; post by &lt;a href=&quot;http://www.rgoarchitects.com/nblog/default.aspx&quot;&gt;Arnon&lt;/a&gt; and it inspired my to try this approach:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static class &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;ServiceLocator&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;private class &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Resolver&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;lt;T&amp;gt; &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;where &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T:&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;class&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;private static &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T theInstance;&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static void &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;Create(T instance)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;if &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(instance == &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;)&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;throw new&lt;br /&gt;                    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;ArgumentNullException&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #a31515&quot;&gt;&amp;quot;instance&amp;quot;&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Interlocked&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;.&lt;br /&gt;                Exchange(&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;ref &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;theInstance, instance);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T Instance&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;get&lt;br /&gt;            &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;                &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;theInstance;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static void &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;SetService&amp;lt;T&amp;gt;(T instance)&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;where &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T:&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;class&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Resolver&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;lt;T&amp;gt;.Create(instance);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;public static &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T GetService&amp;lt;T&amp;gt;() &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;where &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;T:&lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;class&lt;br /&gt;    &lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;{&lt;br /&gt;        &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: blue&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;background: #eeeeee; color: #2b91af&quot;&gt;Resolver&lt;/span&gt;&lt;span style=&quot;background: #eeeeee&quot;&gt;&amp;lt;T&amp;gt;.Instance;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Pay attention to the Resolver private class. For every type T that is specified as a generic parameter, a new type, Resolver&amp;lt;SomeType&amp;gt; will be created and the static field, theInstance, will be set to the service instance.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;What did we achieve here? &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the previous implementation we had to lock the dictionary on every read or write. In the new implementation there is no need to lock for read and the lock for writing was changed to Interlocked which has better performance so we have better concurrency.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;No dictionary. Although it is negligible there is no need to search a dictionary. Besides, I always felt that something like service resolution should be something which is related to the infrastructure and should not be managed by a dictionary and I think this implementation better achieves it.&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2008/06/lock-free-service-locator.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-6812318877934195417</guid><pubDate>Sun, 23 Mar 2008 08:37:00 +0000</pubDate><atom:updated>2008-03-23T10:37:41.579+02:00</atom:updated><title>Code semantics</title><description>&lt;p&gt;As I started a new job I was introduced to a new codebase. This happens to any developer who is starting a new job or a new role.&lt;/p&gt;  &lt;p&gt;Since people are different, their approaches towards code is different as well, and were one developer gives his data-access interfaces and objects the suffix &#39;DAO&#39;, another developer will suffix it with &#39;Repository&#39;, hence, we might get &#39;ICoustomerDAO&#39; or &#39;ICustomerRepository&#39;. &lt;/p&gt;  &lt;p&gt;The meaning is the same - a service that is charge of storing and retrieving information about a customer.&lt;/p&gt;  &lt;p&gt;As I mentioned at the beginning, I was recently introduced to a new codebase that has its own semantics. Some of these semantics were trivial like using the convention of &#39;Sink&#39;. I immediately understood that a sink is a recipient of data and it is the end of the road for this data. Examples are ConsoleSink which writes to the console and DBSink which writes to the database.&lt;/p&gt;  &lt;p&gt;This however, is a simple example as a system is more complex than that. There are other &amp;quot;hidden&amp;quot; semantics that are just the result of differences between people and the way they things.&lt;/p&gt;  &lt;p&gt;The example I used with the DAO/Repository in taken from the semantics of Domain-Driven Development (DDD) which gives very high importance to the way things are expressed. The base for DDD is first of all defining an ubiquitous language that is used to define the entities in the system and the relations between them.&lt;/p&gt;  &lt;p&gt;I believe that if a software project will have a defined semantics, the amount of the mess that is usually a part of a codebase will dramatically be reduced, it will be easier to get familiar with the code and maintenance will be easier.&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2008/03/code-semantics.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-4062693455540942101</guid><pubDate>Sat, 22 Mar 2008 08:40:00 +0000</pubDate><atom:updated>2008-03-23T10:40:43.388+02:00</atom:updated><title>Blog returned here</title><description>&lt;p&gt;However, there are posts that do not exist here but u can find on: &lt;a href=&quot;http://jajahdevblog.com/reshef&quot;&gt;http://jajahdevblog.com/reshef&lt;/a&gt;&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2008/03/blog-returned-here.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-9218530020544247541</guid><pubDate>Sun, 25 Nov 2007 15:49:00 +0000</pubDate><atom:updated>2007-11-25T17:49:39.875+02:00</atom:updated><title>Blog has moved</title><description>&lt;p&gt;&lt;a href=&quot;http://jajahdevblog.com/reshef&quot;&gt;http://jajahdevblog.com/reshef&lt;/a&gt;&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2007/11/blog-has-moved.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-4684678759691813281</guid><pubDate>Thu, 22 Nov 2007 12:14:00 +0000</pubDate><atom:updated>2007-11-25T17:32:08.764+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">dynamic proxy</category><category domain="http://www.blogger.com/atom/ns#">reflection</category><title>Tip of the day #2: Dynamic proxies</title><description>&lt;p&gt;A dynamic proxy is an approach that is not fully natively supported in .net but natively exists in java.&lt;/p&gt; &lt;p&gt;What a proxy means, is to wrap an object with with wrapper that intercepts call to that object and does something in addition.&lt;/p&gt; &lt;p&gt;What is it good for? A naive example is to add a proxy that log calls to methods of an object.&lt;/p&gt; &lt;p&gt;A more real world example is to use proxy in an &lt;a href=&quot;http://en.wikipedia.org/wiki/Object-relational_mapping&quot; target=&quot;_blank&quot;&gt;O/R mapper&lt;/a&gt;. The mapper can create proxies on top of the domain object and intercept calls to properties in order to fetch data from the db and track changes to the data that will later be persisted (&lt;a href=&quot;http://en.wikipedia.org/wiki/Nhibernate&quot; target=&quot;_blank&quot;&gt;NHibernate&lt;/a&gt; works this way).&lt;/p&gt; &lt;p&gt;The idea of dynamic proxy is to provide an implementation of an interceptor and attach it to the proxy. From now on, all calls to methods will pay through the interceptor which, for the logging example, logs every call to the method. Concrete examples can be found in the link at the bottom of this post.&lt;/p&gt; &lt;p&gt;The .net native implementation for dynamic proxies is by using &lt;a href=&quot;http://msdn2.microsoft.com/en-US/library/system.contextboundobject.aspx&quot; target=&quot;_blank&quot;&gt;ContextBoundObject&lt;/a&gt; or &lt;a href=&quot;http://msdn2.microsoft.com/en-US/library/system.marshalbyrefobject.aspx&quot; target=&quot;_blank&quot;&gt;MarsalByRefObject&lt;/a&gt;. This approach is limited since you have to inherit from one of these classes which is not clean.&lt;/p&gt; &lt;p&gt;There are several implementations of dynamic proxies that do not interfere with inheritance hierarchy of your code. &lt;br&gt;Here are some examples:&lt;/p&gt; &lt;p&gt;1) &lt;a href=&quot;http://www.codeproject.com/csharp/hamiltondynamicproxy.asp&quot; target=&quot;_blank&quot;&gt;Castle DynamicProxy&lt;/a&gt;&lt;/p&gt; &lt;p&gt;2) &lt;a href=&quot;http://www.codeproject.com/dotnet/dynamicproxy.asp&quot; target=&quot;_blank&quot;&gt;DynamicProxy.NET&lt;/a&gt;&lt;/p&gt; &lt;p&gt;3) &lt;a href=&quot;http://www.codeproject.com/cs/library/LinFuPart1.asp&quot; target=&quot;_blank&quot;&gt;LinFu&lt;/a&gt;&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-2-dynamic-proxies.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-1860914615075579799</guid><pubDate>Thu, 22 Nov 2007 10:14:00 +0000</pubDate><atom:updated>2007-11-22T12:17:57.829+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">multithreading</category><category domain="http://www.blogger.com/atom/ns#">remoting</category><title>Tip of the day: Mutual exclusion when Remoting</title><description>&lt;p&gt;Another tip from Gal:&lt;strong&gt;&quot;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Just a small tip that might have saved me a few good hours:  &lt;p&gt;A Mutex can be released only by the thread that originally locked it.  &lt;p&gt;A semaphore can be released by any thread.  &lt;p&gt;I used a mutex at the beginning, while locking and releasing on different threads, and at the threadpool desire, got (&quot;sometimes&quot; is the WORST one) an  &lt;p&gt;&lt;strong&gt;ApplicationException - Object synchronization method was called from an unsynchronized block of code&lt;/strong&gt;.  &lt;p&gt;Usually this means that the mutex is not locked but still tries to be released – check &lt;a href=&quot;http://bbellomo.blogspot.com/2007/03/object-synchronization-method-was.html&quot; target=&quot;_blank&quot;&gt;this&lt;/a&gt; out.  &lt;p&gt;But in my case (&lt;em&gt;first remoting call locks an object and the second releases&lt;/em&gt;), it was just that a mutex was the wrong sync method, and a semaphore quickly solved the problem.  &lt;p&gt;As mentioned in the link, the error message need some work.&lt;strong&gt;&quot;&lt;/strong&gt;&lt;/p&gt;  </description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-mutual-exclusion-when.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-180352560150729757</guid><pubDate>Wed, 21 Nov 2007 12:10:00 +0000</pubDate><atom:updated>2007-11-21T14:34:45.595+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">multithreading</category><title>Tip of the day: Interlocked</title><description>&lt;p&gt;The Interlocked class in .net is a static class that provides type operation that are performed atomically.&lt;/p&gt; &lt;p&gt;By providing this, it can help solve simple multithreading scenarios since:&lt;/p&gt; &lt;p&gt;1) It removes the chance of deadlocks that can be caused by using the &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx&quot; target=&quot;_blank&quot;&gt;lock&lt;/a&gt; keyword.&lt;/p&gt; &lt;p&gt;2) It has better &lt;a href=&quot;http://www.ftponline.com/reports/vslivesf/2004/shaw/&quot; target=&quot;_blank&quot;&gt;performance&lt;/a&gt; than the lock keyword.&lt;/p&gt; &lt;p&gt;3) Simpler (and less) syntax.&lt;/p&gt; &lt;p&gt;Lets take a look at a scenario where&amp;nbsp;you would like to count how many times a method is called while a program runs (multithreaded environment):&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;int&lt;/span&gt; methodCallsCounter = 0;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;void&lt;/span&gt; SomeMethod()&lt;br /&gt;{&lt;br /&gt;    System.Threading.Interlocked.&lt;br /&gt;        Increment(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;ref&lt;/span&gt; methodCallsCounter);&lt;br /&gt;    ...&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;The Interlocked class has a set of methods to manipulate data. A complete documentation and a full example can be found &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.threading.interlocked.aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;Talking about multithreading and locks, I suggest reading &lt;/font&gt;&lt;/strong&gt;&lt;a href=&quot;http://www.interact-sw.co.uk/iangblog/2004/03/23/locking&quot; target=&quot;_blank&quot;&gt;&lt;strong&gt;&lt;font color=&quot;#0000ff&quot;&gt;this&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;font color=&quot;#ff0000&quot;&gt;. It describes very important points about locking.&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-interlocked.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-297182301823203274</guid><pubDate>Sun, 18 Nov 2007 08:13:00 +0000</pubDate><atom:updated>2007-11-18T10:14:06.848+02:00</atom:updated><title>Tip of the day: Friend assemblies</title><description>&lt;p&gt;The &lt;strong&gt;internal&lt;/strong&gt; keyword (C#) is defined as the way to restrict access to members&amp;nbsp;only to types in same assembly.&lt;/p&gt; &lt;p&gt;But, since the .net framework 2.0, there is a way to to define an assembly as a friend assembly. This can be done by specifying&lt;/p&gt;&lt;pre&gt;&lt;a&gt;[assembly:InternalsVisibleTo(&quot;&lt;em&gt;friend assembly&lt;/em&gt;&quot;)]&lt;/a&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;in the AssemblyInfo.cs file (or in any other file).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;After specifying this, the friend assembly can access the internals of the assembly&amp;nbsp;marked with this attribute.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;I can see two&amp;nbsp;useful thing&amp;nbsp;that can be achieved:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;1) Better encapsulation of course.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;2) Enable access for unit testing by using this strategy.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The msdn documentation is &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/0tke9fxk(VS.80).aspx&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-friend-assemblies.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-7068937430234753090</guid><pubDate>Thu, 15 Nov 2007 16:35:00 +0000</pubDate><atom:updated>2007-11-15T18:35:26.096+02:00</atom:updated><title>Tip of the day #2: ref/out key words for reference types</title><description>&lt;p&gt;This tip is the courtesy of Gal.&lt;/p&gt; &lt;p&gt;It surprised&amp;nbsp;and embarrassed me&amp;nbsp;to realize that I didn&#39;t know it.&lt;/p&gt; &lt;p&gt;When a reference type is passed to a method &lt;strong&gt;without&lt;/strong&gt; using the ref or out keywords, a new reference is created inside the method and it points to the same location in the memory as the reference that was passed so we have two &quot;pointers&quot; to the same location in memory.&lt;/p&gt; &lt;p&gt;In case the the reference &lt;strong&gt;inside&lt;/strong&gt; the method is set to a different location, the reference that was passed&amp;nbsp;&lt;strong&gt;does not&lt;/strong&gt; change.&lt;/p&gt; &lt;p&gt;When the ref/out keywords are used, we have only &lt;strong&gt;one&lt;/strong&gt; reference inside and outside the method so if the reference is changed to point to another location and it happens &lt;strong&gt;inside&lt;/strong&gt; the method, the outer reference is changed too.&lt;/p&gt; &lt;p&gt;Basic stuff...&lt;/p&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-2-refout-key-words-for.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-6568356289962460614</guid><pubDate>Thu, 15 Nov 2007 13:04:00 +0000</pubDate><atom:updated>2007-11-21T14:39:18.638+02:00</atom:updated><title>Tip of the day: ThreadStatic attribute</title><description>&lt;p&gt;According to the msdn documentation, the ThreadStatic attribute &quot;Indicates that the value of a static field is unique for each thread.&quot;&lt;/p&gt; &lt;p&gt;A practical use for it can be a &lt;strong&gt;registrar&lt;/strong&gt; implementation. A registrar can be used as a static entry point to store context data for say, a request that is being processed by several classes that need to share data.&lt;/p&gt; &lt;p&gt;Implementation example:&lt;/p&gt;&lt;pre class=&quot;code&quot;&gt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Registrar&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;   [&lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;ThreadStatic&lt;/span&gt;]&lt;br /&gt;   &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;object&lt;/span&gt;&amp;gt;&lt;br /&gt;      m_registrar = &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;object&lt;/span&gt;&amp;gt; &lt;br /&gt;      LocalDictionary&lt;br /&gt;   {&lt;br /&gt;      &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;get&lt;br /&gt;&lt;/span&gt;      {&lt;br /&gt;         &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;if&lt;/span&gt; (m_registrar == &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;         {&lt;br /&gt;            m_registrar =&lt;br /&gt;               &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: rgb(43,145,175)&quot;&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;string&lt;/span&gt;, &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;object&lt;/span&gt;&amp;gt;();&lt;br /&gt;         }&lt;br /&gt;         &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; m_registrar;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;object&lt;/span&gt; Get(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;string&lt;/span&gt; key)&lt;br /&gt;   {&lt;br /&gt;      &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;return&lt;/span&gt; LocalDictionary[key];&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;void&lt;/span&gt; Set(&lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;string&lt;/span&gt; key, &lt;span style=&quot;color: rgb(0,0,255)&quot;&gt;object&lt;/span&gt; value)&lt;br /&gt;   {&lt;br /&gt;      LocalDictionary[key] = value;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;a href=&quot;http://11011.net/software/vspaste&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;Now since the m_registrar is marked with ThreadStatic attribute, is &lt;strong&gt;static per thread&lt;/strong&gt;, which means that no locking is required and no data corruption can occur.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;A real world example for using such strategy is HttpContext.Items&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;strong style=&quot;color: red&quot;&gt;Gilad pointed out two important points:&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;- ASP.NET is changing threads under the hood so don&#39;t use it there since u will most probably get your data corrupted. The HttpContext knows how to handle that so use it.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;- When using a thread pool, the threads are not killed and so when a thread completes execution and returned to the pool, the static data is still kept. In this case u should use an execution wrapper than clears the per thread static data after the thread complete execution.&lt;/p&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-threadstatic-attribute.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-1206865110623969033</guid><pubDate>Wed, 14 Nov 2007 18:05:00 +0000</pubDate><atom:updated>2007-11-14T20:05:40.684+02:00</atom:updated><title>Tip of the day: Data Type aliasing</title><description>&lt;p&gt;There is a feature in .net that is not commonly remembered and it is Data Type aliasing.&lt;/p&gt; &lt;p&gt;&lt;br&gt;What it means is that u give a data type a different name.&lt;/p&gt; &lt;p&gt;&lt;br&gt;I can see two uses for it:&lt;br&gt;&lt;strong&gt;1) Enhance code readability:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;br&gt;Instead of having very long type declarations such as when using generic types inside generic types, u can assign an alias to that declaration. Here is an example:&lt;br&gt;&lt;/p&gt;&lt;pre&gt;    &lt;br /&gt;    // Aliasing&lt;br /&gt;    using HashOfHashes = &lt;br /&gt;	Dictionary&amp;lt;string, Dictionary&amp;lt;string, List&amp;lt;string&amp;gt;&amp;gt;&amp;gt;;&lt;br /&gt; &lt;br /&gt;    public class AliasExample&lt;br /&gt;    {&lt;br /&gt;        HashOfHashes m_hash;&lt;br /&gt; &lt;br /&gt;        public AliasExample()&lt;br /&gt;        {&lt;br /&gt;            m_hash = new HashOfHashes();&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        public HashOfHashes MyHash&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return m_hash;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;2) Alias data types that are prone to change:&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br&gt;An example is an identifier property of class. It can save a lot find&amp;amp;replace when changing code like&amp;nbsp;changing the identifier of a member from int to Guid and it better shows intent. For example:&lt;br&gt;&lt;/p&gt;&lt;pre&gt;    // Aliasing&lt;br /&gt;    using MemberIdentidfier = Guid;&lt;br /&gt; &lt;br /&gt;    public class MemberWithAlias&lt;br /&gt;    {&lt;br /&gt;        private MemberIdentidfier m_id;&lt;br /&gt;        private string m_name;&lt;br /&gt; &lt;br /&gt;        public MemberWithAlias&lt;br /&gt;		(MemberIdentidfier id)&lt;br /&gt;        {&lt;br /&gt;            m_id = id;&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        public MemberIdentidfier Id&lt;br /&gt;        {&lt;br /&gt;            get { return m_id; }&lt;br /&gt;        }&lt;br /&gt; &lt;br /&gt;        public string Name&lt;br /&gt;        {&lt;br /&gt;            get { return m_name; }&lt;br /&gt;            set { m_name = value; }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-data-type-aliasing.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1390798273045590911.post-3898183338079614559</guid><pubDate>Wed, 14 Nov 2007 17:33:00 +0000</pubDate><atom:updated>2007-11-14T19:33:56.984+02:00</atom:updated><title>Tip of the day: Primitive but efficient way for profiling (on development machine)</title><description>&lt;p&gt;Small thing I read about. It is so simple and obvious but I never thought about it... &lt;p&gt;In&amp;nbsp;the lack of a decent profiler,  &lt;p&gt;if u observe&amp;nbsp;that a certain process takes a large amount of time (such as system startup), &lt;p&gt;start the debugger and while the time consuming task is running, click the pause button in VS. &lt;p&gt;Now take a look at the call stack to see which method take the time to execute.&lt;/p&gt;</description><link>http://reshefstipoftheday.blogspot.com/2007/11/tip-of-day-primitive-but-efficient-way.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item></channel></rss>