A: 

One approach you don't mention (and the one I'd prefer in most cases) is to extract interfaces for the classes you want the user of the API to be able to fake. Not knowing your API not every single class in it has to have it's interface extracted.

Patrik Hägne
This won't really work well because the API has several methods that take in and expose a concrete class. Changing this would be a breaking change.
Jeff Moser
I'm not sure how extracting an interface could be a breaking change, you don't necessarily have to use the extracted interface in your API. It's on the clients side were he/she would expect an IFoo instead of a Foo, if you pass them a Foo from the API, that's fine.
Patrik Hägne
The trick is that existing client could would be expecting a "Foo", not an IFoo that we'd be returning, and then things break. Likewise, if the existing API had a class that took in an IFoo but (to keep the API the same) exposed a Foo property, things get messy quickly.
Aaron Lerch
Wow, ignore the bad grammar in my first sentence. :)
Aaron Lerch
Well, ofcourse method parameters would have to be changed to expect IFoo's also, but that wouldn't be a breaking change, would it? If it would, if you for example know that theres some reflection mumbo jumbo going on, just create overloads that expect the interfaces.
Patrik Hägne
To clarify Aaron's remarks, imagine you have an existing "public class UserRepository { public UserData GetData(string userName) {...} }" you could have an interface IUserRepository and IUserData, but existing customers are expecting the GetData to return a concrete "UserData" and not a IUserData.
Jeff Moser
The extracted interface would have to have something like "IUserData GetData(string userName);", but that would break existing customers. If the extracted interface was "UserData GetData(string userName);", we'd be back to the same problem.
Jeff Moser
Yes, of course, you can not change return values, but that shouldn't matter to the client when mocking. It's the interaction with the API that's mocked so he would in his code expect an IFoo, that the API returns Foo's in some cases shouldn't matter. Maybe you're talking about integration tests.
Patrik Hägne
That would work, yes. With (at least) one problematic exception: classes that accept the abstraction (IFoo) and expose it out as a concrete property: public Foo TheFoo { get; }Assuming parameter "foo", each class would need to do some sort of _Foo = foo as Foo; which is messy.
Aaron Lerch
What you're pointing out seems like an edge case Aaron, depending on the particular case, inside the class that accepts the IFoo, convert it to a Foo and use it for the property. I'm not saying this is the solution for all cases in the entire API, but it's the preferred one, where it's possible.
Patrik Hägne
A: 

Third party users should not be testing your API. They would want to test their code against your API and so they need to create Mocks of the API etc. but they would be relying on your testing of the API to ensure it works. Or is that what you meant? Do you want to make your API easy to test against?

Start again in that case, and this time think about the testers :)

Brody
Nice clarification :) Our API is used by other applications. For example, say that one of our classes returned a concrete "UserData" class and the 3rd party application took in this class and did things with it. I want them to be able to mock out the real UserData.
Jeff Moser
In short, they're not testing our API. They're testing how their code will respond when given specific data from our API. E.g. they can create a mock that fires a certain event or returns a specific piece of data back, just as if our API supplied it natively.
Jeff Moser
What you point out here is exactly why the approach of extracting interfaces works perfectly well.
Patrik Hägne
+2  A: 

Another approach would be to create a seperate branch of the API and do option 3 there. Then you just maintain these two versions and deprecate the former. Merging changes from one branch into the other should work automatically most of the time.

Kim
This is the long term solution. The old API becomes a facade for the newer, correct one
chadmyers
I think that a separate branch for doing #3 would be make sense since it'd take a bit of code review. There'd still be cases where we'd want to be careful from a security perspective, but maybe there is no way around a member by member review to do it well.
Jeff Moser
+4  A: 

What you're really asking is, "How do I design my API with SOLID and similar principles in mind so my API plays well with others?" It's not just about testability. If your customers are having problems testing their code with yours, then they're also having problems WRITING/USING their code with yours, so this is a bigger problem than just testability.

