469,337 Members | 6,063 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,337 developers. It's quick & easy.

design advise selecting object for webservice methods

Hello,
I've come here to get some advise from the experts. Currently I am
tasked with modifying a web service that currently supports one
product to support multiple products. It needs to be backwards
compatible so other products still work with the same method calls and
the new products need to have methods with the same names, but
different logic.

Currently we keep some credential information from the client that
consumes the service in a session object that we've created upon them
logging in. I've added the product type to the session object.

I have created new object types for each new product that inherit from
the original object which includes all of the methods and override
methods that require different logic.

I would like to know how I could implement this so that when a
webservice method call comes in with a session variable passed that
contains the product type that the correct object's method is called.

i.e.

[WebMethod]
method1(Session session)
{
if (session.sessionType == product1)
return product1.mehtod1;
}
is there any way I can do this without using if/else or switch logic
in each webservice method? I was thinking of a function that maps the
session and returns a type so I can cast from the base object. But I
do not know how to do this, or if it is even possible. Thanks guys!

Sep 4 '07 #1
4 1371
<da*****@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
>[...] I've added the product type to the session object.

I have created new object types for each new product that inherit from
the original object which includes all of the methods and override
methods that require different logic.

[...]

is there any way I can do this without using if/else or switch logic
in each webservice method? I was thinking of a function that maps the
session and returns a type so I can cast from the base object. But I
do not know how to do this, or if it is even possible. Thanks guys!
If the various objects types for the different products are not too big
(they do not contain a lot of instance data), you could consider storing the
objects themselves (instead of their types) in the Session. In this way,
your web methods could just retrieve the object from the Session, cast it to
the base type, and call the corresponding method just the way you wanted.

If this is not suitable, you could put your "if/else or switch logic"
(that provides the adequate object from its type) inside a specific private
method that returns the base class for your products. Then call this private
method at the beginning of all your web methods and invoke the proper method
on the object returned.

If the switch logic becomes too big, you could simplify it by using
Reflection to create an object from the name of the type (stored in
Session), but I think that this is overkill and the single method containing
the switch statement should solve the problem quite nicely.

Sep 4 '07 #2
On Sep 4, 3:59 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
<dan6...@gmail.comwrote in message

news:11**********************@y42g2000hsy.googlegr oups.com...
[...] I've added the product type to the session object.
I have created new object types for each new product that inherit from
the original object which includes all of the methods and override
methods that require different logic.
[...]
is there any way I can do this without using if/else or switch logic
in each webservice method? I was thinking of a function that maps the
session and returns a type so I can cast from the base object. But I
do not know how to do this, or if it is even possible. Thanks guys!

If the various objects types for the different products are not too big
(they do not contain a lot of instance data), you could consider storing the
objects themselves (instead of their types) in the Session. In this way,
your web methods could just retrieve the object from the Session, cast it to
the base type, and call the corresponding method just the way you wanted.

If this is not suitable, you could put your "if/else or switch logic"
(that provides the adequate object from its type) inside a specific private
method that returns the base class for your products. Then call this private
method at the beginning of all your web methods and invoke the proper method
on the object returned.

If the switch logic becomes too big, you could simplify it by using
Reflection to create an object from the name of the type (stored in
Session), but I think that this is overkill and the single method containing
the switch statement should solve the problem quite nicely.
I will probably go forward with the second approach. The first and
third are good ideas, and I'm glad you mentioned them. Thanks so much
for the help.

Sep 5 '07 #3
On Sep 4, 3:59 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
If this is not suitable, you could put your "if/else or switch logic"
(that provides the adequate object from its type) inside a specific private
method that returns the base class for your products. Then call this private
method at the beginning of all your web methods and invoke the proper method
on the object returned.
I have a question about implementing this approach. When I call a
function in the object returned by the private method it always uses
the logic in the base class, rather than the class that inherits the
logic. Perhaps I am missing something...

//WebService
[WebMethod()]
public int ServerGetGdsPort()
{
BaseObject workingBaseObject =
getProduct(serviceModeEnum);
return workingBaseObject.Port;
}

//BaseObject
....
private int port = Global.Configuration.PortNumber;
public int Port
{
get { return this.port; }
}

//Object that inherits from base
....
private int port = Global.Configuration.PortNumber;
public new int Port
{
get { return this.port; }
}

Is there any way to use the logic in my inhered class here?

Thanks,
Danny

Sep 11 '07 #4
I have a question about implementing this approach. When I call a
function in the object returned by the private method it always uses
the logic in the base class, rather than the class that inherits the
logic. Perhaps I am missing something...

//WebService
[WebMethod()]
public int ServerGetGdsPort()
{
BaseObject workingBaseObject =
getProduct(serviceModeEnum);
return workingBaseObject.Port;

}

//BaseObject
...
private int port = Global.Configuration.PortNumber;
public int Port
{
get { return this.port; }

}

//Object that inherits from base
...
private int port = Global.Configuration.PortNumber;
public new int Port
{
get { return this.port; }

}

Is there any way to use the logic in my inhered class here?

Thanks,
Danny
I should have tried a little harder before posting. Anyway, to make
things complete I'll include the answer to my question.

I had to add the virtual identifier to the properties in the base
class and the override identifier to the ones in the derived class:

//WebService
[WebMethod()]
public int ServerGetGdsPort()
{
BaseObject workingBaseObject =
getProduct(serviceModeEnum);
return workingBaseObject.Port;

}

//BaseObject
...
private int port = Global.Configuration.PortNumber;
public virtual int Port
{
get { return this.port; }
}

//Object that inherits from base
...
private int port = Global.Configuration.PortNumber;
public override new int Port
{
get { return this.port; }
}

Now the logic which returns an object of the base class's type (that
is declared as an inherited object) works!

-Danny
Sep 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by hellrazor | last post: by
5 posts views Thread by mtv | last post: by
3 posts views Thread by John S | last post: by
4 posts views Thread by John Sitka | last post: by
9 posts views Thread by Jens Jensen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.