服务堆栈将数据放入变量而不是数据库

时间:2014-05-30 15:15:06

标签: c# servicestack ormlite-servicestack

我使用服务堆栈将数据存储在我的数据库中。 使用示例MovieService,简单的HTTP POST允许存储数据。 http://mono.servicestack.net/ServiceStack.MovieRest/

我想将一些这些发布的数据放在变量中而不将其存储在我的数据库中。 有了这些数据,我就可以做一些SQL请求来获取一些外来的KEY。 我想知道这是否可行?如果是,请给我一个提示。 谢谢

1 个答案:

答案 0 :(得分:1)

好的,这很简单,有我的代码(来自ServiceStack.MovieRest示例的副本):

/// <summary>
///     Define your ServiceStack web service request (i.e. Request DTO).
/// </summary>
/// <remarks>The route is defined here rather than in the AppHost.</remarks>
[Api("GET or DELETE a single movie by Id. Use POST to create a new LifetouchRespRate and PUT to update it")]
[Route("/movies", "POST,PUT,PATCH,DELETE")]
[Route("/movies/{Id}")]
public class Movie : IReturn<MovieResponse>
{
    /// <summary>
    ///     Initializes a new instance of the movie.
    /// </summary>
    public Movie()
    {
        this.Genres = new List<string>();
    }

    /// <summary>
    ///     Gets or sets the id of the movie. The id will be automatically incremented when added.
    /// </summary>
    [AutoIncrement]
    public int Id { get; set; }

    public string ImdbId { get; set; }
    public string Title { get; set; }
    public decimal Rating { get; set; }
    public string Director { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string TagLine { get; set; }
    public List<string> Genres { get; set; }
}

/// <summary>
///     Define your ServiceStack web service response (i.e. Response DTO).
/// </summary>
public class MovieResponse
{
    /// <summary>
    ///     Gets or sets the movie.
    /// </summary>
    public Movie Movie { get; set; }
}

/// <summary>
///     Create your ServiceStack restful web service implementation.
/// </summary>
public class MovieService : Service
{
    /// <summary>
    ///     GET /movies/{Id}
    /// </summary>
    public MovieResponse Get(Movie movie)
    {
        return new MovieResponse
                   {
                       Movie = Db.Id<Movie>(movie.Id),
                   };
    }

    /// <summary>
    ///     POST /movies
    ///     returns HTTP Response =>
    ///     201 Created
    ///     Location: http://localhost/ServiceStack.MovieRest/movies/{newMovieId}
    ///     {newMovie DTO in [xml|json|jsv|etc]}
    /// </summary>
    public object Post(Movie movie)
    {
        Db.Insert(movie);
        var newMovieId = Db.GetLastInsertId();

        var newMovie = new MovieResponse
                           {
                               Movie = Db.Id<Movie>(newMovieId),
                           };

        return new HttpResult(newMovie)
                   {
                       StatusCode = HttpStatusCode.Created,
                       Headers =
                           {
                               {HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newMovieId.ToString())}
                           }
                   };           
    }

在MovieService类中,在数据库中注册数据是下面的原始数据,所以只需删除它就不要保存在数据库中:

Db.Insert(movie);

所以要创建变量来获取我可以像这样处理的所有信息:

        String imdbid = movie.ImdbId;
        String title = movie.Title;
        decimal rating = movie.Rating;

我进行了这种操作,因为它是通过http-post从其他应用程序接收数据,我必须在几个表上分享来自一个帖子的所有信息。我使用SQL请求找到了我的主键,其中包含我得到的信息。

感谢您的链接Scott,它对我有很大的帮助。我知道这对你来说很容易,但对于像我这样的初学者来说,当你拿到它时,它有点棘手而且很酷:)干杯!