如何使用 System.Web.Http.OData.Delta 修补枚举?

akn*_*ds1 5 rest odata asp.net-web-api

尝试利用System.Web.Http.OData.Delta在 ASP.NET Web API 服务中实现PATCH方法,但似乎无法将更改应用于类型的属性IEnumerable<T>。我正在使用 Delta 的最新 Git 版本 (2012.2-rc-76-g8a73abe)。有人能够做到这一点吗?

考虑这种数据类型,应该可以在对 Web API 服务的 PATCH 请求中更新该数据类型:

public class Person
{
    HashSet<int> _friends = new HashSet<int>();

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public IEnumerable<int> Friends
    {
        get { return _friends; }
        set
        {
            _friends = value != null ? new HashSet<int>(value) : new HashSet<int>();
        }
    }

    public Person(int id, string firstName, string lastName)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
    }

    public Person()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

此 Web API 方法通过以下方式实现对 Person 的修补Delta<Person>

public void Patch(int id, Delta<Person> delta)
{
    var person = _persons.Single(p => p.Id == id);
    delta.Patch(person);
}
Run Code Online (Sandbox Code Playgroud)

如果我向服务发送包含以下 JSON 的 PATCH 请求,则该人的Friends属性应该被更新,但遗憾的是它没有发生:

{"Friends": [1]}
Run Code Online (Sandbox Code Playgroud)

问题的关键在于如何让 Delta 更新Friends这些数据。另请参阅CodePlex 中的讨论

Fil*_*p W 3

问题可能是 Deta 会尝试将 JSON 分配JArray给您Hashset<int>

如果您将它用于 JsonMEdiaTypeFormatter 并且您内部化了 Delta 代码(意味着您可以修改它),您必须执行类似的操作(这很粗糙,但有效):

bool TrySetPropertyValue(string name, object value)在of内部 Delta<T>,返回 false:

        if (value != null && !cacheHit.Property.PropertyType.IsPrimitive && !isGuid && !cacheHit.Property.PropertyType.IsAssignableFrom(value.GetType()))
        {
           return false;
        }
Run Code Online (Sandbox Code Playgroud)

改成:

var valueType = value.GetType();
var propertyType = cacheHit.Property.PropertyType;
if (value != null && !propertyType.IsPrimitive && !propertyType.IsAssignableFrom(valueType))
{
    var array = value as JArray;
    if (array == null)
        return false;

    var underlyingType = propertyType.GetGenericArguments().FirstOrDefault() ??
        propertyType.GetElementType();
    if (underlyingType == typeof(string))
    {
        var a = array.ToObject<IEnumerable<string>>();
        value = Activator.CreateInstance(propertyType, a);
    }
    else if (underlyingType == typeof(int))
    {
        var a = array.ToObject<IEnumerable<int>>();
        value = Activator.CreateInstance(propertyType, a);
    }
    else
        return false;
}
Run Code Online (Sandbox Code Playgroud)

这仅适用于int或的集合string,但希望能将您推向一个好的方向。

例如,现在您的模型可以具有:

public class Team {
        public HashSet<string> PlayerIds { get; set; }
        public List<int> CoachIds { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

并且您将能够成功更新它们。