Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling web methods from the same web service

I need to write a wrapper for a collection of web methods that are exposed in a particular web service. It makes sense to stick this new wrapper method in the same service since it's affecting the same type of object.

Most of these web methods are just thin methods which call static methods in other files, which is fine, but they also have some security logic done before these static method calls. Rather than recreate the security logic before each method call I want to wrap, is it possible to just call these other web methods from inside the same service locally, or is this bad practice?

Here's an example:

[WebMethod]
public int SmallMethod1(int a)
{
    //SecurityLogic
    return AnObject.StaticMethod1();
}

[WebMethod]
public int SmallMethod2(int b)
{
    //SecurityLogic
    return AnObject.StaticMethod2();
}

[WebMethod]
public int WrapperMethod(int c)
{
    return AnObject.StaticMethod1() + AnObject.StaticMethod2();
}
like image 623
C-Mart Avatar asked Oct 14 '22 17:10

C-Mart


1 Answers

In general you will want to separate the public interface of your web service from the actual implementation as cleanly as possible, in your example you have done this by encapsulating them in AnObject which allows unit testing the encapsulated methods separately (which is a big problem otherwise especially with web methods).

Having said that from a testing perspective I would suggest rather doing this:

[WebMethod]
public int WrapperMethod(int c)
{
    return AnObject.WrapperMethod(c)
}

This would allow you to write tests that test WrapperMethod directly (encapsulated in AnObject), rather than trying to recreate testing for AnObject.StaticMethod1() + AnObject.StaticMethod2() in your unit tests - this gets messy quickly because now you have the same logic in two different spots.

like image 148
BrokenGlass Avatar answered Oct 17 '22 01:10

BrokenGlass