网络API可查询 - 如何申请AutoMapper? [英] Web API Queryable - how to apply AutoMapper?

查看:211
本文介绍了网络API可查询 - 如何申请AutoMapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的装饰与OData的可查询属性的简单的WebAPI的方法。

  [可查询]
    公共虚拟的IQueryable< PersonDto>得到()
    {
        返回uow.Person()GETALL())。 //现在返回的人,而不是PersonD
    }
 

我想要做的就是从人型的转变查询结果使用AutoMapper之前的WebAPI将结果转换为JSON键入PersonDto。

有谁知道我能做到这一点?我知道,在GETALL()调用后,我可以申请Mapper.Map然后再转换回IQueryable的,但是这将导致返回并映射整个表之前的OData应用过滤器(不好!)。

这样看来,这个问题的的ASP.NET Web API返回查询的DTO的? 涵盖了同样的问题(参见更好的回答第二个响应),这里的建议是使用自定义MediaTypeFormatter使用AutoMapper在链的末端,但我不知道如何做到这一点的基础上的例子,我都看到了。

任何帮助将受到欢迎!

- 进一步信息

我已经看了源$ C ​​$下IQueryable的,可惜我看不到任何方式利用code用于这一目的的。我已成功地编写这似乎工作的一个额外的过滤器,但它是不是肯定是不优雅。

 公共类PersonToPersonDtoConvertAttribute:ActionFilterAttribute
{
    公众覆盖无效OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        HTT presponseMessage响应= actionExecutedContext.Response;

        如果(响应!= NULL)
        {
            ObjectContent responseContent = response.Content为ObjectContent;
            VAR的查询=(responseContent.Value为IQueryable的<学生>)了ToList()。
            response.Content =新ObjectContent< IEnumerable的< StudentResource>>(query.ToList()选择(Mapper.Map<人,PersonDto>),responseContent.Formatter);
        }
    }
}
 

然后,我已经布置得像

动作

  [可查询]
    [PersonToPersonDtoConvert]
    公众的IQueryable<人>得到()
    {
        返回uow.GetRepo< IRepository<人>>()GETALL();
    }
 

解决方案

有一个更好的解决方案。试试这个:

  public虚拟的IQueryable< PersonDto>获取(ODataQueryOptions<人>查询)
{
    变种人= query.ApplyTo(uow.Person()GETALL());
    返回ConvertToDtos(人);
}
 

这将确保查询运行于人,而不是PersonDTO。如果你想转换发生过一个属性,而不是在code,你仍然想实现类似于你把一个动作过滤器。

I've got a simple WebApi method like this decorated with the OData queryable attribute.

    [Queryable]
    public virtual IQueryable<PersonDto> Get()
    {
        return uow.Person().GetAll()); // Currently returns Person instead of PersonD
    }

What I want to do is transform the result of the query from type Person to type PersonDto using AutoMapper before WebAPI converts the result to JSON.

Does anybody know how I can do this? I am aware, I could apply Mapper.Map after the GetAll() call and then convert back to IQueryable, however this would result in the entire table being returned and mapped before the OData filter is applied (not good!).

It would appear that this question ASP.NET Web API return queryable DTOs? covers the same issue (see second response for a better answer), where the suggestion is to use AutoMapper at the end of the chain using a custom MediaTypeFormatter, however I have no idea how to do that based on the example I have seen.

Any help will be gratefully received!

-- Further Info

I've looked at the source code for IQueryable, but unfortunately there I can't see any way of utilising the code for this purpose. I have managed to write an additional filter which appears to work, however it isn't certainly isn't elegant.

public class PersonToPersonDtoConvertAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        HttpResponseMessage response = actionExecutedContext.Response;

        if (response != null)
        {
            ObjectContent responseContent = response.Content as ObjectContent;
            var query = (responseContent.Value as IQueryable<Student>).ToList();
            response.Content = new ObjectContent<IEnumerable<StudentResource>>(query.ToList().Select(Mapper.Map<Person, PersonDto>), responseContent.Formatter);
        }
    }
}

Then I have decorated the action like

    [Queryable]
    [PersonToPersonDtoConvert]
    public IQueryable<Person> Get()
    {
        return uow.GetRepo<IRepository<Person>>().GetAll();
    }

解决方案

There is a better solution. Try this:

public virtual IQueryable<PersonDto> Get(ODataQueryOptions<Person> query)
{
    var people = query.ApplyTo(uow.Person().GetAll());
    return ConvertToDtos(people);
}

This will make sure the query runs on Person instead of PersonDTO. If you want the conversion to happen through an attribute instead of in code, you'll still want to implement an action filter similar to what you put up.

这篇关于网络API可查询 - 如何申请AutoMapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