问题描述:

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.

网友答案:

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

网友答案:

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 ;)

相关阅读:
Top