0

I'm trying to come up with realizing "changes validator". I'm using Entity framework at back-end and angular at front-end. Communication via WebApi. For many reason client send me List<DepartmentSettingDto> and other properties to my controller. This list can contains many objects which can be unmodified from user. So, I dont want to update database if DepartmentSetting has no changes with data from db. I want to have something like this:

public void UpdateDepartmentsSettings(DepatmentsSettingsDto depatmentsSettings)
{
    using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew
        , new TransactionOptions {IsolationLevel = IsolationLevel.Serializable}))
    {
        foreach (var departmentSetting in DepatmentsSettingsDto.DepartmentSettings)
        {
            var currentSetting = _context.DepartmentSettings.Find(departmentSetting.Id)
            if departmentSetting.Modified(currentSetting)
            {
                var newSetting = Mapper.Map<DepartmentSettingDto, DepartmentSetting>
                    (departmentSetting, currentSetting);
                Mapper.Map(currentSetting, newSetting);
            }
        }                
        _context.SaveChanges();
        transaction.Complete();
    }
}

Is it possible to write an extension method or something like this to avoid saving unmodified changes? Thanks in advance!

user3818229
  • 1,537
  • 2
  • 21
  • 46

1 Answers1

1

An extension method would require you to iterate through the properties of the object to check if they're different. A better solution to this would be to overload Equals (and GetHashCode) and just test for equality before updating the DB. Check this SO post for details on how to overload these methods.

Something else might be helpful if you have control over the frontend. In that case, possible approach would be to have the frontend watch for changes of any property, that might change and send that flag to you along with the DTO. I.e. put a CSS class on all your HTML elements where a change might occur and hook a jQuery handler to them. After the first change has occurred you can detach it. That way you don't need to even query the DB for this particular record in order to check for equality - just issue an update if the frontend say so.

This is what I mean:

<input type="hidden" id="isThereChanges" value="false" />

<div>
    <input type="text" class="editor-field" />
    <input type="text" class="editor-field" />
    <input type="checkbox" class="editor-field" />
    <input type="radio" class="editor-field" />
</div>

<script>
    $(document).ready(function() {
        $(".editor-field").on("change", ChangeDetected);
    });

    function ChangeDetected() {
        $(isThereChanges).val(true);
        $(".editor-field").off("change", ChangeDetected);
    }        
</script>

Well this works with a hidden field but you can replace it with a field in your DTO or even not bother the web server if there is no change, based on a check, just before you send the AJAX request to the WebAPI...

Community
  • 1
  • 1
Bozhidar Stoyneff
  • 3,576
  • 1
  • 18
  • 28