views:

34

answers:

2

I am consuming an external C# Web Service method which returns a simple calculation result object like this:

[Serializable]
public class CalculationResult
{
    public string Name { get; set; }
    public string Unit { get; set; }
    public decimal? Value { get; set; }
}

When I add a Web Reference to this service in my ASP .NET project Visual Studio is kind enough to generate a matching class so I can easily consume and work with it.

I am using Castle Windsor and I may want to plug in other method of getting a calculation result object, so I want a common class CalculationResult (or ICalculationResult) in my solution which all my objects can work with, this will always match the object returned from the external Web Service 1:1.

Is there anyway I can tell my Web Service client to hydrate a particular class instead of its generated one? I would rather not do it manually:

foreach(var fromService in calcuationResultsFromService)
{
    ICalculationResult calculationResult = new CalculationResult()
    {
       Name = fromService.Name
    };
    yield return calculationResult;
}

Edit: I am happy to use a Service Reference type instead of the older Web Reference.

A: 

First of all, your web service - why do you actually GENERATE the classes? use common shared types.

http://www.codeproject.com/KB/WCF/WCFCollectionTypeSharing.aspx

This is obviously not an option when consuming a public web service, but as you control both ends... that has many advantages.

Among them complete code control ;)

TomTom
I do not own the code to the ASP .NET 2.0 web service I am consuming.
row1
Then I stonrgly suggest taking the classes and "dealing with them" as they are generated. Treat them as DTO (Dta Transfer Objects) and move them into your own objects ASAP. The results are basically a "read only" document for you. Do NOT add methods there. Keep it as it is.
TomTom
+1  A: 

You can use http://automapper.codeplex.com. Typically it is used to simplify domain objects to DTO.

Panji
This seems to be an acceptable compromise.
row1