Team XSockets.NET

Custom Extensions

Almost every method you use in the controller is a extension method. All the methods for communication such as Invoke, InvokeTo, InvokeToAll, Publish, PublishTo, PublishToAll are extensions. The FindOn methods are also extensions.

Extensions are powerful and you can easily write your own!

If you are not used to generics and extension methods this might look complex, but you will get used to it.

The signature of a simple extension method for a XSockets controller may look like this.

public static void DoStuff<T>(this IXSocketController controller) where T : class, IXSocketController;

This is as basic as it gets. First of the method is static (as will also be in a static class). Then the method is generic <T> and we tell at the end of the signature that the type of T needs to be an instance (class) and that the instance have to implement IXSocketController.

So the complete sample may look like.

public static class MyExtensions
{
    public static void DoStuff<T>(this IXSocketController controller) where T : class, IXSocketController
    {
        Composable.GetExport<IXLogger>().Debug("In my custom DoStuff extension");
    }
}

You will now be able to use your new extension from any controller like this.

this.DoStuff();

Not very useful, but next we will show a few samples of more useful extension.


Note: It might look strange that we have this

public static void DoStuff<T>(this IXSocketController controller) where T : class, IXSocketController

when we could have had this instead

public static void DoStuff<T>(this T controller) where T : class, IXSocketController

The reason for us having IXSocketController as the extension type is that we now can pass other controllers as the type T. In other words, we can now use the extension between controllers so that controller A can use it on controller B like this.

var a = new A();
a.DoStuff<B>();

results matching ""

    No results matching ""