实现PUT API方法以更新实体的最简单方法 [英] Easiest way to implement PUT API method to update entity

查看:105
本文介绍了实现PUT API方法以更新实体的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web Api项目,当前更新实体是通过POST完成的。创建实体也可以通过相同的POST方法完成,我通过检查用户是否向我发送了一个实体ID来实现,然后实现了与创建相对的更新。

I have a Web Api project and currently updating an entity is done via POST. Creating an entity is also done via the same POST method, and I implement by checking if the user has sent me an entity Id and then implementing an update as opposed to a create.

这有很多明显的原因,是不好的。从客户端的角度来看,这也令人困惑,并使整个方法变得缓慢且难以定义。

This is bad good for a number of obvious reasons. It's also confusing from a client perspective and makes the whole method slow and hard to define.

所以我将更新拆分为PUT方法,我希望它成为像这样(伪代码)

So I'm splitting updating into a PUT method, and I want it to be like so (psuedo code)

public string PUT(MyModel model){

    //Check if model Id is sent. return if not.


    //Check what other fields have been sent.


    //Update only those fields using EF and save.

    }

我想实施:

var toUpdate = context.MyModels.FirstOrDefault(x => x.Id == model.Id);

if(toUpdate == default(MyModel)){ //Return error to client }

if(model.first != null){ toUpdate.first = model.first; }
//Repeat for every field.

context.MyModels.AddOrUpdate();
context.SaveChanges();

由于我是构建API的新手,所以我想知道这是否是创建PUT更新方法的标准。可能会因为基于意见而关闭,但是我想知道该实现是否可行,因为我在构建此API方面没有任何指导。

As I'm new to building API's I wondered if this is standard for creating PUT update methods. This might get closed as Opinion-based but I'd like to know if this implementation is okay as I've had no guidance in building this API really.

推荐答案

对于您的情况,我认为您可以像这样实现。

For your case, I think you can implement like this.

try
{
    context.Entry<MyModel>(model).State = EntityState.Modified;
    context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
    var msg = dbEx.EntityValidationErrors.Aggregate(string.Empty, (current1, validationErrors) => validationErrors.ValidationErrors.Aggregate(current1, (current, validationError) => current + (Environment.NewLine + $"Property: {validationError.PropertyName} Error: {validationError.ErrorMessage}")));
    var fail = new Exception(msg, dbEx);
    throw fail;
}

实际上,我更喜欢将更新代码放入 generic 类,这样我就不必为每个实体编写更新代码

Actually, I prefer putting update code inside generic class, so that I don't need to write the update code for every entity

public void DoUpdate<TEntity>(TEntity entity) where TEntity : class
{
    try
    {
        Context.Entry(entity).State = EntityState.Modified;
        Context.SaveChanges();
    }
    catch (DbEntityValidationException dbEx)
    {
        var msg = dbEx.EntityValidationErrors.Aggregate(string.Empty, (current1, validationErrors) => validationErrors.ValidationErrors.Aggregate(current1, (current, validationError) => current + (Environment.NewLine + $"Property: {validationError.PropertyName} Error: {validationError.ErrorMessage}")));
        var fail = new Exception(msg, dbEx);
        throw fail;
    }
}

这篇关于实现PUT API方法以更新实体的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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