0

Entity Framework と Backbone.js で ASP.NET Web API を使用しています

ユーザーが XYZ を更新できる更新モジュールに取り組んでいます。

現在、更新中に発生する可能性のある 3 つのケースがあります。

  • 成功
  • 失敗した
  • 見つかりません

だから私はこの列挙型を使用することにしました

enum UpdateStatus
{
    Success = 1,
    Failed = 0,
    NotFound = 2
}

だからここに私の方法がどのように見えるかです

public UpdateStatus UpdateXYZ(Model model)
{
    var data = _repo.Table.where(m => m.id == model.id);
    if(data.count == 0)
    {
        return UpdateStatus.NotFound;
    }

    try
    {
        // update here
        return UpdateStatus.Sucess; 
    }
    catch
    {
        // log errors
        return UpdateStatus.Failed;
    }
}

その後、サービス レイヤーで、同じ値を Web API アクションに返します。そして、Web APIアクションでは、次のようなものがあります...

public HttpResponseMessage Put(Details details)
{
    if (ModelState.IsValid)
    {
        //The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent.
        //return new HttpResponseMessage(HttpStatusCode.ResetContent);
        UpdateStatus = _magicService.UpdateXYZ(details);
        if (UpdateStatus.Success)
        {
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
        else if(UpdateStatus.NotFound)
        {
            return new HttpResponseMessage(HttpStatusCode.Gone);
        }
        return new HttpResponseMessage(HttpStatusCode.Conflict);
    }
    else
    {
        string messages = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage + " - " + (x.Exception == null ? "" : x.Exception.Message)));
        return Request.CreateResponse<string>(HttpStatusCode.BadRequest, messages.ToString());
    } 

}   

リポジトリ レイヤーで UpdateStatus 列挙型を定義し、それをサービス レイヤーと Web レイヤーでも使用しています。このアプローチについて意見が欲しいですか、それとも私がこれを行うことができた他の方法はありますか?

これについてあなたの考えを共有してください。

4

1 に答える 1

1

ドメイン モデルが、ビュー モデルを使用する必要がある Web API の境界を離れていない限り問題ありません。

于 2013-02-22T07:08:00.803 に答える