开发者

Where to set up AutoMapper to convert asmx proxy objects to domain objects?

开发者 https://www.devze.com 2023-02-16 12:57 出处:网络
I am calling a .asmx web service that I have added to a project as a Web Reference. Any objects that come back from the webservice are in the namespace of the webservice.I would like to use AutoMappe

I am calling a .asmx web service that I have added to a project as a Web Reference.

Any objects that come back from the webservice are in the namespace of the webservice. I would like to use AutoMapper to map these objects to my domain objects.

e.g.:

namespace My.Domain
{
    public class Person开发者_JS百科
    {
        public string Name { get; set; }
        public Pet Pet { get; set; }
    }

    public class Pet
    {
        public string Name { get; set; }
    }
}

This works...

My.WebService ws = new My.WebService();
My.WebService.Person person = ws.GetPersonById(1);
My.WebService.Pet pet = person.Pet;

But I would like to do this...

using My.Domain;

My.WebService ws = new My.WebService();
Person person = ws.GetPersonById(1);  
Pet pet = person.Pet;

Is there somewhere I can put AutoMapper to interject in the webservice so I can do that? Where can I set it up? Or is there some other way I need to do this?


the easiest way to do this in AutoMapper is something like...

var request = Mapper.Map<DomainObject, ServiceReferenceObject>(requestDomainObject);
var result = ws.DoSomething(request);
var resultDomainObject = Mapper.Map<ServiceReferenceObject, DomainObject>(result);

Yes, you have an extra line each time you need to convert something to or from the service reference, but with AutoMapper at least it's just one line and all the messiness is hidden away inside the mapper so you don't have to write and maintain it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号