Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the single responsibility principle in large wcf services?

We're using about 7 services at the moment. There quite large.

Does anyone have any experience with the single responsibility principle and WCF services? Does this mean that you'll end up with lot's of small contracts? If so, how do you manage these in your application?

like image 969
Sorskoot Avatar asked Jan 14 '09 12:01

Sorskoot


People also ask

How do I use single responsibility principle?

The single responsibility principle (SRP) states that every class or module in a program should have responsibility for just a single piece of that program's functionality. Further, the elements of that responsibility should be encapsulated by the responsible class rather than spread out in unrelated classes.

Could you provide an example of the single responsibility principle?

For example, if we have a class that we change a lot, and for different reasons, then this class should be broken down into more classes, each handling a single concern.

How do you implement single responsibility principle in C#?

A class should have only one reason to change. Definition − In this context, responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes.

What are the benefits that can be attained if the project follow the single responsibility principle in its implementation?

The single responsibility principle provides another substantial benefit. Classes, software components and microservices that have only one responsibility are much easier to explain, understand and implement than the ones that provide a solution for everything.


1 Answers

I think you are confusing single responsibility with interface segregation.

From the client/service interface perspective, you should keep your contracts lean and mean. See below for an example of that.

On the SRP side of things, that should be entirely internal to the service implementation and the client should not be aware of this. If you service code is too large, split it up into classes. Then have your service code, at least initially, act as a facade and forward all the calls to the relevant objects. Later on, you have the option of spliting your service into multiple services. But be aware, that SOA and object oriented design, although overlap, are separate and have different requirements.

Interface segregation example: We have a service here at work that we use to do various functions on some business objects. The original service had one interface. As it grew, we realized we had three family of methods: data object persistence, business updates, business analysis. We split up into three contracts. Our client/service implements all 3, so the only thing we had to do was split the contract into three and setup two additional endpoints in our WCF configuration. Very simple.

Hope this helps.

like image 86
Szymon Rozga Avatar answered Nov 15 '22 03:11

Szymon Rozga