3

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?

4
  • 1
    @Graviton: This is off-topic for this site. This site is for doing code reviews, which means questions should have code to be looked at. Commented Feb 18, 2011 at 13:44
  • programmers.stackexchange.com may be a better site for this question.
    – Michael K
    Commented Feb 18, 2011 at 13:59
  • @Mark, I think this question belongs here-- it's all about how the code should be and best practices, so it's a form of code review service that I'm asking about.
    – Graviton
    Commented Feb 18, 2011 at 15:27
  • 1
    @Graviton: Then that's what Programmers.SE is for. Commented Feb 18, 2011 at 15:50

3 Answers 3

3

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.

2

I would definitely go with a non-static class. This allows you to implement IDisposable and clean up your un-managed resources in the way .NET prefers.

1

If you are doing TDD, you might want to read this article from the Google Testing blog.

I really liked the following quote from the article, which summarizes the problem really well:

The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to "wire" since there are no objects, the code and data are separate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.