Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need (for unit testing purposes) to create adapters for external library classes.ExchangeService and ContactsFolder are Microsoft's implementations in its' EWS library. So I created my adapters that implement my interfaces, but it seems that contactsFolder has a dependency for ExchangeService in its' constructor. The problem is that I cannot instantiate ContactsFolderAdapter without somehow accessing the actual ExchangeService instance (I see only ExchangeServiceAdapter in scope).

Is there a better pattern for this that retains the adapter classes? Or should I "infect" ExchangeServiceAdapter with some kind of GetActualObject method?

enter image description here

share|improve this question

migrated from Jun 6 '14 at 19:13

This question came from our site for professional and enthusiast programmers.

I think the adapter pattern is the right one here. What I don't see is your interfaces having more than one concrete implementation which is the advantage of the pattern in this context. Assuming that you only want to use IContactsFolder and IExchangeService in your unit tests (Microsoft is responsible for testing their classes), you need to create a mock or stub implementation of each interface that returns predictable dummy data. There are a lot of .NET frameworks for object mocking that will automatically implement these interfaces for you.

If there is some logic you are trying to test that exists in ExchangeServiceAdapter and ContactsFolderAdapter, then that logic needs to be extracted out into a class that uses the IContactsFolder or IExchangeService interfaces. Adapters should do nothing but translate one interface into another.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.