Castle DynamicProxy IInterceptorSelector implementation

I just got this test pass:

[Test]
public void BasicCase()
{
    ProxyGenerationOptions options = new ProxyGenerationOptions();
    options.Selector = new AllInterceptorSelector();
    var target = this.generator.CreateInterfaceProxyWithTarget(
        typeof(ISimpleInterface), 
        new SimpleClass(),
        options,
        new NoopInterceptor() ) as ISimpleInterface;
    Assert.IsNotNull( target );
    target.Do();            
}

And here’s how Do proxy method looks like in Reflector:

interceptorSelectorMethodInReflector 

Skip generated types when performing analysis in NDepend

Code created in more recent versions of C# has a lot of generated types, even if you don’t use code generation explicitly. Interators (the yield keyword), and anonymous delegates both use generated types underneath. Also those neat anonymous types introduced in C# 3.0 are nothing more than a compiler magic.

All this may clutter your NDepend window of choice, when looking at your projects. You can however get rid of generated stuff, pretty easily, with this simple CQL query:

// <Name>Types not generated by the compiler</Name>
SELECT TYPES FROM ASSEMBLIES "YourAssembly" 
WHERE !(HasAttribute "System.Runtime.CompilerServices.CompilerGeneratedAttribute")

Simple, but useful.

If you want to get rid of just some types of generated classes, for example you want to get rid of iterators, but want to leave anonymous delegate classes, you may use regular expressions for that. This is my clumsy attempt to that:

// <Name>Types not generated by the compiler</Name>
SELECT TYPES FROM ASSEMBLIES "Castle.Facilities.WcfIntegration" WHERE 
!NameLike ".+<>c__.+" /* Anonymous delegates, like private sealed class <>c__DisplayClass1
*/ AND 
!NameLike ".+\+<.*>d__.+" /* Iterators, like private sealed class <FindDependencies>d__0<T> :
IEnumerable<T>, IEnumerable, IEnumerator<T>, IEnumerator, IDisposable */

Technorati Tags: , ,

Remove assemblies from Dependency Graph in NDepend

Last minor version of NDepend introduced cool, interactive Dependency Graph, that was really a huge step forward as compared to static .png we got earlier. You can now load set of assemblies, and immediately see dependencies between them, without running your_picture_viewer_here. You also can drill down the dependency tree  and see dependencies between namespaces within an assembly, classes within namespace, methods within class…

One thing (ok, there are more, but we’ll get to that in a minute), that I missed, was the ability to remove an element from the graph. You could do this using CQL, but this just was too complicated solution. This is now possible with new and fresh 2.10.3 build.

Remove element from NDepend' Dependency Graph

So, back to my wishlist. You can interact with only one element of the graph at any given moment. That means, although you can select few assembles with ctrl+LMB if you click Remove, only one will get removed. Same with drilling down. You can’t select few assembles and go “View internal dependency cycles on graph” to see cycles between classes in all of those assemblies. (well, you can as a matter of fact do this with CQL, but again, this may require a lot of typing and GUI just feels like the right place to do it.)

Second thing I wish it had, is a scrollbar for zooming instead of + / – buttons. Can we get this Partick?

Technorati Tags: ,

All your RAM are belong to us

I’m finishing work on my master’s thesis. As a part of it, I’m evaluating different Agile PM tools. I just installed ThoughtWorks Mingle on my VM and I was amazed that it was veeeery slow. I opened Task Manager and I saw this:

mingle

I’m pretty sure it shouldn’t be like that. But at least now I understand why my VM is crawling.

 

Technorati Tags: ,,

Learning Wix (the hard way)

I spent few last days at work, trying to create an installer for an application I’ve been working on. There aren’t many free tools to help you with that task: Wix, NSIS or Inno Setup as far as I know. I thought Wix was a reasonable choice. I don’t think that anymore.

Wix’s documentation ranges from poor to nonexistent. Many things are done in a very awkward way. And the best (and in many cases only) way to get any help, is the discussion group wix-users.

