指定一个唯一的标识符属性跨越的WebAPI模型对象 [英] Specify a unique identifier attribute for an object across webapi Models

查看:185
本文介绍了指定一个唯一的标识符属性跨越的WebAPI模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个POST调用到的WebAPI我试图返回一个创建(NEWOBJECT)的事情。但存在用于创建于ApiController只能采取对象,并完成剩下的没有签名

In a POST call to a WebApi I am trying to return a Created(newobject) thing. But there is no signature for Created in ApiController that can only take the object and do the rest.

它工作正常,如果我回到这样的:

It works fine if I return something like:

return Created(newobject.blahid.ToString(), newobject);



如果我做了

or if I do a

return CreatedAtRoute("DefaultApi", new { controller = ControllerContext.ControllerDescriptor.ControllerName, id = newobject.blahid.ToString()}, newobject);



我要简化为:

I want to simplify this to:

return Created(newobject);



我需要在BaseController实施方法

I would need to implement a method in a BaseController

public class BaseController : ApiController
{
    protected new CreatedNegotiatedContentResult<T> Created<T>(T content)
    {
        var id = GetId(content);//need help here
        return base.Created(id, content);
    }
}



我不希望担心唯一标识符一个对象被称为不同的方式在不同的车型如: myobjguid,someblahguid等我只是想找到它并将其标记为身份证。

I don't want to worry about the Unique Identifier for an object being called differently in different models e.g. myobjguid, someblahguid etc. I would just want to find it out and mark it as "id".

说,如果我的模型是

 public class Model_A
{
    public List<Model_A> ChildModels { get; set; }

    [LookForThisAttribute]//I want something like this
    public Guid Model_AGuid { set; get; }

    public Guid ? ParentGuid { set; get; }

    public List<SomeOtherObject> OtherObjects { set; get; }
}

有一个属性([LookForThisAttribute]),或者是我可以设置我的所有车型,以指定这是假设作为唯一标识符,如果我曾经寻找它的人。

Is there an attribute([LookForThisAttribute]) or something I can set on all my models to specify that this is the guy to be assumed as THE unique identifier if I ever look for it.

就像在实体框架[键]属性。不管你叫它,实体框架知道它的将是主键。

Just like the [Key] attribute in Entity Framework. No matter what you call it, Entity Framework know its going to be the primary key.

所以GETID(T含量)方法可以把对象和返回的值具有属性 [LookForThisAttribute] 设置?

So the GetId(T content) method can take the object and return the value of the property that has a [LookForThisAttribute] set?

推荐答案

我结束写出来我自己的属性,然后在BaseController仰视它。

I ended up writing my own Attribute and then looking up for it in the BaseController.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class UniqueIdAttribute: Attribute
    {
    }

而在BaseController创建方式:

And in the BaseController Created method:

    protected CreatedNegotiatedContentResult<T> Created<T>(T content)
            {
                            var props =typeof(T).GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(UniqueIdAttribute)));
            if (props.Count() == 0)
            {
                //log this
                return base.Created(Request.RequestUri.ToString(), content);
            }
            var id = props.FirstOrDefault().GetValue(content).ToString();
            return base.Created(new Uri(Request.RequestUri + id), content);
}



在这里马克Gravell的帖子帮我越来越具有财产的价值我自定义属性:
如何获得性能与给定的属性列表?

随着该控制器相应的单元测试工作正常,我。

Along with a corresponding unit test for the controllers works fine for me.

现在我可以叫创建(anyobject); 所有ApiControllers而没有对不同的名称,人们把它们的ID,只要他们装饰它我的自定义属性。

Now I can just call Created(anyobject); from all ApiControllers without bothering about the different names people put for their IDs as long as they decorate it with my custom attribute.

这篇关于指定一个唯一的标识符属性跨越的WebAPI模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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