As I sit here on my deck, enjoying the cool autumn breeze1, I thought, what better thing to write about than Web services! Well, no, actually I am just recalling some stuff that's happened lately. On the MSDN Architecture forums and in some coding and design discussions we had this week, both of which involve the question of best practices for Web services.
Before we talk about Web services best practices, it seems to me that we need to distinguish between two kinds of application services. First, there are the services that everyone has been talking about for the last several years--those that pertain to service-oriented architecture (SOA). These are the services that fall into the application integration camp, so I like to call them inter-application services.
Second, there are services that are in place to make a complete application, such as logging, exception handling, data access and persistence, etc.--pretty much anything that makes an application go and is not a behavior of a particular domain object. Maybe thinking of them as domain object services would work, but I fear I may already be losing some, so let's get back to it. The main concern within this post are those services using within an application, so I call them intra-application services.
It seems like these latter services, the intra-application ones, are being often confused with the former--the inter-application services. It's certainly understandable because there has been so much hype around SOA in recent years that the term "service" has been taken over and has lost its more generic meaning. What's worse is that there has been a lot of confusion around the interaction of the terms Web service and just plain service (in the context of SOA). The result is that you have folks thinking that all Web services are SO services and sometimes that SO services are always Web services.
My hope here is to make some clarification as to the way I think we should be thinking about all this. First off, Web services are, in my book at least, simply a way of saying HTTP-protocol-based services, usually involving XML as the message format. There is no, nor should there be, any implicit connection between the term Web service and service-oriented service. So when you think Web service, don't assume anything more than that you're dealing with a software service that uses HTTP and XML.
The more important distinction comes in the intent of the service--the purpose the service is designed for. Before you even start worrying about whether a service is a Web service or not, you need to figure out what the purpose of the service is. This is where I get pragmatic (and those who know me know that I tend to be an idealist at heart). You simply need to determine if the service in question will be consumed by a client that you do not control.
The reason this question is important is that it dramatically affects how you design the service. If the answer is yes, you automatically take on the burden of treating the service as an integration (inter-application) service, and you must concern yourself with following best practices for those kinds of services. The core guideline is that you cannot assume anything about the way your service will be used. These services are the SO-type services that are much harder to design correctly, and there is tons of guidance available on how to do them2. I won't go in further depth on those here.
I do think, though, that the other kind of services--intra-application services--have been broadly overlooked or just lost amidst all the discussion of the other kind. Intra-application services do not have the external burdens that inter-application services have. They can and should be designed to serve the needs of your application or, in the case of cross-cutting services (concerns) to serve the needs of the applications within your enterprise. The wonderful thing about this is that you do have influence over your consumers, so you can safely make assumptions about them to enable you to make compromises in favor of other architectural concerns like performance, ease of use, maintainability, etc.
Now let's bring this back to the concrete question of best practices for intra-application Web services. For those who are using object-oriented design, designing a strong domain model, you may run into quite a bit of trouble when you need to distribute your application across physical (or at least process) tiers. Often this is the case for smart client applications--you have a rich front end client that uses Web services to communicate (usually for data access and persistence). The problem is that when you cross process boundaries, you end up needing to serialize, and with Web services, you usually serialize to XML. That in itself can pose some challenges, mainly around identity of objects, but with .NET, you also have to deal with the quirks of the serialization mechanisms.
For example, the default XML serialization is such that you have to have properties be public and read-write, and you must have a default constructor. These can break encapsulation and make it harder to design an object model that you can count on to act the way you expect it to. WCF makes this better by letting you use attributes to have better control over serialization. The other commonly faced challenge is on the client. By default, if you use the VS Add Web Reference, it takes care of the trouble of generating your service proxies, but it introduces a separate set of proxy objects that are of different types than your domain objects.
So you're left with the option of either using the proxy as-is and doing a conversion routine to convert the proxy objects to your domain objects, or you can modify the proxy to use your actual domain objects. The first solution introduces both a performance (creating more objects and transferring more data) and a complexity (having conversion routines to maintain) hit; the second solution introduces just a complexity hit (you have to modify the generated proxy a bit). Neither solution is perfectly elegant--we'd need the framework to change to support this scenario elegantly; as it is now, the Web services stuff is designed more with inter-application services in mind (hence the dumb proxies that encourage an anemic domain model) than the intra-application scenario we have where we intend to use the domain model itself on the client side.
If you take nothing else away from this discussion, I'd suggest the key take away is that when designing Web services, it is perfectly valid to do so within the scope of your application (or enterprise framework). There is a class of services for which it is safe to make assumptions about the clients, and you shouldn't let all of the high-falutin talk about SOA, WS-*, interoperability, etc. concern you if your scenario does not involve integration with other systems that are out of your control. If you find the need for such integration at a later point, you can design services (in a service layer) then to meet those needs, and you won't be shooting yourself in the foot trying to design one-size-fits-all services now that make so many compromises so as to make the app either impossible to use or very poorly performing.
My own preference that I'd recommend is to use the command-line tools that will generate proxies for you (you can even include a batch file in your project to do this) but then modify them to work with your domain model--you don't even need your clients to use the service proxies directly. If you use a provider model (plugin pattern) for these services, you can design a set of providers that use the Web services and a set that talk directly to your database. This enables you to use your domain model easily in both scenarios (both in a Web application that talks directly to the db as well as a smart client that uses Web services).
It requires a little extra effort, but it means you can design and use a real domain model and make it easier easier to use by hiding the complexity of dealing with these framework deficiencies for consumers of the domain model. This is especially helpful in situations where you have different sets of developers working on different layers of the application, but it is also ideal for use and reuse by future developers as well.
One of these days, I'll write some sample code to exemplify this approach, maybe as part of a future exemplar.
Notes 1. The weatherthing says it's 65 degrees Fahrenheit right now--at 1pm! 2. My observation is that it is safe to assume that when other people talk about services and Web services, these are the kind they're thinking of, even if they don't make the distinction I do in this post.
The opinions expressed herein are solely my own personal opinions, founded or unfounded, rational or not, and you can quote me on that.
Thanks to the good folks at dasBlog!
Copyright © 2017 J. Ambrose Little