I’ve had my share of wrestling with Wix. Here’s what I learned.

  • You can define properties, call predefined properties, and change their values using [propertyName] syntax. This however does not work everywhere. If you for example want to pass some dynamic data to your Custom Action, you need another Custom Action, that will set the parameters for you before you can use them.

For example.

    <CustomAction Id="SetParameter" Property="MyAction" Value="&quot;[#myProgramToCall.exe]&quot;  -param [A_PROPERTY]" Execute="immediate" Return="check"  />
    <CustomAction Id="MyAction" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="no"/>
    <InstallExecuteSequence>
      <Custom Action="SetParameter" After="CostFinalize">
        (Not Installed) AND (NOT REMOVE)
      </Custom>
      <Custom Action="MyAction" Before="InstallFinalize">
        (Not Installed) AND (NOT REMOVE)
      </Custom>
    </InstallExecuteSequence>

You need all of that just to call myProgramToCall.exe during your installation with command line ‘-param whatever_the_value_of_A_PROPERTY_is’. You also need to set exactly those attributes on CustomAction tags, in order for that to work. This is different than in the documentation of QtExec, that sais:

<CustomAction Id="QtExecDeferredExampleWithProperty_Cmd" Property="QtExecDeferredExampleWithProperty"
              Value="&quot;[#MyExecutable.exe]&quot;" Execute="immediate" Return="ignore"/>
<CustomAction Id="QtExecDeferredExampleWithProperty" BinaryKey="WixCA" DllEntry="CAQuietExec"
              Execute="deferred" Return="check" Impersonate="no"/>
.
.
.
<InstallExecuteSequence>
    <Custom Action="QtExecDeferredExampleWithProperty_Cmd" After="CostFinalize"/>
    <Custom Action="QtExecDeferredExampleWithProperty" After="TheActionYouWantItAfter"/>
</InstallExecuteSequence>

If you set Return=”ignore” on Custom Action that sets the parameter, you’ll get an exception:

Error	1	ICE68: Invalid custom action type for action 'SetCertsBatPath'.

  • The (Not Installed) AND (NOT REMOVE)  is needed, if you want to run the action only when program is being installed, and not when it’s being removed.
  • If you have Votive project as part of your Visual Studio Solution and you want to reference files in other projects, you can use relative paths. Also use Configuration environmental variable to include the right version of files (Release/Debug). In order for this to compile, you also need to add project reference to My.Actual.Project in your Wix project.
<File Id="Microsoft.Practices.ObjectBuilder2.dll" Name="Microsoft.Practices.ObjectBuilder2.dll" DiskId="1"
Source="../My.Actual.Project/bin/$(var.My.Actual.Project.Configuration)/Microsoft.Practices.ObjectBuilder2.dll"/>
  • If you want to install a windows service as part of your installation, you use ServiceInstall tag, like this:
<ServiceInstall Id="MyService"
   Name="MyService"
   DisplayName="MyService"
   Type="ownProcess"
   Start="auto"
   ErrorControl="normal"  Vital="yes" Account="[SERVICE_USERNAME]" Password="[SERVICE_PASSWORD]"
   Description="Desc">
   <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" 
      ThirdFailureActionType="restart" RestartServiceDelayInSeconds="10" ResetPeriodInDays="1" />
</ServiceInstall>
<ServiceControl Id="MyServiceControl"
   Name="MyService" Start="install" Stop="both" Remove="uninstall" Wait="yes"/>

Notice that there is no reference to the actual file containing the service. If you have many <File /> tags ServiceInstall will pick, and try to install as service the first one of them from the top. Alternatively you can explicitly designate a file by setting KeyPath attribute to yes.

