Monday 29 June 2009

Resharper and ranges

If you used Resharper 4.5, you immediately noticed that the Full Cleanup adds loads completely useless ranges to your otherwise clean code. Fortunately its quite easy to fix. From main menu in Visual Studio go to Resharper -> Options… then in Type Members Layout you have to untick <use default params> checkbox and edit the xml in the following way:

- remove all the <Group.. > tags together with all the content
- add a RemoveAllRegions="true" to all Pattern elements: <Pattern RemoveAllRegions="true">

After doing it I ended up with the following xml (with the comments stripped)

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">

<!--Do not reorder COM interfaces and structs marked by StructLayout attribute-->
<Pattern RemoveAllRegions="true">
<Match>
<Or Weight="100">
<And>
<Kind Is="interface"/>
<Or>
<HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute"/>
<HasAttribute CLRName="System.Runtime.InteropServices.ComImport"/>
</Or>
</And>
<HasAttribute CLRName="System.Runtime.InteropServices.StructLayoutAttribute"/>
</Or>
</Match>
</Pattern>

<!--Special formatting of NUnit test fixture-->
<Pattern RemoveAllRegions="true">
<Match>
<And Weight="100">
<Kind Is="class"/>
<HasAttribute CLRName="NUnit.Framework.TestFixtureAttribute" Inherit="true"/>
</And>
</Match>

<!--Setup/Teardow-->
<Entry>
<Match>
<And>
<Kind Is="method"/>
<Or>
<HasAttribute CLRName="NUnit.Framework.SetUpAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.TearDownAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.FixtureSetUpAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.FixtureTearDownAttribute" Inherit="true"/>
</Or>
</And>
</Match>
</Entry>

<!--All other members-->
<Entry/>

<!--Test methods-->
<Entry>
<Match>
<And Weight="100">
<Kind Is="method"/>
<HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false"/>
</And>
</Match>
<Sort>
<Name/>
</Sort>
</Entry>
</Pattern>

<!--Default pattern-->
<Pattern RemoveAllRegions="true">

<!--public delegate-->
<Entry>
<Match>
<And Weight="100">
<Access Is="public"/>
<Kind Is="delegate"/>
</And>
</Match>
<Sort>
<Name/>
</Sort>
</Entry>

<!--public enum-->
<Entry>
<Match>
<And Weight="100">
<Access Is="public"/>
<Kind Is="enum"/>
</And>
</Match>
<Sort>
<Name/>
</Sort>
</Entry>

<!--fields and constants-->
<Entry>
<Match>
<Or>
<Kind Is="constant"/>
<Kind Is="field"/>
</Or>
</Match>
<Sort>
<Kind Order="constant field"/>
<Static/>
<Readonly/>
<Name/>
</Sort>
</Entry>

<!--Constructors. Place static one first-->
<Entry>
<Match>
<Kind Is="constructor"/>
</Match>
<Sort>
<Static/>
</Sort>
</Entry>

<!--properties, indexers-->
<Entry>
<Match>
<Or>
<Kind Is="property"/>
<Kind Is="indexer"/>
</Or>
</Match>
</Entry>

<!--interface implementations-->
<Entry>
<Match>
<And Weight="100">
<Kind Is="member"/>
<ImplementsInterface/>
</And>
</Match>
<Sort>
<ImplementsInterface Immediate="true"/>
</Sort>
</Entry>

<!--all other members-->
<Entry/>

<!--nested types-->
<Entry>
<Match>
<Kind Is="type"/>
</Match>
<Sort>
<Name/>
</Sort>
</Entry>
</Pattern>

</Patterns>

Friday 26 June 2009

JSON-RPC services

Recently I had to implement a JSON-RPC (http://json-rpc.org/) for integration with some other system. First I tried to use the new WCF's new capability to work with JSON data, but this was a painful experience. After a full day of getting NullReferenceExceptions without any reasonable error message, I gave up. Let’s face it, a library that gives your NullReferenceException only because you misconfigured something is crap and shouldn’t be used in production environment.

Then one of my colleagues recommended JayRock (http://jayrock.berlios.de/) open source framework – and here the experience was completely different! After some 15 minutes I had the web service up and running. This framework gives you the (old school) IHttpHandler which not only automagically exposes all the methods you need, but also gives you a neat http test page, where you can see all the methods implemented on the service and run them. It is easy to reverse-engineer the java script from these pages and include them in your web site. Strongly recommended!