Simply extracting interfaces will not solve the problem because it's likely your existing class interfaces (what the concrete classes expose as their methods/properties) aren't design with Interface Segregation Principle in mind, so the extracted interface would have all sorts of problems (some of which you mentioned in comment to a previous answer).

I like to call this the IHttpContext problem. ASP.NET, as you know, is very difficult to test around or with due to the "Magic Singleton Dependency" problem of HttpContext.Current. HttpContext is not mockable without fancy tricks like what TypeMock uses. Simply extracting an interface of HttpContext is not going to help that much because it's SO huge. Eventually, even IHttpContext would become a burden to test with so much so that it's almost not worth doing any more than trying to mock HttpContext itself.

Identifying object responsibilities, slicing up interfaces and interactions appropriately, and designing with Open/Closed Principle in mind is not something you and try to force/cram into an existing API designed without these principles in mind.

I hate to leave you with such a grim answer, so I'll give you one positive suggest: How's about YOU take all the grief on behalf of your customers and make some sort of service/facade layer over top of your old API. This service layer will have to deal with the minutiae and pain of your API, but will present a nice, clean, SOLID-friendly public API that your customers can use with much less friction.

This also has the added benefit of allowing you to slowly replace parts of your API and eventually make it so your new API isn't just a facade, it IS the API (and the old API is phased out).

chadmyers
Very well put Chad, actually you're taking words out of my mouth, I was just about to update my earlier answer and nuance what I said a bit and give some examples but you nailed it here. I think that the service/facade layer as you put it is the way to go.
Patrik Hägne
I agree with the theory of your answer. I also agree that the .NET framework itself has some similar problems that we're facing(as you mention with HttpContext and friends).The problem that we're facing is that the existing API we have is so huge that it'd be a major undertaking to add another layer
Jeff Moser
The reason is that there are so many classes. It's a hard sell, especially in the current economy, to spend time and money to replace what we have, (even if spread it out over time). It's hard because I agree with the theory, but the business demands right now won't allow a huge systemic fix.
Jeff Moser
Testability is important and I don't want to shirk this point. We do have quite a bit of integration testing on the existing API, but we don't have the ability to easily mock collaborators with our classes and that's now causing grief, especially in the era of good mock-ing frameworks.
Jeff Moser
Jeff: I understand. So then maybe the answer is: "Just don't worry about it, then". Let them use TypeMock or something. Going forward, start incorporating SOLID principles into your design for the new stuff.
chadmyers
+1  A: 

As a reply to your edit, interface extraction does indeed work very well here:

public interface IUserRepository
{
    IUserData GetData(string userName);
}

public class UserRepository 
    : IUserRepository
{
    // The old method is not touched.
    public UserData GetData(string userName)
    {
        ...    
    }

    // Explicitly implement the interface method.
    IUserData IUserRepository.GetData(string userName)
    {
        return this.GetData(userName);
    }
}

As I also said in a comment this may not be the way to go in every place. I think you should identify some main points in your API where it's extra important for your customers to be able to fake the interaction and start there. You don't have to make a complete rewrite of the whole API but it can transform gradually.

Patrik Hägne
Good point on the explicit interface argument. This does add an essentially duplicated method, but it is a transition option.Also, thanks for your continued work and following up on this question. I really appreciate it!
Jeff Moser
A: 

I agree with Kim. Why not re-write your core API using the best practices you explained, and supply a set of proxy/adapter classes that expose the old interface but talk to your new API?

Old developers will be naturally encouraged to migrate to the new API, but not be forced to immediately do so. New developers will simply use your new API. Announce an EOL for your old API interface if you are concerned about developers staying on the old API.

Robert Venables
It definitely sounds like a good idea. Unfortunately, the pragmatic side is hard to ignore. There is a huge investment of time and training in the old API that a rewrite is infeasible from a business perspective at the moment.
Jeff Moser