Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have run into instances where someone would add columns to a table in the database, update the Entity Framework models for our solution, but forgot to update the forms to compensate for the added fields. We would typically realize the impact of this when a user filed a bug report that they updated the object and properties wouldn't update (or in some case, data would be lost). As such, I did some looking into doing a generic template for model validation that would work in "all" cases. It appears that what I've come up with successfully works, but would like to know if there is anything I could do to improve performance and/or readability. I have been told that reflection is a big no-no but it appears to be the best tool for the job in this case.

(I am aware that the perfect scenario would involve not forgetting to update the the insert/update stuff when adding properties to an object, but people can be forgetful.)

Here is the razor view code:

foreach (var property in Model.GetType().GetProperties().Where(property => !typeof (IEnumerable).IsAssignableFrom(property.PropertyType)).Where(property => !property.PropertyType.Namespace.Contains("Models")))
{
    <input type="hidden" name="@String.Format("{0}_2", property.Name)" id="@String.Format("{0}_2", property.Name)" value="@property.GetValue(Model, null)" />
}

And here is JavaScript that is used to display an alert if there are properties that are not accounted for:

function validateModelPropertiesForFormInputs() {
    $("#messageText").text('');
    $("#messages").hide();
    $("#missingModelProperties input[type='hidden']").each(function () {
        var actualPropertyName = $(this).attr("name").substr(0, ($(this).attr("name").length - 2));
        var value = $("#" + actualPropertyName).val();
        if (value != null) {
            $(this).remove();
        } else {
            $(this).attr("name", actualPropertyName);
            $("#messageText").append("You're not accounting for " + actualPropertyName + "!<br />");
            $("#messages").show('medium');
            $(".formSubmitButton").attr("disabled", "disabled");
        }
    });
}

The end result of this is so that if a developer (because this is only for the developer role - the razor view code isn't even executed if the user is just a user) goes to a form where there are properties of the model (outside of Entity Framework navigation properties) that are not being accounted for in the form itself, the submit button will be disabled and an error displays letting the developer know that someone dropped the ball and it needs to be picked back up. I am just curious if there might be a better way of achieving this without having to write extra code that is really just playing the role of big brother.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.