design advise selecting object for webservice methods

D

dan655t

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!
 
A

Alberto Poblacion

[...] 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.
 
D

dan655t

[...] 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.
 
D

dan655t

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
 
D

dan655t

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top