tags:

views:

37

answers:

4

Hi,

I am working on a WCF based web services project. We have like 50 different services which provide get,create, update and delete operations. My problem is when I find a bug in an operation, let's say in get operation, that is common on all the services, i need to open and replace all the code in all the 50 services. My question is, is there a way to implement one single core code template so any change I made will be used by all services? Basically I am looking something just like master page concept in ASP.NET. Let me give one imaginary example to illustrate my question better;

public string Get(Parameter param)
{
    ArrayList myList = new ArrayList();
    //some service specific business rules implemented here
    return myList.ToString();
}

Let's say that above code are being used by all the services and I want to change my ArrayList object to a List object like this:

public string Get(Parameter param)
{
    List<string> myList = new List<string>();
    //some service specific business rules implemented here
    return myList.ToString();
}

So, right now, I need to change all the services if I want to implement this by opening respective code files in each and every of them and then replacing the code. I am thinking using interfaces, which we already use now, but I cannot think a way to implement interfaces in such a way. If there is, do you have any examples? Or is there any other way around to solve this issue? I appreciate any help in advance. Thanks.

A: 

Its hard to say without some more examples, but could you shift the common parts to an abstract class? For example:

interface IMyService
{
    string Get(Parameter param); // common stuff
    void GetInternal(Parameter param); // implementation specific stuff
}

abstract class MyServiceBase : IMyService
{
    public string Get(Parameter param)
    {
        List<string> myList = new List<string>();
        GetInternal(param); // do service specific stuff
        return myList.ToString();
    }
}

class MyService1 : MyServiceBase
{
    public GetInternal(Parameter param)
    {
        // service specific stuff
    }
}

Option 2:

Could you use a T4 template or something to generate the bilerplate code for each service?

rally25rs
+1  A: 

You really should extract the internal logic of each web-service into re-usable and dependency-free generic component and leave the web service operation as a stub that just calls into this logic, e.g.:

//Convert:
public string Get(Parameter param)
{
    List<string> myList = new List<string>();
    //some service specific business rules implemented here
    return myList.ToString();
}

//Into:
public string Get(Parameter param)
{
    var serviceFor = new ModelService<MyModel>();
    return serviceFor.ExecuteGet(param);
}
mythz
A: 

We have WCF services and asmx Web services sharing code by putting all the implementation code into a class, and then each service instatiates that class and calls the method we want:

private ServiceImplementation _implementation = new ServiceImplementation();

public string Get(Parameter param)
{
    List<string> myList = new List<string>();
    //some service specific business rules implemented here
    return implementation.Get(param);
}

In that way, all of our implementation code is in one place.

Paul Manzotti
A: 

Usually I use the Services implementation as a very thin layer, kind of controller classes only. I hardly put logic into the class itself if I can avoid it. This seems the case on your example. Make sure that your service implementation only controls the basic flow of your service logic, and using "worker" classes to execute the logic itself. This way you can reuse/share the logic when necessary. This also helps the code to be more readable.

I hope this helps.

Wagner Silveira