<File KeyPath="yes"/>
  • Notice I passed username of a user that should be used to run a service, and password with [SERVICE_PASSWORD] and [SERVICE_USERNAME] properties. Where did they come from? – I customized the UI, and added a step to the installer, where user types in the username and the password.

    It requires quite a lot of work, but it’s actually quite straightforward process. I created another file in my Wix project, that holds the UI. I took one of the standard ones, and customized it as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <WixVariable Id="WixUIBannerBmp" Value="My.Banner.bmp" />
        <WixVariable Id="WixUILicenseRtf" Value="License.rtf" />
        <UI Id="WixUI_MyUI">
            <TextStyle Id="WixUI_Font_Normal" FaceName="Tahoma" Size="8" />
            <TextStyle Id="WixUI_Font_Bigger" FaceName="Tahoma" Size="12" />
            <TextStyle Id="WixUI_Font_Title" FaceName="Tahoma" Size="9" Bold="yes" />
            <Property Id="DefaultUIFont" Value="WixUI_Font_Normal" />
            <DialogRef Id="BrowseDlg" />
            <DialogRef Id="DiskCostDlg" />
            <DialogRef Id="ErrorDlg" />
            <DialogRef Id="FatalError" />
            <DialogRef Id="FilesInUse" />
            <DialogRef Id="MsiRMFilesInUse" />
            <DialogRef Id="PrepareDlg" />
            <DialogRef Id="ProgressDlg" />
            <DialogRef Id="ResumeDlg" />
            <DialogRef Id="UserExit" />
 
            <Publish Dialog="ExitDialog" Control="Finish" Event="EndDialog" Value="Return" Order="999">1</Publish>
            <Publish Dialog="WelcomeDlg" Control="Next" Event="NewDialog" Value="ServiceUserCredentialsDlg">1</Publish>
            <Publish Dialog="ServiceUserCredentialsDlg" Control="Back" Event="NewDialog" Value="WelcomeDlg">1</Publish>
            <Publish Dialog="ServiceUserCredentialsDlg" Control="Next" Event="NewDialog" Value="InstallDirDlg">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="Back" Event="NewDialog" Value="ServiceUserCredentialsDlg">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="Next" Event="SetTargetPath" Value="[WIXUI_INSTALLDIR]" Order="1">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="Next" Event="NewDialog" Value="VerifyReadyDlg" Order="2">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="ChangeFolder" Property="_BrowseProperty" Value="[WIXUI_INSTALLDIR]" Order="1">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="ChangeFolder" Event="SpawnDialog" Value="BrowseDlg" Order="2">1</Publish>
            <Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="InstallDirDlg" Order="1">NOT Installed</Publish>
            <Publish Dialog="VerifyReadyDlg" Control="Back" Event="NewDialog" Value="MaintenanceTypeDlg" Order="2">Installed</Publish>
            <Publish Dialog="MaintenanceWelcomeDlg" Control="Next" Event="NewDialog" Value="MaintenanceTypeDlg">1</Publish>
            <Publish Dialog="MaintenanceTypeDlg" Control="RepairButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish>
            <Publish Dialog="MaintenanceTypeDlg" Control="RemoveButton" Event="NewDialog" Value="VerifyReadyDlg">1</Publish>
            <Publish Dialog="MaintenanceTypeDlg" Control="Back" Event="NewDialog" Value="MaintenanceWelcomeDlg">1</Publish>
            <Property Id="ARPNOMODIFY" Value="1" />
 
            <Dialog Id="ServiceUserCredentialsDlg" Width="370" Height="270" Title="[ProductName] Setup">
                <Control Id="Title" Type="Text" X="15" Y="6" Width="200" Height="15" Transparent="yes" NoPrefix="yes" Text="{\WixUI_Font_Title}User Information" />
                <Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes" Text="Description" />
                <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="!(loc.InstallDirDlgBannerBitmap)" />
                <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" />
                <Control Id="TextLabel1" Type="Text" NoWrap="no" X="20" Y="50" Width="290" Height="40" Text="A description" />
                <Control Id="UserNameLabel" Type="Text"   X="20" Y="110" Width="290" Height="13" Text="User Name:" />
                <Control Id="UserName" Type="Edit"        X="20" Y="122" Width="320" Height="18" Property="SERVICE_USERNAME" Text="47"/>
                <Control Id="PassLabel" Type="Text"    X="20" Y="143" Width="290" Height="13" Text="Password:" />
                <Control Id="Password" Type="Edit" Password="yes"  X="20" Y="155" Width="320" Height="18" Property="SERVICE_PASSWORD"/>
                <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" />
                <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="Next">
                    <Condition Action="disable">(SERVICE_USERNAME = "") OR (SERVICE_PASSWORD = "")</Condition>
                    <Condition Action="enable">
                        <![CDATA[(SERVICE_USERNAME <> "") AND (SERVICE_PASSWORD <> "")]]> </Condition>
                </Control>
                <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="Back" />
                <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="Cancel">
                    <Publish Event="SpawnDialog" Value="CancelDlg">1</Publish>
                </Control>
            </Dialog>
        </UI>
 
        <UIRef Id="WixUI_Common" />
    </Fragment>
