Search code examples
c#asp.net-mvcpost-redirect-get

Implementing a feature to track if the user is repeating a search


I have a view model that represents all the fields available for searching. I'd like to add some logic that would be able to identify if the search values are all the same and determine whether to hit the DB again for their query.

I think I would have to do something like..

  • after user submits form save form values to some temporary field.
  • upon second submission compare temp value to form values collection.
  • if values are equal set property in view model IsSameSearch = true

I'd like to use the Post Redirect Get Pattern too. So that My search View doesn't do anything except post the form values to another action that processes and filters the data, which is then "Getted" using Ajax.

The SearchViewModel contains many many search parameters. Here is an abbreviated version.

    public bool UseAdvancedSearch { get; set; }
    public bool isSameSearch { get; set; }
    /// <summary>
    /// Gets or sets the page.
    /// </summary>
    [HiddenInput]
    [ScaffoldColumn(false)]
    public int Page { get; set; }

    [HiddenInput]
    [ScaffoldColumn(false)]
    public string SortOption { get; set; }

    /// <summary>
    ///     Gets or sets the address keywords.
    /// </summary>
    [Display(Name="Address")]
    public string AddressKeywords { get; set; }

    /// <summary>
    ///     Gets or sets the census.
    /// </summary>
    public string Census { get; set; }

    /// <summary>
    ///     Gets or sets the lot block sub.
    /// </summary>
    public string LotBlockSub { get; set; }

    /// <summary>
    ///     Gets or sets the owner keywords.
    /// </summary>
    [Display(Name="Owner")]
    public string OwnerKeywords { get; set; }

    /// <summary>
    ///     Gets or sets the section township range.
    /// </summary>
    public string SectionTownshipRange { get; set; }

    /// <summary>
    ///     Gets or sets the strap.
    /// </summary>
    ///
    [Display(Name="Account Number/Parcel ID")]
    public string Strap { get; set; }

    /// <summary>
    ///     Gets or sets the subdivision.
    /// </summary>
    public string Subdivision { get; set; }

    /// <summary>
    /// Gets or sets the use code.
    /// </summary>
    [Display(Name = "Use Code")] 
    public string UseCode { get; set; }

    /// <summary>
    ///     Gets or sets the zip code.
    /// </summary>
    [Display(Name="Zip Code")]
    public string ZipCode { get; set; }

Solution

  • If you are getting data from Entity Framework you could cache the data at EF level. Look at the package entity framework extended https://github.com/loresoft/EntityFramework.Extended. It is as simple as adding method .FromCache () to the query you use to retrieve and filter the data and it will cache the query result. Make sure you load all the data required using includes etc.

    You wouldn't have to worry about same search in model as the caching provider would look at filter settings and determine that it was different. Alternatively cache the data before filtering and then filter the cached results. This is more appropriate if you have lots of filter parameters with significant variance as you will only have to cache 1 large result rather than thousands of smaller results.

    You can get more advanced and specify cache period e.g. Cache for 10 minutes