Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:35:06+00:00 2026-05-14T22:35:06+00:00

I am following the Nerd Dinner tutorial as I’m learning ASP.NET MVC, and I

  • 0

I am following the Nerd Dinner tutorial as I’m learning ASP.NET MVC, and I am currently on Step 3: Building the Model. One part of this section discusses how to integrate validation and business rule logic with the model classes. All this makes perfect sense. However, in the case of this source code, the author only validates one class: Dinner.

What I am wondering is, say I have multiple classes that need validation (Dinner, Guest, etc). It doesn’t seem smart to me to repeatedly write these two methods in the partial class:

public bool IsValid
{
    get { return (GetRuleViolations().Count() == 0); }
}

partial void OnValidate(ChangeAction action)
{
    if (!IsValid)
    {
        throw new ApplicationException("Rule violations prevent saving.");
    }
}

What I’m wondering is, can you create an abstract class (because “GetRuleViolations” needs to be implemented separately) and extend a partial class? I’m thinking something like this (based on his example):

public partial class Dinner : Validation {
    public IEnumerable<RuleViolation> GetRuleViolations() {
        yield break;
    }
}

This doesn’t “feel” right, but I wanted to check with SO to get opinions of individuals smarter than me on this. I also tested it out, and it seems that the partial keyword on the OnValidate method is causing problems (understandably so). This doesn’t seem possible to fix (but I could very well be wrong).

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:35:07+00:00Added an answer on May 14, 2026 at 10:35 pm

    I’m wondering is, can you create an
    abstract class … and extend a
    partial class?

    Sure, the partial keyword simply indicates that the class is implemented in multiple files – it has no real bearing on inheritance … except in one narrow respect:

    If the partial class contains a
    reference to a forward-declared
    partial method, but no part of that
    class implements that partial method –
    all calls to that partial method will
    be omitted by the compiler.

    So what does this mean. If your partial class declares a partial method in one of it’s parts, but no other part of your class defines the partial method – then you can’t call that partial method in any derived classes … since it won’t exist.

    Let’s look at an example:

    // file1.cs  (code gen'd)
    public partial class Validation {
        partial void OnValidate(ChangeAction action);
    
        private void SomeMethod() {
            OnValidate( ChangeAction.Whatever );
        }
    }
    
    // file2.cs (Validation class body)
    public partial class Validation {
        //partial void OnValidate(ChangeAction action) { ... }
    }
    
    public class Dinner : Validation {
        public void SomeOtherMethod() {
           OnValidate(null); // won't compile ... OnValidate doesn't exist
        }
    }
    

    You should also be aware that partial methods cannot have modifiers (like new, abstract, virtual, public, private, etc). This means you cannot override a partial method in a derived class. You can, however, define a virtual method that a partial method calls.

    To your general question, there’s nothing wrong with inheriting from partial classes or trying to avoid duplication of code. However, you need to work within the one or two limitations that partial classes/methods impose.

    In your example, if you want to avoid duplicating logic, you may need to define your partial methods in the base class to make sure they are always available. Derived classes would not be be able to override them – but if this is needed, then just don’t make those methods partial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.