简体   繁体   中英

How do I compare two instances of a ViewModel for value equality in C#

So I have a ViewModel for searching my database. I intend to refactor away the result set collection. I don't think it belongs in this ViewModel. All I want to do is when the user submits the page is compare the ViewModel passed to my Controller Action with the ViewModel stored in TempData. So I can tell if it is the SameSearch.

So how could that be done? considering I have many many many properties in my viewModel I'd prefer not to compare one by one. And using Json.Encode to serialize them and compare seems "hacky" to me.

Also, if I do have to compare one by one. Should that be done by overriding .Equals()

controller action Search()

public ActionResult Search(SearchViewModel model)
{
    SearchViewModel savedSearch = null;

    if (Request["submit"].Equals("reset",StringComparison.OrdinalIgnoreCase))
    {
        TempData.Remove("filter");
        model = new SearchViewModel();
    }
    else if (TempData.ContainsKey("filter"))
    {
        savedSearch = (SearchViewModel)Convert.ChangeType(TempData["filter"], typeof(SearchViewModel));
    }

    var currentmodel = System.Web.Helpers.Json.Encode(model);
    var savedmodel = System.Web.Helpers.Json.Encode(savedSearch);

    if (savedSearch == null)
    {
        model.NewSearch = true;
    }

    //The search hasn't changed.
    if(currentmodel == savedmodel) 
    {
        model.isSameSearch = true;
    }
    else
    {
        model.isSameSearch = false;
    }
    //do more stuff
    //return view
}

ViewModel SearchViewModel

public class SearchViewModel
{

    public SearchViewModel()
    {
        Page = 1;
        BuildingSearch = new SearchBuildingViewModel();
        ParcelSearch = new SearchParcelViewModel();
        SalesSearch = new SearchSalesViewModel();
        PropertyFeatureSearch = new SearchPropertyFeaturesViewModel();
        ValuesSearch = new SearchValuesViewModel();
    } 

    /// <summary>
    /// Gets or sets a value indicating whether use advanced search.
    /// </summary>
    public bool UseAdvancedSearch { get; set; }
    public bool NewSearch { 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; }

    #region Search View Models

    /// <summary>
    /// Gets or sets the building search.
    /// </summary>
    public SearchBuildingViewModel BuildingSearch { get; set; }

    /// <summary>
    /// Gets or sets the parcel search.
    /// </summary>
    public SearchParcelViewModel ParcelSearch { get; set; }

    /// <summary>
    /// Gets or sets the property feature search.
    /// </summary>
    public SearchPropertyFeaturesViewModel PropertyFeatureSearch { get; set; }

    /// <summary>
    /// Gets or sets the sales search.
    /// </summary>
    public SearchSalesViewModel SalesSearch { get; set; }

    /// <summary>
    /// Gets or sets the values search.
    /// </summary>
    public SearchValuesViewModel ValuesSearch { get; set; }

    #endregion

    /// <summary>
    /// Gets or sets the search results.
    /// </summary>
    [ScaffoldColumn(false)]
    public IPagination<ParcelResultItemViewModel> SearchResults { get; set; }
}

Implement IEquatable<T> in your ViewModel where T is the ViewModel class name, and create your own Equals method logic:

public bool Equals(ViewModel other)
{
    //compare properties, etc here
}

And override GetHashCode():

public override int GetHashCode()
{
    //you custom hash code algorithm
}

Consider using http://comparenetobjects.codeplex.com/ which will do a deep compare of objects. You can even use NuGet to get it: http://www.nuget.org/packages/CompareNETObjects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM