Question

I am unsure where to place my business logic. I have a WCF service which exposes its methods to my client.

Should my business logic go in the service method

public User GetUser(int id)
{  
     //Retrieve the user from a repository and perform business logic
     return user;
}

or should it be in a separate class where each WCF service method will in turn call the business layer methods.

public User GetUser(int id)
{  
     return _userLogic.GetUser(id);
}
Was it helpful?

Solution

My personal preference is to have WCF as a very thin layer on top of a separate business layer. The WCF layer does nothing more than make calls to the business layer, similar to what you have shown in option 2. This gives you some flexibility in the event that you want to have your business layer consumed by something other than WCF clients (for example, a WPF application calling your business layer directly rather than via WCF).

OTHER TIPS

WCF services are already, by default, designed for reuse. I see no reason not to have some logic in your services, though keep in mind things like the Single Responsibility Principle so you don't end up with a service that does a dozen things.

Even then, if you end up parceling out your functionality into smaller classes, it's not a bad idea at all to host those classes as WCF services. You can then use them in-proc (via pipes) when needed or across machine boundaries (tcp) or even as web services. Create facades as needed to provide access to the functionality of your other, smaller services.

There's no real need to avoid putting any logic in WCF service classes.

I think that the decision depends on your business needs. WCF is a mechanism to transport data (objects) between server and client. If you like your businsess logic runs on server, you should let WCF exposes the object after running your business logic.

It should go in a separate set of classes. Your WCF layer should only contain logic that directly pertains to how the product of the service is delivered.

In your case, I see that you have a WCF method that returns a User (I assume this is a custom class) why have a separate method to return the UserID instead of populating that property as part of returning the User object?

For reuse/testability/maintenance/readability you should always put you BL in a separate layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top