Domanda

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; }
È stato utile?

Soluzione

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

Altri suggerimenti

What you are describing is called caching.

One way to accomplish that in your scenario would be to implement GetHashCode() in a way that it would take into account all your fields/properties to compute a unique value. That way you can use your Hash as the key entry in your cache, and store the results with that key.

For that actual caching you could just use the MemoryCache class provided by the .Net Framework if you are not deploying to a web farm.

Also, if you are familiar with IoC and DI (such as using Unity), things like this can be implemented as an Interceptor, and only requiring you to add an attribute to the method you'd like to cache. That way you implement caching only once as a cross-cutting concern and not fill up your application code with things like this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top