C# – Interop Best Practices: Should I use Static Class, or Normal Classes

c

I have a front end C# that needs to call a C++ back end. So interop is needed.

I have an "interop layer", that converts the C# data structure into C++ structure, and do all the memory freeing grunt work.

My question is, should I write this interop layer as a static class, or should I wrap it in a normal class and instantiate it as an object when I need to use it?

Best Answer

static (especially static classes) should be avoided as much as possible, IMO. It results in inflexible and hard to test code. A proper normal class would also allow you to do some initialization or cleanup (via IDisposable) if needed, and you could easily switch to an interface to utilize mocking or replace the backend with a managed one.