0

I have appSourceInfoModel taken from Database, now i am passing ViewModel i.e. reviewerAppsBacklogViewModel and if reviewerAppsBacklogViewModel and appSourceInfoModel are same then do not update database if there are changes then Update. I am doing, first assigning to variable var DBappSourceInfoModel = appSourceInfoModel; then I assigning some values to appSourceInfoModel then comparing the initially saved model DBappSourceInfoModel and appSourceInfoModel. But, assigning some values to appSourceInfoModel also change values in the initially saved model DBappSourceInfoModel. All of the code can be found below.

AppSourceInfo appSourceInfoModel = _appSourceInfoRepository.Get(a => a.Review.ReviewId == reviewId);

var DBappSourceInfoModel = appSourceInfoModel; //Initially save Model in var

appSourceInfoModel.Cost = reviewerAppsBacklogViewModel.Cost;
appSourceInfoModel.InProgress = true;
appSourceInfoModel.PegiRating = reviewerAppsBacklogViewModel.PegiRating;
appSourceInfoModel.Rating = reviewerAppsBacklogViewModel.AverageUserReviewsRating;
appSourceInfoModel.DownloadCounter = reviewerAppsBacklogViewModel.NoofDownloadsFromSource;
appSourceInfoModel.ReviewCounter = reviewerAppsBacklogViewModel.NoofReviewOfSource;
appSourceInfoModel.StoreCategory = reviewerAppsBacklogViewModel.StoreCategory;

var IsAppSourceInfoModelChanged = !DBappSourceInfoModel.Equals(appSourceInfoModel);
if (IsAppSourceInfoModelChanged)
{
    _appSourceInfoRepository.Update(appSourceInfoModel);
}

I have Solved it using this simple Code in My Model i.e. AppSourceInfo

 public object Clone()
    {
        return this.MemberwiseClone();
    }

and change the following code

var DBappSourceInfoModel = appSourceInfoModel; //Initially save Model in var

to

var DBappSourceInfoModel = (AppSourceInfo) appSourceInfoModel.Clone();
Aftab Ahmed
  • 665
  • 9
  • 17
  • What is your question? (and of course _assigning some values to appSourceInfoModel also change values in the initially saved model DBappSourceInfoModel_ - you have used `var DBappSourceInfoModel = appSourceInfoModel;` which means `DBappSourceInfoModel` holds a reference to `appSourceInfoModel`) –  May 31 '16 at 10:01
  • If you wish to create new reference of AppSourceInfo, simply use "new": `var DBappSourceInfoModel = new AppSourceInfo()`. DBappSourceInfoModel now contains different reference against appSourceInfoModel, thus any changes on appSourceInfoModel doesn't reflected directly in DBappSourceInfoModel. – Tetsuya Yamamoto May 31 '16 at 10:06
  • @Stephen Muecke thats y i have asked a question if there is any solution so that i can check changes in initial and changed model :) – Aftab Ahmed May 31 '16 at 10:16
  • i dont wanna check line by line changes :/ – Aftab Ahmed May 31 '16 at 10:17
  • You need to check it line by line (either in the controller, or have you model implement `IEquatable` and override `Equals` in the class - refer [this answer](http://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object)) –  May 31 '16 at 10:21
  • i did this `var DBappSourceInfoModel = (AppSourceInfo) appSourceInfoModel.Clone();` Change AppSourceInfo Model Added some code `public object Clone() { return this.MemberwiseClone(); }` – Aftab Ahmed May 31 '16 at 11:29
  • @AftabAhmad, That wont help at all and is pointless unless you have overridden the `.Equals()` method to compare the values of all properties in the model (`DBappSourceInfoModel.Equals(appSourceInfoModel)` returns false even if you do not change any values) –  Jun 01 '16 at 00:03

1 Answers1

1

You need to perform a Copy (shallow probably sufficient)

var DBappSourceInfoModel = appSourceInfoModel;

Is simply creating a reference to the same object. Implement ICloneable on the DBappSourceInfoModel then use Clone method,

Your Clone method needs to copy all the info to the new Object, also performing Deep Copies on internal references if needed,

This will copy all the details to the other object and create two separate objects,

look here for reference: https://msdn.microsoft.com/en-us/library/system.icloneable%28v=vs.110%29.aspx

EDIT

Just to be clear, you also need to use the IComparable interface to define how the objects are compared for equality,

https://msdn.microsoft.com/en-us/library/system.icomparable%28v=vs.110%29.aspx

Mark Homer
  • 960
  • 6
  • 15
  • i did this `var DBappSourceInfoModel = (AppSourceInfo) appSourceInfoModel.Clone();` **Change AppSourceInfo Model Added some code** `public object Clone() { return this.MemberwiseClone(); }` – Aftab Ahmed May 31 '16 at 11:23
  • This does nothing at all to solve OP's issue. All it does is take a copy of the model and the `.Equals()` method will always return `false` because the model is a reference type and they are now not the same reference even if all the values in both models are identical. –  Jun 01 '16 at 00:08
  • @StephenMuecke what? If you use Clone it is not a reference type and does everything to fix the issue. He wants to compare changes in class you cant do that by comparing the same Object. Your point is mute anyway as clearly it has addressed the OP's issue. – Mark Homer Jun 01 '16 at 09:03
  • Your not understanding. OP is trying to compare the 2 objects - the original and the 'cloned' object using the `Equals()` method and expect if each property is the same then it will return false - which it wont because they are 2 different references. This does nothing to solve the issue and OP will soon realize it. –  Jun 01 '16 at 09:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113496/discussion-between-stephen-muecke-and-mark-homer). –  Jun 01 '16 at 09:47
  • OK I thought it pretty obvious how to compare objects but still, have added edit, you need to use IComparable to define how objects are defined for equality, – Mark Homer Jun 01 '16 at 10:24
  • @StephenMuecke what should i use instead of .Equal(), because clone gives collection while original have null collection due **lasy loading** OFF – Aftab Ahmed Jun 01 '16 at 10:25
  • You need to do a Deep Copy to clone complex types within Clone, and use IComparable to define how you compare the Object for Equality I have done an edit – Mark Homer Jun 01 '16 at 10:50