Why isn't UpdateModel virtual?

Last post 02-09-2009 1:55 PM by levib. 1 replies.

Sort Posts:

  • Why isn't UpdateModel virtual?

    02-09-2009, 6:03 AM
    • Member
      4 point Member
    • simeyla
    • Member since 01-29-2009, 9:45 PM
    • Posts 8

    I've heard Phil on a podcast say 'traditionally people make things non-virtual unless theres a good reason not to, but we decided to make things virtual unless theres a good reason not to'...

     Therefore you tell me why all the Controller.UpdateModel methods are non-virtual ? Is there a legitimate good reason?

     I want to be able to do this :

    I have an abstract base controller, and a model (ModelBase) used by all the models in my page. I want to be able to do UpdateModel (productsModel) and have it update the base model too.

    I just don't want to use 'new' as I am having to do.

    public abstract class RRController : Controller
    {

    public RRController()
    :
    base() {
    }

    protected internal new void UpdateModel<TModel>(TModel model) where TModel : ModelBase
    {
    base.UpdateModel(model);
    UpdateBaseModel(model);
    }

     

    public void UpdateBaseModel(ModelBase model)
    {
    model.Initialized =
    true;
    model.PromotionID = ((
    string)Session["PromotionId"] ?? "").ToUpper();
    model.PartnerId = ((
    int?)Session["PartnerId"]) ?? 6; // rolling razor #&#&#&#&#&
    }

    }

  • Re: Why isn't UpdateModel virtual?

    02-09-2009, 1:55 PM
    Answer
    • Contributor
      4,587 point Contributor
    • levib
    • Member since 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 790
    • AspNetTeam

    I don't remember offhand if we had a good reason for sealing that method or if this was simply an oversight on our part.  In any case, your scenario is not a good candidate for overriding the UpdateModel() method since overriding a method doesn't allow you to change generic constraints.

Page 1 of 1 (2 items)