June 14, 2004

C# Needs Static Interface Members

For those who were subscribed to my old blog, this is a bit of a rehashing so forgive me. I'm in the middle of building a Web Service where I have to return some information about hotels. The data is stored in SQL Server, but I'm focusing on getting the messaging interface done before delving into the actual data retrieval. The web service has the requirement that it must conform exactly to the interface of an existing web service from one of our business partners. That way, our partners can go to the web.config file and swap out their implementation for ours. Turns out the syntax and semantics of their messages are pretty quirky (read crappy). For example, rather than making use of the SOAP Fault facility, their message format defines an node that contains and elements that must contain the data from System.Exception::Message and System.Exception::StackTrace, respectively (No, I'm not kidding).

Anyway, I digress. The point is that I want to test that the message structure returned from the web service conforms to their specifications under normal conditions and exception conditions. I know I only need to pass the SQL stored procedure (which I haven't written yet) two parameters: DestinationCode and HotelCode. What I really want to do is to create an interface that defines the operation like so:

public interface IHotelGateway { XPathNavigator GetHotelDetails(string DestinationCode, string HotelCode); }

This way, I can build myself a mock implementation of the IHotelGateway interface that returns some sample data right away, letting me concentrate on getting the interface right before I worry about the implementation details. This has the benefit of allowing the business partner to test their client against my web service without having to wait for me to do all the wiring against the database. The next problem I anticipate is that there will be several places in my local code that look like this:


IHotelGateway dal = new MockHotelGateway();

So it would be best to refactor this into a private member of the web service:

private IHotelGateway dal = new MockHotelGateway();

But being a lazy programmer, I don't want to have to recompile my application just to change the implementation (MockHotelGateway vs SqlHotelGateway). So I really want to have Factory object create the IHotelGateway for me based on certain conditions (Debug vs. Release for example). So I want to create:

public interface IHotelGatewayFactory
{
static IHotelGateway Create();
}

allowing my code to look like:

private IHotelGateway dal = HotelFactory.Create();

...but the C# compiler won't let me, because you can't have static interface members. Sure, I can remove the static requirement from my interface, sure I could use delegates instead, sure I could do a lot of things. But what I don't know is I shouldn't be able to create static interface members...perhaps I can get some smart Microsoft folks to respond?! Stay tuned!

Update: Dug up the original thread from Chris Anderson's weblog.

Posted by Christian at June 14, 2004 01:51 PM |
Comments
Post a comment