views:

158

answers:

3

What is considered the appropriate development for .asmx or wcf service classes regarding how many files, lines of code, responsibilities, etc? Do most people publish separate .asmx service files for the different crud methods for every class?

+2  A: 

Grab this book from wherever you can:

Service Oriented Architecture (SOA): Concepts, Technology, and Design

Answers each of your questions and many many more you're bound to run into during your implementation.

Justin Niessner
Thanks. I'll try to pick it up sometime. Anyone care to share some insight on my question until then?
suedeuno
+1 for referencing a Thomas Erl book. There is no better source of knowledge about SOA than his compendium.
jrista
+2  A: 

First of all, the best practice for new development is to use WCF. See Microsoft: ASMX Web Services are a “Legacy Technology”.

Second, in SOA, one tries to create services with coarsly-grained operations. For instance, you would want an OrderProduct operation, rather than StartOrder, AddLineItem, AddOption, FinishOrder operations. The OrderProduct operation might accept an OrderDTO as follows:

public class OrderDTO {
    public CustomerInfo Customer {get;set;}
    public DateTime OrderTime {get;set}
    public DateTime ShipTime {get;set;}
    public List<LineItemDTO> LineItems {get; private set;}
}

public class LineItemDTO {
    public int LineItemNumber {get;set;}
    public string ProductName {get;set;}
    public int Quantity {get;set}
    public Decimal Amount {get;set}
    public Decimal ExtendedAmount {get;set;}
}

Rather than a StartOrder method that just creates an empty order, followed by AddLineItem calls to add individual line items (as you might do from a desktop application), I'm recommending a single OrderProduct method that accepts an OrderDTO, which will have a collection of LineItemDTO. You'll send the entire order all at once, add all the pieces in a transaction, and be done.

Finally, I'd say that you should still separate into business and data layers. The service layer should be concerned only with the services side of things, and will call on your business logic layer in order to get things done.

John Saunders
I agree with WCF over asmx. Care to elaborate on OrderProduct over the other operations? Also agree with the last statement.
suedeuno
Yep, makes sense. Thanks for adding an explanation.
suedeuno
+2  A: 

Generally speaking, a service should encapsulate a set of common operations. Regardless of whether you use ASMX or WCF, you shouldn't be creating one "service" for each operation. The general idea behind service-oriented architecture (SOA) is to model real-world business behavior. To give you a dumb, but hopefully effective, example...think of a waitress at a restaurant. The waitress provides a service to customers in the form of taking orders, serving those orders, providing drink refills, providing condiments, and finally handling payment. The service the waitress offers is not a single operation, its an aggregation of related operations.

However, it doesn't stop there. The true nature of SOA is that any given service is likely to rely on other services. The waitress can not do her job without relying on the services of the cook, to provide meals, the person serving the counter, where she can get instances of condiments and drink, and the services provided by the restaurant building itself. There are also some fundamental differences between the kind of service provided by a waitress, and that provided by a cook. To bring it down to technical programming terms...a Waitress is a task service, but a Cook is an entity (or CRUD) service. The waitress handles higher level operations that provide useful functionality to clients, while the cook handles lower level operations that provide fine-grained and complex functionality only to other employees of the restaurant.

I can't really give you a specific answer to your question, other than to say just organize your services however they logically fit. Its probably not a good practice to have one operation per service...however, it is not unheard of for a service to have just one operation. Task services often have just one operation. Entity services often have many operations, usually CRUD based, but sometimes additional services. There are also Utility services that provide lowest level, infrastructural operations (back to the restaurant, utility services would be like stoves, grills, the register, etc.) If you model your services after actual business concepts, then the operations they expose and their dependencies on each other should eventually become clear.

For some GREAT information on SOA, check out the SOA series by Thomas Erl (Prentice Hall), as they are the definitive resource for implementing a service-oriented enterprise.

jrista
Thanks for the information. The problem I see about lumping services together in a single web service is you can end up with thousands of lines of code making it horrific to maintain.
suedeuno
Unless your services are massively complex and you are not properly architecting your domain, you should not have thousands of lines in a single service. Services are gateways to domain behavior...but generally they should not contain all of the domain behavior themselves. If you have thousands of lines of code in a service with multiple operations, then you have a serious architectural problem. Separate concerns, divide responsibilities, and distribute your code amongst multiple classes to solve a problem. Never lump it all up into one monstrosity of a service.
jrista
That is the problem. This is inherited code and everything is lumped into one service. So that's why I asked how most people handle it. I come from a oop background so everything has a single responsiblity.
suedeuno
Based on your two comments, it sounds like you don't fully understand the difference between a service and an operation. Your first comment you stated "lumping services together in a single web service". A service is, according to SOA principals, a single autonomous thing that provides one or more operations exposed over some transport (usually the web, hence "web services"). You don't lump multiple "services" into a single "web service", nor do you lump everything into a single service. You need to identify which methods (operations) belong together, and create a *service* out of them.
jrista
That service can then be implemented on some platform (i.e. .NET, C#), and exposed to consumers via a variety of transports (i.e. WCF and http, tcp, named pipes, REST, etc.) I highly recommend that you look into Thomas Erl's book *"SOA Principals of Service Design"*, which is an EXCELLENT book that will properly introduce you to SOA. Find his books here: http://www.soabooks.com/
jrista
I understand the difference between a service and an operation. I didn't say "services in a single web service", I said everything meaning a ton of methods with differing responsibilities lumped into one service.
suedeuno
I guess I am a bit confused. Were you able to break up your code into multiple, cohesive services? Were you able to solve the architectural problem you were presented with? Or do you still need help?
jrista
I'm currently breaking apart the services in one application and moving them to WCF. This particular service wasn't as large as some of the others but it's a start. The joy of inheriting legacy apps.
suedeuno