helper method returning a call to itself I wanted to eliminate some duplication and came up with something I've not seen so I figured I post it and see if others had.

Using a delegate to allow the method to return another call to itself.

The commented out code is what I replaced.

delegate ChainRepositoryCommand ChainRepositoryCommand(Action&lt;Data.Repository&gt; action);
ChainRepositoryCommand RepositoryCommand(Action&lt;Data.Repository&gt; action)
{
    using (var repo = new Data.Repository(true))
    {
        action(repo);
    }
    return new ChainRepositoryCommand(a =&gt; RepositoryCommand(a));
}

public void Register(ISender&lt;T&gt; sender)
{
    RepositoryCommand
        (repo =&gt; repo.AddClient(sender.Name, true))
        (repo =&gt; repo.AddMessageType&lt;T&gt;())
        (repo =&gt; repo.AddRegistration&lt;T&gt;(this.name, sender.Name));
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddClient(sender.Name, true);
    //}
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddMessageType&lt;T&gt;();
    //}
    //using (var repo = new Data.Repository(true))
    //{
    //    repo.AddRegistration&lt;T&gt;(this.name, sender.Name);
    //}
    sender.Sending += sender_Sending;
}


Update: I worked it into my repository class as a static method.  I know this is all probably obfuscating from a clean code standpoint, but it's fun to experiment with new things from time to time.

public class Repository : IDisposable
{
    ...
    public delegate Chain Chain(Action&lt;Repository&gt; action);
    public static Chain Command(Action&lt;Repository&gt; action)
    {
        using (var repo = new Data.Repository(true))
        {
            action(repo);
        }
        return new Chain(next =&gt; Command(next));
    }
    ...
}

public class Server&lt;T&gt; : IServer&lt;T&gt;
{
    ...
    public Server(string name)
    {
        serverName = name;
        Data.Repository.Command
            (repo =&gt; repo.AddServer(name));
    }

    public void Register(ISender&lt;T&gt; sender)
    {
        Data.Repository.Command
            (repo =&gt; repo.AddClient(sender.Name, true))
            (repo =&gt; repo.AddMessageType&lt;T&gt;())
            (repo =&gt; repo.AddRegistration&lt;T&gt;(this.serverName, sender.Name));
        sender.Sending += sender_Sending;
    }
    ...
}
Using a delegate to allow the method to return another call to itself.


You don't necessarily need a delegate to allow another call to itself. For example:

public class SimpleCalculation
{
    private int result;

    public int Result
    {
        get { return result; }
    }

    public SimpleCalculation Add(int i)
    {
        result += i;
        return this;
    }

    public SimpleCalculation Subtract(int i)
    {
        result -= i;
        return this;
    }
}

//Usage:
var sc = new SimpleCalculation();
sc.Add(5).Subtract(2);
Console.WriteLine(sc.Result); //Writes '3' to the screen


I'm not saying your code is wrong, only saying that you don't need delegates to achieve this. Also this:

//using (var repo = new Data.Repository(true))
//{
//    repo.AddClient(sender.Name, true);
//}
//using (var repo = new Data.Repository(true))
//{
//    repo.AddMessageType&lt;T&gt;();
//}
//using (var repo = new Data.Repository(true))
//{
//    repo.AddRegistration&lt;T&gt;(this.name, sender.Name);
//}


Could also be rewritten as:

using (var repo = new Data.Repository(true))
{
    repo.AddClient(sender.Name, true);
    repo.AddMessageType&lt;T&gt;();
    repo.AddRegistration&lt;T&gt;(this.name, sender.Name);
}


I don't know if you already knew or tried this but I'm just pointing it out as a possibility. But I agree with you: experimenting with code is fun! :)