Previously I’ve described the most popular ways to deal with TCP/IP connection in .NET framework. This time I want to show you one of the most important interface in the XGain server – ISocket.
Network layer is really hard to test and debug. XGain works on the Socket class level and this class doesn’t have an interface. That means testing of any component references to this type is really hard, but everything is possible. Few months ago I found a good podcast with Paweł Klimczyk and Maciej Aniserewicz about mocking frameworks. In full .NET Framework we can use TypeMock to create a fake call to the any method from any type. Unfortunately, this library does not support .NET Core platform.
I have decided to create a interface and wrapper for .NET Socket class. How ISocket looks? It is an interface with few properties like BufferSize and several methods.
XGainSocket is a simple wrapper over internal Socket. This class has only internal visibility, so we are sure of that the client will not know what is happening in the heart of the server.
Those two types create a layer of abstraction between XGain and any potential client. It means client will be available to create unit tests of his processing logic without any hardcore tricks.
However, creating interface and wrapper for particular type has advantages and disadvantages. Unit tests are less complicated and we have more possibilities to operate on the definition instead of implementation. But… Don’t create interface and wrapper for every type! Use common sense. Use frameworks and libraries like TypeMock. Think! Sometimes there is no sense in testing every element. Consider another types of tests. When you are dealing with for example Database, maybe integration tests will be better solution.
XGain in version 1.0.0 is available on the nuget.org. You can install my server by typing this line in nuget command prompt.
In next post I’m going to describe some tricks with network buffer and Negle’s algorithm. Feel free to ask, comment and give feedback!