ServiceStack How generate an Json response with only the Primary Key?

ⅰ亾dé卋堺 提交于 2019-12-25 02:29:45

问题


When I create a new record in my table I would like generate an json response with only the primary ID of my new record, somethink like : {"PrimaryID":123}

I actually use this handmade function:

    // Inserts a new row into the PatientSession table
    public string AddPatientSession(PatientSession p)
    {
        int id = (int)_dbConnection.Insert<PatientSession>(p, selectIdentity: true);
        string Idconvert = id.ToString();
        string IdInsert = "{\"PatientSessionId\":" + Idconvert + "}";
        return IdInsert;
    }

But I assume it's not the best way to do it, have you a suggestion please? Thanks in advance


回答1:


If you just want to return a small JSON payload with just an Id you can use a type with only the fields you want to return, e.g:

public class AddPatientSession : IReturn<PatientId> { ... }

public class PatientId {
    public int PatientSessionId { get; set; }
}

Then use in your service like:

public class MyServices : Service
{
    public object Any(AddPatientSession request)
    {
        var model = request.ConvertTo<PatientSession>();
        return new PatientId {
            PatientSessionId = Db.Insert(model, selectIdentity: true);
        }
    }
}

Returning an object takes advantage of ServiceStack's built-in Content Negotiation to return the object serialized in the preferred Content-Type, e.g. JSON for JSON/ajax clients.

You can also return an anonymous type containing just the Id:

public object Any(AddPatientSession request)
{
    var model = request.ConvertTo<PatientSession>();
    return new {
        PatientSessionId = Db.Insert(model, selectIdentity: true);
    }
}

Which will also serialize to JSON when requested, but the lack of a type does prevent this from being called with ServiceStack's generic typed Service Clients.




回答2:


Thanks you so much @mythz it's working well I just use a convert function to int because "Db.Insert" return a long type.

// Add PatientSession via POST
public class PatientSessionADD : IReturn<PatientSessionResponseId>
{
    public int PatientSessionId { get; set; }
    public int ByPatientId { get; set; }
    public DateTime PatientStartSessionTime { get; set; }
    public int PatientStartSessionByUserId { get; set; }
    public DateTime PatientEndSessionTime { get; set; }
    public int PatientEndSessionByUserId { get; set; }

}

public class PatientSessionResponseId
{
    public int PatientSessionId { get; set; }
}


public object Post(PatientSessionADD request)
    {
        var p =new PatientSession()
        {
                ByPatientId = request.ByPatientId,
                PatientStartSessionTime = request.PatientStartSessionTime,
                PatientStartSessionByUserId = request.PatientStartSessionByUserId
        };

        return new PatientSessionResponseId
        {
            PatientSessionID = Convert.ToInt16( Db.Insert<PatientSession>(p, selectIdentity: true) )
        };
    }

To resume this function get a HTTP POST message, store it in database and return a JSON response with only the Primary ID generated.

Have fun and thanks again mythz



来源:https://stackoverflow.com/questions/24657224/servicestack-how-generate-an-json-response-with-only-the-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!