locked
About Repository data mainiatained in ASP.NET web api controller RRS feed

  • Question

  • User-814694070 posted

    Hi All,

    As HTTP is stateless, ASP.NET WebAPI would not support any kind of state management. In any ASP.NET WebAPI implementation , where Controller class has Repository Interface (induced with dependency injection). Repository class would connect with Database and holds Model information.

    What I am not able to understand, since ASP.NET WebAPI is stateless, how is the model information in Controller class is being maintained for multiple HTTP methods.

    For Example, in the below example how would the comments dictionary list is being maintained at the server end for each of the HTTP methods, if ASP.NET WebAPI is stateless?

    public class CommentsController : ApiController

     {

    ICommentRepository repository;

    public CommentsController(ICommentRepository repository)  {

    this.repository = repository;

     }

    public HttpResponseMessage PostComment(Comment comment)

     {

        comment = repository.Add(comment);

         var response = Request.CreateResponse<Comment>(HttpStatusCode.Created, comment);

         response.Headers.Location = new Uri(Request.RequestUri, "/api/comments/" + comment.ID.ToString());

               return response;

       }

    }

    public  class DictionaryCommentRepository : ICommentRepository

     {

           int nextID = 0;

           Dictionary<int, Comment> comments = new Dictionary<int, Comment>();

    --------------------

           public Comment Add(Comment comment)

            {

                comment.ID = nextID++;

                comments[comment.ID] = comment;

               return comment;

            }

    }

    Thanks

    Thursday, May 4, 2017 6:01 AM

All replies

  • User-2057865890 posted

    Hi lakshmiprasanna,

    A stateless protocol does not require the server to retain session information or status about each communications partner for the duration of multiple requests. In contrast, a protocol that requires keeping of the internal state on the server is known as a stateful protocol.

    reference:

    http://stackoverflow.com/a/3105337

    Best Regards,

    Chris

    Friday, May 5, 2017 6:12 AM
  • User-814694070 posted

    Thanks Chris..

    http://stackoverflow.com/a/3105337 , brings more clarity to me. What I am referring to Comments Dictionary is Resource and Resource state should be maintained same across clients.

    And maintaining in Server is the right place.

    Stateless means not having\maintaining  client information on the server. For each of the request by HTTP to Restfull service, complete client details should be flowing across.

    There is difference between Client State or application state and Resource State. Client or Application state should be maintained at client side and Resource state should be maintained at Server side.

    Thanks.

    Friday, May 5, 2017 9:09 AM