</Wix>

The Dialog tag, defines the look of the added window. It has some labels, and two textboxes, where user types username and password, that get saved to their respective properties. There is also a condition set on the Next button, which disables it, unless both fields are filled out.

The series of Publish above that, defines transitions between windows (which window should be shown when you click Next/Back).

Now, to actually instruct Wix to use this particular UI, we need to add in the first file the following line within Product tag:

<UIRef Id="WixUI_MyUI"/>

Hope this helps.

Technorati Tags:

Testing collections with NUnit

How do you test collections for equality of their elements? I often used to write my own custom assert for that, something like:

public void AssertCollectionElementsAreEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
{
    var first = expected.GetEnumerator();
    var second = actual.GetEnumerator();
    int count = 0;
    while(first.MoveNext())
    {
        if(second.MoveNext())
        {
            Assert.AreEqual(first.Current,second.Current);
        }
        else
        {
            throw new AssertionException(string.Format("Collection has less elements than expected: {0}", count));
        }
        count++;
    }
    if(second.MoveNext())
    {
        throw new AssertionException(string.Format("Collection has more elements than expected."));
    }
}

I thought it’s just plain to common task to not be included in the framework itself, so I read the manual, and I found CollectionAssert.AreEqual() method that does exactly that.

Then Charlie Poole, made me realize there is even simpler way.

using System.Collections.Generic;
using NUnit.Framework;
 
namespace NUnitAreEqualSample
{
    [TestFixture]
    public class CollectionsEqualitySampleTests
    {
        [Test]
        public void DifferentTypesOfCollectionsShoudBeEqualIfElementsAreEqual()
        {
            var list = new List<string> {"foo", "bar"};
            var array = new[] {"foo", "bar"};
            Assert.AreEqual(list, array);//notice this
        }
    }
}

This produces following result:

nunit_areEqual_passed

Simple Assert.AreEqual() does the job. Sweet.

Technorati Tags: , ,

AnkhSVN 2.0 – Visual Studio SVN integration reinvented

Along with Subversion 1.5 and TortoiseSVN 1.5 a new version of AnkhSVN has been released some time ago.

AnkhSVN_Solution_Explorer

AnkhSVN v1.x used to suffer from many issues. It was instable, used far too much resources, and had many usability bugs, that repelled many people. For version 2.0 many parts of the tool have been rewritten from ground up, and now it’s a very descent tool that you should give second chance if you abandoned it after trying out previous version.

I was very pleasantly surprised by it, when I fired up my Visual Studio today, and I saw this:

AnkhSVN

Geee, now it even checks for updates, cool.

Good stuff. If you passed on it before, you definitely should give it another try – really, new version is a whole other tool.

Disable Firefox download window flashing

If you (like me) are annoyed by the Firefox download window coming up and flashing on your taskbar each time you download a file here’s quick way to disable it:

  • Go to about:config (like I shown here)
  • find browser.download.manager.flashCount
  • change it’s value to 0

done.

 

You may also want to make sure that browser.download.manager.focusWhenStarting is false.

Technorati Tags:

Posting to Twitter with Launchy

[UPDATE2]

This tool has been deprecated. I leave the download links as they are, but if you want to interact with Twitter from within Launchy you better check out updated version of this tool here.

[UPDATE]

I uploaded a binary version of the tool, for those who don’t want/can’t deal with source code.

