mikeobrien/HidLibrary — pull request #40 by amullins83 (2014-04-10 18:54:50+00:00)
Title: Interfaces

This commit introduces two interfaces: IHidDevice and IHidEnumerator, that can be used to create testing mocks for classes that consume HidLibrary. In order to facilitate this use, I had to pull the delegate and event type declarations out of HidDevice and into the HidLibrary namespace. Any projects out there relying on the existence of `HidDevice.RemovedHandler` must instead use `RemovedHandler`. All of the examples included in this repo work with these changes. The IHidEnumerator definition file includes an instance class that wraps `HidDevices`. An example use case:

#MyDevice.cs:

```C#
using System.Collections.Generic;
using System.Linq;
using HidLibrary;

namespace MyStuff
{
public class MyDevice
{
private static myVid = 0x9999;
private static myPid = 0x999;

private IHidDevice _device;

public MyDevice(IHidEnumerator enumerator)
{
this._device = enumerator.Enumerate(myVid, myPid).FirstOrDefault();
}

// TODO: define useful methods for your device
}
}
```

#MyTestHid.cs

```C#
using NUnit.Framework;
using HidLibrary;
using MyStuff;

namespace MyStuffTests
{
public class TestHid : IHidDevice
{
// TODO: implement the interface any way you like
}

public class TestHidEnumerator : IHidEnumerator
{
// TODO: define Enumerate() methods
}

[TestFixture]
public class MyDeviceTests
{
private MyDevice device;
private TestHidEnumerator enumerator = new TestHidEnumerator();

[SetUp]
public void beforeEach()
{
device = new MyDevice(enumerator);
}

// TODO: Add your tests
}
}
```

#Program.cs

```C#
using MyStuff;
using HidLibrary;

namespace MyApp
{
public static class Program
{
public static void Main()
{
var myDevice = new MyDevice(new HidEnumerator());
// TODO: Use your device to change the world
}
}
}
```