Grab it here.

I’ve been using Twitter for few days now, and I’ve tried quite a few options to use it. Currently I’ve settled on three different ways of interaction with the services (excluding browsing its website)

  • When I’m in Firefox I use TwitterBar extension, that allows me to write posts in my address bar.
  • When I read posts (tweets?) of other users in Twihrl and I want to comment on them, I use Twihrl to post.
  • When I’m just doing something on my computer and I want to post without running Firefox or Twihrl I use my beloved app launcher – Launchy to post.

This last option is a little bit tricky and it required a little bit of work to set up. I assume you have Launchy already installed. If not, go get it here.

You’ll also need Twitt.Poster that we’ll be invoking from Launchy to do actual work for us. Just compile the project, set your username and password in app.config, and put the compiled files in some directory.

Next, open Launchy, right-click it, and select “Options”. Then navigate to “Plugins” tab, and select “Runny”. This is the plugin, that will invoke Twitt.Poster.

To make it do so, click the ‘+’ button at the bottom and add new entry to the grid. Name is the name of the command in Launchy – “twitt” in my example but you can call it whatever you like. Then Program is the fully qualified path to the executable of the poster application. Last column is command line arguments and you should set it to “$$”. Notice that you need to put dollar sings in quotation marks, otherwise it won’t work. When you’re done just click OK, and we’re all set.

launchy_twitter

Now you can invoke the command in Launchy, press tab, and enter your message. When you’re done just press enter and your message should get posted to Twitter.

launchy_twitter2

That’s it. So far this is my favorite way of posting to Twitter.

As for Twitter.Poster, use it however you like and don’t sue me if it breaks something. I publish it on terms of BSD licence. However, it uses Twitterizer library, that is licensed under terms of GPL v3, so take that into account when creating derivative work.

This is just a sample application that I created mostly for myself. However you can post bugs and feature requests in comments. I don’t want to promise anything, but I may implement some of them if there’s demand and I find some time.

Technorati Tags: ,

ALT.NET Thunderbird configuration

ALT.NET discussion group is quite active.

altnet_activity

There are around 1700, to over 2000 posts per month. It’s easy to drown in the flood of information, and if you want to benefit from the group you need to develop a strategy.

Here’s mine:

I use Thunderbird, as my email client, and for the group as well. In its basic form, it’s not so well suited for the job of handling threaded discussion, but same as Firefox – you can expand its capabilities with extensions.

altnet_thunderbird_threads

The first thing to do, even before you go looking for extensions is viewing your messages by threads. [via Lifehacker]

To do this, go to Tools–>Options–>Advanced–>General, and select “Config Editor”. Find mailnews.thread_pane_column_unthreads and change its value to false. And you’re done. You should have now your messages grouped in threads and sorted by date, like I have on the screenshot above.

altnet_sort_by_thread

 altnet_extensions_quotecollapse

My favorite extension is QuoteCollapse. Its name is quite self-explanatory – it collapses quoted text in messages, so that you can only see what the person actually has written, without seeing all previous posts that you probably already read before.

 

altnet_extensions_quotecollapse_sample

  The small picture you see on the above screenshot was put there by MessageFaces extension. It gets those images from gravatar.com, or some other places. I don’t exactly know, but it’s good to visually recognize the author of the message. Even if you remember the author as “tape-measure guy”, it’s still easier to connect those posts to earlier posts by the same author, at least for me.

SearchBar, is a small but useful extension that allows you to quickly find some message very easily.

Url Link, is an extension that I use in both: Firefox and Thunderbird, that allows you to click links that are inserted as plain text, like this: www.goo… eeeer I mean www.live.com.

altnet_extensions_QuickTextQuickText allows you to easily insert parts of text, with some level of flexibility, i.e. you can do something like: [[TO=firstname]]. I use it for my header and footer mostly.

GMailUI sets a few usefull shortcuts, so that you can with one key, move to next unread message, mark all thread as read, and so on. I believe those are the same shortcuts as for GMail, but I’m to lazy to verify that.

And that’s about it. How about you? What do you use?